1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\WebAuthn\Tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\MFA\Model\RegisteredMethod; |
8
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
use SilverStripe\WebAuthn\CredentialRepository; |
10
|
|
|
use Webauthn\AttestedCredentialData; |
11
|
|
|
|
12
|
|
|
class CredentialRepositoryTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
protected $usesDatabase = true; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Member |
18
|
|
|
*/ |
19
|
|
|
protected $member; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var RegisteredMethod |
23
|
|
|
*/ |
24
|
|
|
protected $registeredMethod; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CredentialRepository |
28
|
|
|
*/ |
29
|
|
|
protected $repository; |
30
|
|
|
|
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
$this->member = new Member(); |
36
|
|
|
$this->registeredMethod = new RegisteredMethod(); |
37
|
|
|
$this->repository = new CredentialRepository($this->member, $this->registeredMethod); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testHas() |
41
|
|
|
{ |
42
|
|
|
$this->registeredMethod->Data = json_encode([ |
43
|
|
|
'data' => ['credentialId' => base64_encode('foobar')], |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$this->assertTrue($this->repository->has('foobar')); |
47
|
|
|
$this->assertFalse($this->repository->has('barbaz')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException InvalidArgumentException |
52
|
|
|
* @expectedExceptionMessage Given credential ID does not match any database record |
53
|
|
|
*/ |
54
|
|
|
public function testGetThrowsExceptionOnInvalidCredentialId() |
55
|
|
|
{ |
56
|
|
|
$this->repository->get('non-existent'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetReturnsAttestedCredentialData() |
60
|
|
|
{ |
61
|
|
|
$this->registeredMethod->Data = json_encode([ |
62
|
|
|
'data' => [ |
63
|
|
|
'credentialId' => base64_encode('foobar'), |
64
|
|
|
'aaguid' => base64_encode('1234-5678-9012-3456'), |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(AttestedCredentialData::class, $this->repository->get('foobar')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetUserHandleFor() |
72
|
|
|
{ |
73
|
|
|
$this->registeredMethod->Data = json_encode([ |
74
|
|
|
'data' => ['credentialId' => base64_encode('foobar')], |
75
|
|
|
]); |
76
|
|
|
$this->member->ID = 123; |
77
|
|
|
|
78
|
|
|
$this->assertSame('123', $this->repository->getUserHandleFor('foobar')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetCounterFor() |
82
|
|
|
{ |
83
|
|
|
$this->registeredMethod->Data = json_encode([ |
84
|
|
|
'data' => ['credentialId' => base64_encode('foobar')], |
85
|
|
|
'counter' => 5, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$this->assertSame(5, $this->repository->getCounterFor('foobar')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testUpdateCounterFor() |
92
|
|
|
{ |
93
|
|
|
$this->registeredMethod->Data = json_encode([ |
94
|
|
|
'data' => ['credentialId' => base64_encode('foobar')], |
95
|
|
|
'counter' => 5, |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$this->repository->updateCounterFor('foobar', 10); |
99
|
|
|
$this->assertSame(10, $this->repository->getCounterFor('foobar')); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|