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 get 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 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 these 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 availbale 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 send 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!}
The Easy Way
The easiest way to create a Webauthn Server is to use the class Webauthn\Server.
That’s it!
You can now or authenticate your users.
User Authentication
Credention Request Options
To authenticate you user, you need to send a Webauthn\PublicKeyCredentialRequestOptions object. using your $server object, call the method generatePublicKeyCredentialRequestOptions
In general, to authenticate your user you will ask them for their username first. With this username and , you will find the associated Webauthn\PublicKeyCredentialUserEntity.
And with the user entity you will get all associated Public Key Credential Source objects. The credential list is used to build the Public Key Credential Request Options.
Response Verification
When the authenticator send 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.
Now send the options to the authenticator using your favorite Javascript framework, library or the example availbale in .
<?phpuseWebauthn\PublicKeyCredentialRequestOptions;useWebauthn\PublicKeyCredentialUserEntity;// UseEntity found using the username.$userEntity = $userEntityRepository->findWebauthnUserByUsername('john.doe');// Get the list of authenticators associated to the user$credentialSources = $credentialSourceRepository->findAllForUserEntity($userEntity);// Convert the Credential Sources into Public Key Credential Descriptors$allowedCredentials =array_map(function (PublicKeyCredentialSource $credential) {return $credential->getPublicKeyCredentialDescriptor();}, $credentialSources);// We generate the set of options.$publicKeyCredentialRequestOptions = $server->generatePublicKeyCredentialRequestOptions(PublicKeyCredentialRequestOptions::USER_VERIFICATION_REQUIREMENT_PREFERRED,// Default value $allowedCredentials);
<?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->loadAndCheckAssertionResponse('_The authenticator response you received…', $publicKeyCredentialRequestOptions,// The options you stored during the previous step $userEntity,// The user entity $serverRequest // The PSR-7 request);//If everything is fine, this means the user has correctly been authenticated using the// authenticator defined in $publicKeyCredentialSource} catch(\Throwable $exception) {// Something went wrong!}