/
CredentialRepository.php
240 lines (205 loc) · 6.73 KB
/
CredentialRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace SilverStripe\WebAuthn;
use InvalidArgumentException;
use Serializable;
use Webauthn\AttestedCredentialData;
use Webauthn\PublicKeyCredentialSource;
use Webauthn\PublicKeyCredentialSourceRepository;
use Webauthn\PublicKeyCredentialUserEntity;
use SilverStripe\Dev\Deprecation;
/**
* Implements the required interface from the WebAuthn library - but it does not implement the repository pattern in the
* usual way. This is expected to be stored on a DataObject for persistence. Use the
* @see CredentialRepository::hasChanged() API for determining whether a DataObject this is stored upon should be
* persisted.
*/
class CredentialRepository implements PublicKeyCredentialSourceRepository, Serializable
{
/**
* @var string
*/
private $memberID;
/**
* @var array
*/
private $credentials = [];
/**
* @var bool
*/
private $hasChanged = false;
/**
* @param string $memberID
*/
public function __construct(string $memberID)
{
$this->memberID = $memberID;
}
public function has(string $credentialId): bool
{
return $this->findOneByCredentialId($credentialId) !== null;
}
public function get(string $credentialId): AttestedCredentialData
{
$this->assertCredentialID($credentialId);
return $this->findOneByCredentialId($credentialId)->getAttestedCredentialData();
}
public function getUserHandleFor(string $credentialId): string
{
$this->assertCredentialID($credentialId);
return $this->memberID;
}
public function getCounterFor(string $credentialId): int
{
$this->assertCredentialID($credentialId);
return (int) $this->credentials[$this->getCredentialIDRef($credentialId)]['counter'];
}
public function updateCounterFor(string $credentialId, int $newCounter): void
{
$this->assertCredentialID($credentialId);
$this->credentials[$this->getCredentialIDRef($credentialId)]['counter'] = $newCounter;
$this->hasChanged = true;
}
/**
* Assert that the given credential ID matches a stored credential
*
* @param string $credentialId
*/
protected function assertCredentialID(string $credentialId): void
{
if (!$this->has($credentialId)) {
throw new InvalidArgumentException('Given credential ID does not match any stored credentials');
}
}
public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
{
$ref = $this->getCredentialIDRef($publicKeyCredentialId);
if (!isset($this->credentials[$ref])) {
return null;
}
return $this->credentials[$ref]['source'];
}
/**
* @return PublicKeyCredentialSource[]
*/
public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
{
// Only return credentials if the user entity shares the same ID.
if ($publicKeyCredentialUserEntity->getId() !== $this->memberID) {
return [];
}
return array_map(function ($credentialComposite) {
return $credentialComposite['source'];
}, $this->credentials ?? []);
}
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
{
$ref = $this->getCredentialIDRef($publicKeyCredentialSource->getPublicKeyCredentialId());
if (!isset($this->credentials[$ref])) {
$this->credentials[$ref] = [
'counter' => 0,
];
}
$this->credentials[$ref]['source'] = $publicKeyCredentialSource;
$this->hasChanged = true;
}
/**
* Empty the store deleting all stored credentials
*/
public function reset(): void
{
$this->credentials = [];
}
/**
* Indicates the repository has changed and should be persisted (as this doesn't follow the actual repository
* pattern and is expected to be stored on a dataobject for persistence)
*
* @return bool
*/
public function hasChanged(): bool
{
return $this->hasChanged;
}
/**
* Set the credentials in bulk (for internal use) ensuring that credential objects are initialised correctly
*
* @param array $credentials
*/
protected function setCredentials(array $credentials): void
{
$this->credentials = array_map(function ($data) {
if (is_array($data['source'])) {
$data['source'] = PublicKeyCredentialSource::createFromArray($data['source']);
}
return $data;
}, $credentials ?? []);
}
/**
* Create a reference to be used as a key for the credentials in the array
*
* @param string $credentialID
* @return string
*/
protected function getCredentialIDRef(string $credentialID): string
{
return base64_encode($credentialID ?? '');
}
/**
* Provide the credentials stored in this repository as an array
*
* @return array
*/
public function toArray(): array
{
return $this->credentials;
}
/**
* Create an instance of a repository from the given credentials
*
* @param array $credentials
* @param string $memberID
* @return CredentialRepository
*/
public static function fromArray(array $credentials, string $memberID): self
{
$new = new static($memberID);
$new->setCredentials($credentials);
return $new;
}
public function __serialize(): array
{
return [
'memberID' => $this->memberID,
'credentials' => $this->toArray()
];
}
public function __unserialize(array $data): void
{
$this->memberID = $data['memberID'];
$this->setCredentials($data['credentials']);
}
/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated 4.5.0 Use __serialize() instead
*/
public function serialize()
{
Deprecation::notice('4.5.0', 'Use __serialize() instead');
return json_encode($this->__serialize());
}
/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated 4.5.0 Use __unserialize() instead
*/
public function unserialize($serialized)
{
Deprecation::notice('4.5.0', 'Use __unserialize() instead');
$this->__unserialize(json_decode($serialized ?? '', true));
}
}