Skip to content

Commit

Permalink
Refactor authenticator response identification logic
Browse files Browse the repository at this point in the history
The logic for identifying the type of Authenticator Response has been simplified. Instead of checking for multiple array keys in a data object, we now simply check for the presence of either 'attestationObject' or 'signature'. This refactoring leads to cleaner and more maintainable code.
  • Loading branch information
Spomky committed Apr 8, 2024
1 parent 670d9e9 commit c14a23d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ final class AuthenticatorResponseDenormalizer implements DenormalizerInterface,
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
$realType = match (true) {
array_key_exists('attestationObject', $data) && ! array_key_exists(
'signature',
$data
) => AuthenticatorAttestationResponse::class,
array_key_exists('authenticatorData', $data) && array_key_exists(
'signature',
$data
) => AuthenticatorAssertionResponse::class,
array_key_exists('attestationObject', $data) => AuthenticatorAttestationResponse::class,
array_key_exists('signature', $data) => AuthenticatorAssertionResponse::class,
default => throw InvalidDataException::create($data, 'Unable to create the response object'),
};

Expand Down
6 changes: 3 additions & 3 deletions src/webauthn/src/PublicKeyCredentialLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ private function createResponse(array $response): AuthenticatorResponse
return $this->serializer->deserialize($response, AuthenticatorResponse::class, 'json');
}
switch (true) {
case ! array_key_exists('authenticatorData', $response) && ! array_key_exists('signature', $response):
case array_key_exists('attestationObject', $response):
$attestationObject = $this->attestationObjectLoader->load($response['attestationObject']);

return AuthenticatorAttestationResponse::create(CollectedClientData::createFormJson(
$response['clientDataJSON']
), $attestationObject, $transports);
case array_key_exists('authenticatorData', $response) && array_key_exists('signature', $response):
case array_key_exists('signature', $response):
$authDataLoader = AuthenticatorDataLoader::create();
$authData = Base64UrlSafe::decodeNoPadding($response['authenticatorData']);
$authData = Base64UrlSafe::decodeNoPadding($response['authenticatorData'] ?? '');
$authenticatorData = $authDataLoader->load($authData);

try {
Expand Down

0 comments on commit c14a23d

Please sign in to comment.