Passed
Push — master ( c219a4...e7152d )
by Garion
02:24
created

testAuthenticatorSelectionCriteriaRequiresCrossPlatformAttachmentByDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
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\AuthenticatorSelectionCriteria;
10
use Webauthn\PublicKeyCredentialCreationOptions;
11
12
class RegisterHandlerTest extends SapphireTest
13
{
14
    protected $usesDatabase = true;
15
16
    /**
17
     * @var RegisterHandler
18
     */
19
    protected $handler;
20
21
    /**
22
     * @var Member
23
     */
24
    protected $member;
25
26
    /**
27
     * @var array
28
     */
29
    protected $originalServer;
30
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        $this->handler = new RegisterHandler();
36
37
        $memberID = $this->logInWithPermission();
38
        /** @var Member $member */
39
        $this->member = Member::get()->byID($memberID);
40
41
        $this->originalServer = $_SERVER;
42
43
        // Set default configuration settings
44
        RegisterHandler::config()->set(
45
            'authenticator_attachment',
46
            AuthenticatorSelectionCriteria::AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM
47
        );
48
    }
49
50
    protected function tearDown()
51
    {
52
        $_SERVER = $this->originalServer;
53
54
        parent::tearDown();
55
    }
56
57
    /**
58
     * @param string $baseUrl
59
     * @param string $expected
60
     * @dataProvider hostProvider
61
     */
62
    public function testRelyingPartyEntityDomainIncludesSilverStripeDomain(string $baseUrl, string $expected)
63
    {
64
        $_SERVER['HTTP_HOST'] = $baseUrl;
65
66
        $store = new SessionStore($this->member);
67
        $result = $this->handler->start($store);
68
        $this->assertArrayHasKey('keyData', $result);
69
70
        /** @var PublicKeyCredentialCreationOptions $options */
71
        $options = $result['keyData'];
72
        $this->assertInstanceOf(PublicKeyCredentialCreationOptions::class, $options);
73
74
        $relyingPartyEntity = $options->getRp();
75
        $this->assertSame(
76
            $expected,
77
            $relyingPartyEntity->getId(),
78
            'Relying party entity should identify the current SilverStripe domain'
79
        );
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function hostProvider(): array
86
    {
87
        return [
88
            'domain only' => ['http://example.com', 'example.com'],
89
            'domain with port' => ['https://example.com:8080', 'example.com'],
90
            'subdomain' => ['https://www.example.com', 'www.example.com'],
91
            'subdomain with port' => ['http://my.example.com:8887', 'my.example.com'],
92
            'subfolder' => ['https://example.com/mysite', 'example.com'],
93
            'subfolder with port' => ['http://example.com:8080/mysite', 'example.com'],
94
            'subdomain with subfolder' => ['http://my.example.com/mysite', 'my.example.com'],
95
            'subdomain with port and subfolder' => ['https://my.example.com:8080/mysite', 'my.example.com'],
96
            'credentials with domain and trailing slash' => ['http://foo:[email protected]/', 'example.com'],
97
        ];
98
    }
99
100
    public function testAuthenticatorSelectionCriteriaRequiresCrossPlatformAttachmentByDefault()
101
    {
102
        $store = new SessionStore($this->member);
103
        $result = $this->handler->start($store);
104
        $this->assertArrayHasKey('keyData', $result);
105
106
        /** @var PublicKeyCredentialCreationOptions $options */
107
        $options = $result['keyData'];
108
        $this->assertInstanceOf(PublicKeyCredentialCreationOptions::class, $options);
109
110
        $authenticatorSelection = $options->getAuthenticatorSelection();
111
        $this->assertSame(
112
            AuthenticatorSelectionCriteria::AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM,
113
            $authenticatorSelection->getAuthenticatorAttachment()
114
        );
115
    }
116
}
117