/
AuthenticationTest.php
313 lines (240 loc) · 10.6 KB
/
AuthenticationTest.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace MadWizard\WebAuthn\Tests\Server;
use MadWizard\WebAuthn\Builder\ServerBuilder;
use MadWizard\WebAuthn\Config\RelyingParty;
use MadWizard\WebAuthn\Credential\CredentialId;
use MadWizard\WebAuthn\Credential\CredentialStoreInterface;
use MadWizard\WebAuthn\Credential\UserCredentialInterface;
use MadWizard\WebAuthn\Credential\UserHandle;
use MadWizard\WebAuthn\Crypto\CoseAlgorithm;
use MadWizard\WebAuthn\Crypto\Ec2Key;
use MadWizard\WebAuthn\Exception\ParseException;
use MadWizard\WebAuthn\Exception\UnsupportedException;
use MadWizard\WebAuthn\Exception\VerificationException;
use MadWizard\WebAuthn\Format\Base64UrlEncoding;
use MadWizard\WebAuthn\Format\ByteBuffer;
use MadWizard\WebAuthn\Json\JsonConverter;
use MadWizard\WebAuthn\Server\Authentication\AuthenticationOptions;
use MadWizard\WebAuthn\Server\WebAuthnServer;
use MadWizard\WebAuthn\Tests\Helper\AssertionDataHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function hex2bin;
class AuthenticationTest extends TestCase
{
/**
* @var CredentialStoreInterface|MockObject
*/
private $store;
/**
* @var WebAuthnServer|MockObject
*/
private $server;
public function testStartAuthentication()
{
$options = AuthenticationOptions::createForAnyUser();
$userCredential = $this->createCredential();
$options->addAllowCredential($userCredential->getCredentialId());
$request = $this->server->startAuthentication($options);
$clientOptions = $request->getClientOptions();
$allowCredentials = $clientOptions->getAllowCredentials();
self::assertCount(1, $allowCredentials);
self::assertSame($userCredential->getCredentialId()->toString(), $allowCredentials[0]->getId()->getBase64Url());
self::assertNull($allowCredentials[0]->getTransports());
self::assertNull($clientOptions->getRpId());
self::assertSame(
[
'challenge' => $request->getContext()->getChallenge()->getBase64Url(),
'allowCredentials' => [
[
'type' => 'public-key',
'id' => $userCredential->getCredentialId()->toString(),
],
],
],
$clientOptions->getJsonData()
);
}
public function runAuth(AssertionDataHelper $helper): UserCredentialInterface
{
$credential = $this->createCredential();
$this->store
->expects($this->any())
->method('findCredential')
->with($credential->getCredentialId())
->willReturn($credential);
return $this->server->finishAuthentication(JsonConverter::decodeAssertionString($helper->getCredentialJson()), $helper->getContext())->getUserCredential();
}
public function testValidAssertion()
{
$helper = new AssertionDataHelper();
$userCred = $this->runAuth($helper);
self::assertSame(AssertionDataHelper::DEFAULT_CREDENTIAL_ID, $userCred->getCredentialId()->toString());
/** @var Ec2Key $pubKey */
$pubKey = $userCred->getPublicKey();
self::assertSame(AssertionDataHelper::KEY_A_X, $pubKey->getX()->getHex());
self::assertSame(AssertionDataHelper::KEY_A_Y, $pubKey->getY()->getHex());
}
public function testAllowedCredentials()
{
// SPEC 7.2.1 If the allowCredentials option was given when this authentication ceremony was initiated,
// verify that credential.id identifies one of the public key credentials that were listed in allowCredentials.
$helper = new AssertionDataHelper();
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~not in list of allowed credentials~i');
$helper->setContextOptions(['allowedCredentials' => [Base64UrlEncoding::encode('different credential id')]]);
$this->runAuth($helper);
}
public function testUserHandleOwner()
{
// SPEC 7.2.2 If credential.response.userHandle is present, verify that the user identified by this value is
// the owner of the public key credential identified by credential.id.
$helper = new AssertionDataHelper();
$userHandle = Base64UrlEncoding::encode(hex2bin('123456'));
$helper->setClientOptions(['userHandle' => $userHandle]);
$userCred = $this->runAuth($helper);
self::assertSame('123456', $userCred->getUserHandle()->toHex());
}
public function testUserHandleNotOwner()
{
// SPEC 7.2.2 If credential.response.userHandle is present, verify that the user identified by this value is
// the owner of the public key credential identified by credential.id.
$helper = new AssertionDataHelper();
$userHandle = Base64UrlEncoding::encode(hex2bin('667788'));
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~does not belong to the user~i');
$helper->setClientOptions(['userHandle' => $userHandle]);
$this->runAuth($helper);
}
public function testInvalidClientDataJson()
{
// SPEC 7.2.5 JSON parse
$helper = new AssertionDataHelper();
$helper->setClientOptions(['makeWrongClientJson' => true]);
$this->expectException(ParseException::class);
$this->expectExceptionMessageMatches('~Unparseable client data JSON~i');
$this->runAuth($helper);
}
public function testIncompleteClientDataJson()
{
$helper = new AssertionDataHelper();
$helper->setClientOptions(['removeChallenge' => true]);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~missing data .+ clientData~i');
$this->runAuth($helper);
}
public function testBOMClientDataJson()
{
// SPEC 7.2.5 JSON parse
$helper = new AssertionDataHelper();
$helper->setClientOptions(['includeJsonBom' => true]);
$this->runAuth($helper);
// Check no exceptions
self::assertTrue(true);
}
public function testCredentialType()
{
// SPEC 7.2.6 Verify that the value of C.type is the string webauthn.get.
$helper = new AssertionDataHelper();
$helper->setClientOptions(['type' => 'webauthn.create']);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~webauthn\.get~i');
$this->runAuth($helper);
}
public function testSameChallenge()
{
// SPEC 7.2.8 Verify that the value of C.challenge matches the challenge that was sent to the authenticator
// in the PublicKeyCredentialRequestOptions passed to the get() call.
$helper = new AssertionDataHelper();
$helper->setClientOptions(['challenge' => Base64UrlEncoding::encode('differentchallenge123456789')]);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~challenge.+does not match~i');
$this->runAuth($helper);
}
public function testSameOrigin()
{
// SPEC 7.2.9 Verify that the value of C.origin matches the Relying Party's origin.
$helper = new AssertionDataHelper();
$helper->setClientOptions(['origin' => 'http://example.com']);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~origin.+does not match~i');
$this->runAuth($helper);
}
public function testRpIdMatches()
{
// SPEC 7.2.11 Verify that the rpIdHash in aData is the SHA-256 hash of the RP ID expected by the Relying Party.
$helper = new AssertionDataHelper();
$helper->setClientOptions(['rpId' => 'not-localhost']);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~rpIdHash was not correct~i');
$this->runAuth($helper);
}
public function testTokenBinding()
{
$helper = new AssertionDataHelper();
$helper->setClientOptions(['tokenBinding' => ['status' => 'present', 'id' => '123456']]);
$this->expectException(UnsupportedException::class);
$this->expectExceptionMessageMatches('~token binding is not yet supported~i');
$this->runAuth($helper);
}
public function testInvalidTokenBindingData()
{
$helper = new AssertionDataHelper();
$helper->setClientOptions(['tokenBinding' => [true, false]]);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~unexpected .+ tokenBinding~i');
$this->runAuth($helper);
}
public function testInvalidTokenBindingStatus()
{
$helper = new AssertionDataHelper();
$helper->setClientOptions(['tokenBinding' => ['status' => 'invalidstatus']]);
$this->expectException(VerificationException::class);
$this->expectExceptionMessageMatches('~status.+invalid~i');
$this->runAuth($helper);
}
public function testNeutralTokenBindingStatus()
{
$helper = new AssertionDataHelper();
$helper->setClientOptions(['tokenBinding' => ['status' => 'supported']]);
$this->runAuth($helper);
// Check no exceptions
self::assertTrue(true);
}
protected function setUp(): void
{
$rp = new RelyingParty('Example', 'https://example.com');
$this->store = $this->createMock(CredentialStoreInterface::class);
$this->store->expects($this->any())
->method('getSignatureCounter')
->willReturn(8);
$this->server = (new ServerBuilder())
->setRelyingParty($rp)
->setCredentialStore($this->store)
->build();
}
private function createCredential(): UserCredentialInterface
{
/**
* @var $cred UserCredentialInterface|MockObject
*/
$cred = $this->createMock(UserCredentialInterface::class);
$cred->expects($this->any())
->method('getCredentialId')
->willReturn(CredentialId::fromString(AssertionDataHelper::DEFAULT_CREDENTIAL_ID));
$cred->expects($this->any())
->method('getPublicKey')
->willReturn(
new Ec2Key(
ByteBuffer::fromHex(AssertionDataHelper::KEY_A_X),
ByteBuffer::fromHex(AssertionDataHelper::KEY_A_Y),
Ec2Key::CURVE_P256,
CoseAlgorithm::ES256
)
);
$cred->expects($this->any())
->method('getUserHandle')
->willReturn(UserHandle::fromHex('123456'));
return $cred;
}
}