|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Webauthn; |
|
13
|
|
|
|
|
14
|
|
|
use Base64Url\Base64Url; |
|
15
|
|
|
use Generator; |
|
16
|
|
|
use MongoDB\BSON\ObjectId; |
|
17
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
|
18
|
|
|
use MongoDB\BSON\UTCDateTime; |
|
19
|
|
|
use MongoDB\Database; |
|
20
|
|
|
use Webauthn\AttestedCredentialData; |
|
21
|
|
|
use Webauthn\PublicKeyCredentialSource; |
|
22
|
|
|
use Webauthn\PublicKeyCredentialSourceRepository; |
|
23
|
|
|
use Webauthn\PublicKeyCredentialUserEntity; |
|
24
|
|
|
|
|
25
|
|
|
class CredentialRepository implements PublicKeyCredentialSourceRepository |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Database. |
|
29
|
|
|
* |
|
30
|
|
|
* @var Database |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $db; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initialize. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Database $db) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->db = $db; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource |
|
46
|
|
|
{ |
|
47
|
|
|
$result = $this->db->devices->findOne([ |
|
48
|
|
|
'key.publicKeyCredentialId' => Base64Url::encode($publicKeyCredentialId), |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
if ($result === null) { |
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$result['key']['credentialPublicKey'] = Base64Url::decode($result['key']['credentialPublicKey']); |
|
56
|
|
|
|
|
57
|
|
|
return PublicKeyCredentialSource::createFromArray($result['key']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array |
|
64
|
|
|
{ |
|
65
|
|
|
return []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void |
|
72
|
|
|
{ |
|
73
|
|
|
$key = json_decode(json_encode($publicKeyCredentialSource)); |
|
74
|
|
|
$record = [ |
|
75
|
|
|
'created' => new UTCDateTime(), |
|
76
|
|
|
'owner' => new ObjectId(base64_decode($key->userHandle)), |
|
77
|
|
|
'key' => $key, |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$this->db->devices->insertOne($record); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get credentials by user. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getDescriptorsByUser(ObjectIdInterface $user): Generator |
|
87
|
|
|
{ |
|
88
|
|
|
$result = $this->db->devices->find([ |
|
89
|
|
|
'owner' => $user, |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($result as $resource) { |
|
93
|
|
|
yield PublicKeyCredentialSource::createFromArray($resource['key'])->getPublicKeyCredentialDescriptor(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Depreacted interface methods. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function has(string $credentialId): bool |
|
101
|
|
|
{ |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function get(string $credentialId): AttestedCredentialData |
|
105
|
|
|
{ |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getUserHandleFor(string $credentialId): string |
|
109
|
|
|
{ |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getCounterFor(string $credentialId): int |
|
113
|
|
|
{ |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function updateCounterFor(string $credentialId, int $newCounter): void |
|
117
|
|
|
{ |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|