1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\WebAuthn\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\MFA\Store\SessionStore; |
7
|
|
|
use SilverStripe\Security\Member; |
8
|
|
|
use SilverStripe\WebAuthn\RegisterHandler; |
9
|
|
|
use Webauthn\PublicKeyCredentialCreationOptions; |
10
|
|
|
|
11
|
|
|
class RegisterHandlerTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
protected $usesDatabase = true; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var RegisterHandler |
17
|
|
|
*/ |
18
|
|
|
protected $handler; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Member |
22
|
|
|
*/ |
23
|
|
|
protected $member; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $originalServer; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
|
34
|
|
|
$this->handler = new RegisterHandler(); |
35
|
|
|
|
36
|
|
|
$memberID = $this->logInWithPermission(); |
37
|
|
|
/** @var Member $member */ |
38
|
|
|
$this->member = Member::get()->byID($memberID); |
39
|
|
|
|
40
|
|
|
$this->originalServer = $_SERVER; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function tearDown() |
44
|
|
|
{ |
45
|
|
|
$_SERVER = $this->originalServer; |
46
|
|
|
|
47
|
|
|
parent::tearDown(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $baseUrl |
52
|
|
|
* @param string $expected |
53
|
|
|
* @dataProvider hostProvider |
54
|
|
|
*/ |
55
|
|
|
public function testRelyingPartyEntityDomainIncludesSilverStripeDomain(string $baseUrl, string $expected) |
56
|
|
|
{ |
57
|
|
|
$_SERVER['HTTP_HOST'] = $baseUrl; |
58
|
|
|
|
59
|
|
|
$store = new SessionStore($this->member); |
60
|
|
|
$result = $this->handler->start($store); |
61
|
|
|
|
62
|
|
|
$this->assertArrayHasKey('keyData', $result); |
63
|
|
|
|
64
|
|
|
/** @var PublicKeyCredentialCreationOptions $options */ |
65
|
|
|
$options = $result['keyData']; |
66
|
|
|
$this->assertInstanceOf(PublicKeyCredentialCreationOptions::class, $options); |
67
|
|
|
|
68
|
|
|
$relyingPartyEntity = $options->getRp(); |
69
|
|
|
$this->assertSame( |
70
|
|
|
$expected, |
71
|
|
|
$relyingPartyEntity->getId(), |
72
|
|
|
'Relying party entity should identify the current SilverStripe domain' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function hostProvider(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'domain only' => ['http://example.com', 'example.com'], |
83
|
|
|
'domain with port' => ['https://example.com:8080', 'example.com'], |
84
|
|
|
'subdomain' => ['https://www.example.com', 'www.example.com'], |
85
|
|
|
'subdomain with port' => ['http://my.example.com:8887', 'my.example.com'], |
86
|
|
|
'subfolder' => ['https://example.com/mysite', 'example.com'], |
87
|
|
|
'subfolder with port' => ['http://example.com:8080/mysite', 'example.com'], |
88
|
|
|
'subdomain with subfolder' => ['http://my.example.com/mysite', 'my.example.com'], |
89
|
|
|
'subdomain with port and subfolder' => ['https://my.example.com:8080/mysite', 'my.example.com'], |
90
|
|
|
'credentials with domain and trailing slash' => ['http://foo:[email protected]/', 'example.com'], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|