generated from Firehed/php-library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parser for native toJSON formats (#45)
This adds response parsers for the recently-added [toJSON()](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-tojson) response formats. Progress towards #41, which will be completed when there's message generators that complement the `PublicKeyCredential.parse{Creation|Request}OptionsFromJSON()` methods.
- Loading branch information
Showing
26 changed files
with
429 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Firehed\WebAuthn; | ||
|
||
use UnexpectedValueException; | ||
|
||
use function array_key_exists; | ||
use function is_array; | ||
use function is_string; | ||
|
||
/** | ||
* Parses and decodes the native `PublicKeyCredential.toJSON()` formats into the | ||
* necessary data structures for subsequent authentication procedures. When | ||
* using this decoder, the original value provided by the javascript API should | ||
* be passed in after parsing the raw JSON (using `associative: true`) | ||
* | ||
* @api | ||
*/ | ||
class JsonResponseParser implements ResponseParserInterface | ||
{ | ||
/** | ||
* Parses the JSON wire format from navigator.credentials.create | ||
* | ||
* This should arrive as the following shape: | ||
* | ||
* array{ | ||
* id: Base64UrlString, | ||
* rawId: Base64UrlString, | ||
* response: array{ | ||
* clientDataJSON: Base64UrlString, | ||
* authenticatorData: Base64UrlString, | ||
* transports: string[], | ||
* publicKey?: Base64UrlString, | ||
* publicKeyAlgorithm: int, | ||
* attestationObject: Base64UrlString, | ||
* }, | ||
* authenticatorAttachment?: string, | ||
* clientExtensionResults: array{ | ||
* }, | ||
* type: string, | ||
* } | ||
* | ||
* $data is left untyped since it performs additional checking from | ||
* untrusted user data. | ||
* | ||
* @param mixed[] $data | ||
*/ | ||
public function parseCreateResponse(array $data): Responses\AttestationInterface | ||
{ | ||
// Note: the recommended polyfill library (as of writing) excludes some | ||
// of the fields defined as required by the W3C spec. Fortunately, | ||
// they're not needed. | ||
if (!array_key_exists('type', $data) || $data['type'] !== 'public-key') { | ||
throw new Errors\ParseError('7.1.2', 'type'); | ||
} | ||
if (!array_key_exists('rawId', $data) || !is_string($data['rawId'])) { | ||
throw new Errors\ParseError('7.1.2', 'rawId'); | ||
} | ||
if (!array_key_exists('response', $data) || !is_array($data['response'])) { | ||
throw new Errors\ParseError('7.1.2', 'response'); | ||
} | ||
$response = $data['response']; | ||
if (!array_key_exists('attestationObject', $response) || !is_string($response['attestationObject'])) { | ||
throw new Errors\ParseError('7.1.2', 'response.attestationObject'); | ||
} | ||
if (!array_key_exists('clientDataJSON', $response) || !is_string($response['clientDataJSON'])) { | ||
throw new Errors\ParseError('7.1.2', 'response.clientDataJSON'); | ||
} | ||
return new CreateResponse( | ||
id: self::parse($data['rawId'], '7.1.2', 'rawId'), | ||
ao: Attestations\AttestationObject::fromCbor( | ||
self::parse($response['attestationObject'], '7.1.2', 'response.attestationObject'), | ||
), | ||
clientDataJson: self::parse($response['clientDataJSON'], '7.1.2', 'response.clientDataJSON'), | ||
); | ||
} | ||
|
||
/** | ||
* This will arrive as the following shape: | ||
* | ||
* array{ | ||
* id: Base64UrlString, | ||
* rawId: Base64UrlString, | ||
* response: array{ | ||
* clientDataJSON: Base64UrlString, | ||
* authenticatorData: Base64UrlString, | ||
* signature: Base64UrlString, | ||
* userHandle?: Base64UrlString, | ||
* attestationObject?: Base64UrlString, | ||
* }, | ||
* authenticatorAttachment?: string, | ||
* clientExtensionResults: array{}, | ||
* type: string, | ||
* } | ||
* | ||
* $data is left untyped since it performs additional checking from | ||
* untrusted user data. | ||
* | ||
* @param mixed[] $data | ||
*/ | ||
public function parseGetResponse(array $data): Responses\AssertionInterface | ||
{ | ||
if (!array_key_exists('type', $data) || $data['type'] !== 'public-key') { | ||
throw new Errors\ParseError('7.2.2', 'type'); | ||
} | ||
if (!array_key_exists('rawId', $data) || !is_string($data['rawId'])) { | ||
throw new Errors\ParseError('7.2.2', 'rawId'); | ||
} | ||
if (!array_key_exists('response', $data) || !is_array($data['response'])) { | ||
throw new Errors\ParseError('7.1.2', 'response'); | ||
} | ||
$response = $data['response']; | ||
if (!array_key_exists('authenticatorData', $response) || !is_string($response['authenticatorData'])) { | ||
throw new Errors\ParseError('7.2.2', 'response.authenticatorData'); | ||
} | ||
if (!array_key_exists('clientDataJSON', $response) || !is_string($response['clientDataJSON'])) { | ||
throw new Errors\ParseError('7.2.2', 'response.clientDataJSON'); | ||
} | ||
if (!array_key_exists('signature', $response) || !is_string($response['signature'])) { | ||
throw new Errors\ParseError('7.2.2', 'response.signature'); | ||
} | ||
|
||
return new GetResponse( | ||
credentialId: self::parse($data['rawId'], '7.2.2', 'rawId'), | ||
rawAuthenticatorData: self::parse($response['authenticatorData'], '7.2.2', 'response.authenticatorData'), | ||
clientDataJson: self::parse($response['clientDataJSON'], '7.2.2', 'response.clientDataJSON'), | ||
signature: self::parse($response['signature'], '7.2.2', 'response.signature'), | ||
userHandle: array_key_exists('userHandle', $response) && $response['userHandle'] !== '' | ||
? self::parse($response['userHandle'], '7.2.2', 'response.userHandle') | ||
: null, | ||
); | ||
} | ||
|
||
private static function parse(string $data, string $failSection, string $failMessage): BinaryString | ||
{ | ||
try { | ||
return BinaryString::fromBase64Url($data); | ||
} catch (UnexpectedValueException) { | ||
throw new Errors\ParseError($failSection, $failMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Firehed\WebAuthn; | ||
|
||
interface ResponseParserInterface | ||
{ | ||
/** | ||
* @param mixed[] $data | ||
*/ | ||
public function parseCreateResponse(array $data): Responses\AttestationInterface; | ||
|
||
/** | ||
* @param mixed[] $data | ||
*/ | ||
public function parseGetResponse(array $data): Responses\AssertionInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.