硒测试:使用Webauthn进行身份验证

时间:2020-08-18 21:49:18

标签: javascript selenium testing webauthn

在我的用例中,有一个注册页面可触发特定于浏览器的webauthn流。例如,在Mac上的Chrome中,您会看到以下一系列弹出窗口:

  1. 在USB安全密钥和内置传感器之间选择一个选项
  2. 使用Touch ID确认MacOS
  3. Chrome的确认对话框,要求访问您的安全密钥

除了https://w3c.github.io/webauthn/#add-virtual-authenticator之外,我还没有找到很多有关硒测试中使用webauthn进行身份验证的文档。 哪些资源可用于帮助开发人员弄清楚如何使用JavaScript使用Selenium测试Webauthn?我还签出了https://github.com/SeleniumHQ/selenium/issues/7829,但示例测试用例对我而言没有意义。例子将得到极大的赞赏。

使用js解决方案更新

  import { Command } from 'selenium-webdriver/lib/command';

  addVirtualAuthenticator = async () => {
    await this.driver.getSession().then(async session => {
      this.driver
        .getExecutor()
        .defineCommand('AddVirtualAuthenticator', 'POST', `/session/${session.id_}/webauthn/authenticator`);

      let addVirtualAuthCommand = new Command('AddVirtualAuthenticator');
      addVirtualAuthCommand.setParameter('protocol', 'ctap2');
      addVirtualAuthCommand.setParameter('transport', 'internal');
      addVirtualAuthCommand.setParameter('hasResidentKey', true);
      addVirtualAuthCommand.setParameter('isUserConsenting', true);
      await this.driver.getExecutor().execute(addVirtualAuthCommand);
    });
  };

请注意,this.driver的类型为WebDriver

在击中任何与addVirtualAuthenticator进行交互的代码之前请致电navigator(在我们的情况下,用户注册涉及对navigator.credentials.create的呼叫)。如果您需要在登录时通过navigator.credentials.get({ publicKey: options })访问publicKey,则hasResidentKey关键

2 个答案:

答案 0 :(得分:3)

如果您要在Java中实现此功能并使用硒4,那么一个很好的示例资源是the tests on selenium itself。您基本上需要

  • 创建虚拟身份验证器

    在这种情况下,您应该将传输设置为internal,并将hasUserVerification设置为true以模拟touchID。

VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions();
options.setTransport(Transport.INTERNAL)
       .hasUserVerification(true)
       .isUserVerified(true);
VirtualAuthenticator authenticator =
    ((HasVirtualAuthenticator) driver).addVirtualAuthenticator(options);
  • 执行触发注册的操作。

    如果一切正常,则浏览器不应显示对话框。相反,它应该立即返回证书。

对于任何其他语言或硒版本,您将需要直接调用WebDriver协议。正如您所指出的,W3C spec has documentation on the protocol endpoints

对于Java,可能类似于

browser.driver.getExecutor().defineCommand(
    "AddVirtualAuthenticator", "POST", "/session/:sessionId/webauthn/authenticator");

// ...

Command addVirtualAuthCommand = new Command("AddVirtualAuthenticator");
addVirtualAuthCommand.setParameter("protocol", "ctap2");
addVirtualAuthCommand.setParameter("transport", "usb");
browser.driver.getExecutor().execute(addVirtualAuthCommand);

对于javascript,您应该能够以类似的方式使用defineCommandwebDriver.execute

答案 1 :(得分:-2)

这是硒的最坏习惯

Two Factor Authentication shortly know as 2FA is a authorization mechanism where One Time Password(OTP) is generated using “Authenticator” mobile apps such as “Google Authenticator”, “Microsoft Authenticator” etc., or by SMS, e-mail to authenticate. Automating this seamlessly and consistently is a big challenge in Selenium. There are some ways to automate this process. But that will be another layer on top of our Selenium tests and not secured as well. So, you can avoid automating 2FA.

绕过2FA检查的方法很少:

1。在测试环境中为某些用户禁用2FA,以便您可以在自动化中使用这些用户凭据。 2.在测试环境中禁用2FA。 3.如果从某些IP登录,请禁用2FA。这样,我们可以配置测试机IP来避免这种情况。