0

I am trying to build a selenium test (java) that goes through WebAuthn authentication. Thanks to thread here I am able to retrieve the private key. The question is how to store and reuse it? I tried to store private key to the disk

final PKCS8EncodedKeySpec privateKey = authenticator.getCredentials().get(0).getPrivateKey();
File outputFile = new File("./private.key");
Files.write(outputFile.toPath(), privateKey.getEncoded());

Then when I run test case for the same user I try to load it and create an instance of Credential like this:

Credential credential = Credential.createNonResidentCredential(
                id, "null", new PKCS8EncodedKeySpec(key), /*signCount=*/1);

and load the credentials to my driver:

VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions();
        options.setTransport(VirtualAuthenticatorOptions.Transport.INTERNAL)
                .setHasResidentKey(true)
                .setProtocol(VirtualAuthenticatorOptions.Protocol.CTAP2);
VirtualAuthenticator authenticator = ((HasVirtualAuthenticator) driver).addVirtualAuthenticator(options);
authenticator.addCredential(credential);

but the credential is got refused. Unfortunately I haven't found any information on how to transfer the credentials between the sessions. What is the usual workflow in this case? It should be quite common scenario I think

jazzyekim
  • 101
  • 1
  • 10

1 Answers1

0

I have run into a similar issue, my use case is webauthn registration and login on different tabs ( = I have to transfer the credentials).

After registration I save the credentials of the user:

VirtualAuthenticator virtualAuthenticator = ((HasVirtualAuthenticator) Driver.get()).addVirtualAuthenticator(new VirtualAuthenticatorOptions());
// setup happens
Credential credential = authenticator.getCredentials().get(0);
userAccount.addCredential(credential);

On login I do this:

VirtualAuthenticator virtualAuthenticator = ((HasVirtualAuthenticator) Driver.get()).addVirtualAuthenticator(new VirtualAuthenticatorOptions());

Credential credential = userAccount.getFido2Credential();
Credential c = Credential.createNonResidentCredential (credential.getId(),"<whatever is your rpid(~domain)>", credential.getPrivateKey(), credential.getSignCount());

authenticator.addCredential(c);

My issue was that the rpid was null in the credential object and the authenticator's addCredential method failed because of this.

googyi
  • 1