1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace U2FAuthentication\Fido2; |
15
|
|
|
|
16
|
|
|
use Base64Url\Base64Url; |
17
|
|
|
use CBOR\Decoder; |
18
|
|
|
use U2FAuthentication\Fido2\AttestationStatement\AttestationObjectLoader; |
19
|
|
|
|
20
|
|
|
class PublicKeyCredentialLoader |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Decoder |
24
|
|
|
*/ |
25
|
|
|
private $decoder; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AttestationObjectLoader |
29
|
|
|
*/ |
30
|
|
|
private $attestationObjectLoader; |
31
|
|
|
|
32
|
|
|
public function __construct(Decoder $decoder, AttestationObjectLoader $attestationObjectLoader) |
33
|
|
|
{ |
34
|
|
|
$this->decoder = $decoder; |
35
|
|
|
$this->attestationObjectLoader = $attestationObjectLoader; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function load(string $data): PublicKeyCredential |
39
|
|
|
{ |
40
|
|
|
$json = json_decode($data, true); |
41
|
|
|
if (!array_key_exists('id', $json)) { |
42
|
|
|
throw new \InvalidArgumentException(); |
43
|
|
|
} |
44
|
|
|
$id = Base64Url::decode($json['id']); |
45
|
|
|
if (!array_key_exists('rawId', $json)) { |
46
|
|
|
throw new \InvalidArgumentException(); |
47
|
|
|
} |
48
|
|
|
$rawId = Base64Url::decode($json['rawId']); |
49
|
|
|
if (!array_key_exists('type', $json)) { |
50
|
|
|
throw new \InvalidArgumentException(); |
51
|
|
|
} |
52
|
|
|
if (!hash_equals($id, $rawId)) { |
53
|
|
|
throw new \InvalidArgumentException(); |
54
|
|
|
} |
55
|
|
|
if (!array_key_exists('response', $json)) { |
56
|
|
|
throw new \InvalidArgumentException(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$publicKeyCredential = new PublicKeyCredential( |
60
|
|
|
$json['id'], |
61
|
|
|
$json['type'], |
62
|
|
|
$rawId, |
63
|
|
|
$this->createAuthenticatorResponse($json['response']) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $publicKeyCredential; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function createAuthenticatorResponse(array $response): AuthenticatorAttestationResponse |
70
|
|
|
{ |
71
|
|
|
if (!array_key_exists('clientDataJSON', $response)) { |
72
|
|
|
throw new \InvalidArgumentException(); |
73
|
|
|
} |
74
|
|
|
if (!array_key_exists('attestationObject', $response)) { |
75
|
|
|
throw new \InvalidArgumentException(); |
76
|
|
|
} |
77
|
|
|
$attestationObject = $this->attestationObjectLoader->load($response['attestationObject']); |
78
|
|
|
|
79
|
|
|
return new AuthenticatorAttestationResponse( |
80
|
|
|
CollectedClientData::createFormJson($response['clientDataJSON']), |
81
|
|
|
$attestationObject |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|