Now we want to register a new authenticator and attach it to a user. This step can be done during the creation of a new user account or if the user already exists and you want to add another authenticator.
You can attach several authenticators to a user account. It is recommended in case of lost devices or if the user gets access on your application using multiple platforms (smartphone, laptop…).
To register a new authenticator, you need to generate and send a set of options to it. These options are defined in a Webauthn\PublicKeyCredentialCreationOptions object.
To generate that object, you just need to call the methodgeneratePublicKeyCredentialCreationOptions of the $server object. This method requires a Webauthn\PublicKeyCredentialUserEntity object that represents the user entity to be associated with this new authenticator.
<?phpuseWebauthn\PublicKeyCredentialUserEntity;useWebauthn\PublicKeyCredentialCreationOptions;$userEntity =newPublicKeyCredentialUserEntity('john.doe','ea4e7b55-d8d0-4c7e-bbfa-78ca96ec574c','John Doe');/** This avoids multiple registration of the same authenticator with the user account **//** You can remove this code if it is a new user **/// Get the list of authenticators associated to the user$credentialSources = $credentialSourceRepository->findAllForUserEntity($userEntity);// Convert the Credential Sources into Public Key Credential Descriptors$excludeCredentials =array_map(function (PublicKeyCredentialSource $credential) {return $credential->getPublicKeyCredentialDescriptor();}, $credentialSources);/** End of optional part**/$publicKeyCredentialCreationOptions = $server->generatePublicKeyCredentialCreationOptions( $userEntity,// The user entityPublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE,// We will see this option later $excludeCredentials // Excluded authenticators// Set [] if new user);
Now send the options to the authenticator using your favorite Javascript framework, library or the example available in the Javascript page.
The Public Key Credential Creation Options object (variable $publicKeyCredentialCreationOptions) can be serialized into JSON.
The variable $publicKeyCredentialCreationOptions and $userEntity have to be stored somewhere. These are needed during the next step. Usually these values are set in the session or solutions like Redis.
Response Verification
When the authenticator sends you the computed response (i.e. the user touched the button, fingerprint reader, submitted the PIN…), you can load it and check it.
The authenticator response looks similar to the following example:
The library needs PSR-7 requests. In the example below, we use nyholm/psr7-server to get that request.
<?phpuseNyholm\Psr7\Factory\Psr17Factory;useNyholm\Psr7Server\ServerRequestCreator;$psr17Factory =newPsr17Factory();$creator =newServerRequestCreator( $psr17Factory,// ServerRequestFactory $psr17Factory,// UriFactory $psr17Factory,// UploadedFileFactory $psr17Factory // StreamFactory);$serverRequest = $creator->fromGlobals();try { $publicKeyCredentialSource = $server->loadAndCheckAttestationResponse('_The authenticator response you received…', $publicKeyCredentialCreationOptions,// The options you stored during the previous step $serverRequest // The PSR-7 request);// The user entity and the public key credential source can now be stored using their repository// The Public Key Credential Source repository must implement Webauthn\PublicKeyCredentialSourceRepository $publicKeyCredentialSourceRepository->saveCredentialSource($publicKeyCredentialSource);// If you create a new user account, you should also save the user entity $userEntityRepository->save($userEntity);} catch(\Throwable $exception) {// Something went wrong!}