1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\WebAuthn\Tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\WebAuthn\CredentialRepository; |
8
|
|
|
use Webauthn\AttestedCredentialData; |
9
|
|
|
|
10
|
|
|
class CredentialRepositoryTest extends SapphireTest |
11
|
|
|
{ |
12
|
|
|
public function testHas() |
13
|
|
|
{ |
14
|
|
|
$repo = CredentialRepository::fromArray($this->createTestCredentials(['foobar']), '1'); |
15
|
|
|
|
16
|
|
|
$this->assertTrue($repo->has('foobar')); |
17
|
|
|
$this->assertFalse($repo->has('barbaz')); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @expectedException InvalidArgumentException |
22
|
|
|
* @expectedExceptionMessage Given credential ID does not match any stored credentials |
23
|
|
|
*/ |
24
|
|
|
public function testGetThrowsExceptionOnInvalidCredentialId() |
25
|
|
|
{ |
26
|
|
|
$repo = new CredentialRepository('1'); |
27
|
|
|
$repo->get('non-existent'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetReturnsAttestedCredentialData() |
31
|
|
|
{ |
32
|
|
|
$repo = CredentialRepository::fromArray($this->createTestCredentials(['foobar']), '1'); |
33
|
|
|
|
34
|
|
|
$this->assertInstanceOf(AttestedCredentialData::class, $repo->get('foobar')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetUserHandleFor() |
38
|
|
|
{ |
39
|
|
|
$repo = CredentialRepository::fromArray($this->createTestCredentials(['foobar']), '123'); |
40
|
|
|
|
41
|
|
|
$this->assertSame('123', $repo->getUserHandleFor('foobar')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetCounterFor() |
45
|
|
|
{ |
46
|
|
|
$repo = CredentialRepository::fromArray($this->createTestCredentials(['foobar'], 5), '1'); |
47
|
|
|
|
48
|
|
|
$this->assertSame(5, $repo->getCounterFor('foobar')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testUpdateCounterFor() |
52
|
|
|
{ |
53
|
|
|
$repo = CredentialRepository::fromArray($this->createTestCredentials(['foobar']), '1'); |
54
|
|
|
|
55
|
|
|
$repo->updateCounterFor('foobar', 10); |
56
|
|
|
$this->assertSame(10, $repo->getCounterFor('foobar')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function createTestCredentials(array $names, $counterValue = 1) |
60
|
|
|
{ |
61
|
|
|
$creds = []; |
62
|
|
|
|
63
|
|
|
foreach ($names as $id) { |
64
|
|
|
// phpcs:disable |
65
|
|
|
$creds[base64_encode($id)] = [ |
66
|
|
|
'source' => [ |
67
|
|
|
'publicKeyCredentialId' => $id, |
68
|
|
|
'type' => 'public-key', |
69
|
|
|
'transports' => |
70
|
|
|
array ( |
71
|
|
|
), |
72
|
|
|
'attestationType' => 'none', |
73
|
|
|
'trustPath' => |
74
|
|
|
array ( |
75
|
|
|
'type' => 'empty', |
76
|
|
|
), |
77
|
|
|
'aaguid' => 'AAAAAAAAAAAAAAAAAAAAAA', |
78
|
|
|
'credentialPublicKey' => 'pQECAyYgASFYII3gDdvOBje5JfjNO0VhxE2RrV5XoKqWmCZAmR0f9nFaIlggZOUvkovGH9cfeyfXEpJAVOzR1d-rVRZJvwWJf444aLo', |
79
|
|
|
'userHandle' => 'MQ', |
80
|
|
|
'counter' => 268, |
81
|
|
|
], |
82
|
|
|
'counter' => $counterValue, |
83
|
|
|
]; |
84
|
|
|
// phpcs:enable |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $creds; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|