A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 10
wmc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\webauthn\Controller;
6
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\Auth\State;
10
use SimpleSAML\Configuration;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module\webauthn\Controller;
13
use SimpleSAML\Session;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Set of tests for the controllers in the "webauthn" module.
18
 *
19
 * @package SimpleSAML\Test
20
 */
21
class ManageTokenTest extends TestCase
22
{
23
    /** @var \SimpleSAML\Configuration */
24
    protected Configuration $config;
25
26
    /** @var \SimpleSAML\Configuration */
27
    protected $module_config;
28
29
    /** @var \SimpleSAML\Logger */
30
    protected Logger $logger;
31
32
    /** @var \SimpleSAML\Session */
33
    protected Session $session;
34
35
36
    /**
37
     * Set up for each test.
38
     */
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->config = Configuration::loadFromArray(
44
            [
45
                'module.enable' => ['webauthn' => true],
46
                'secretsalt' => 'abc123',
47
                'enable.saml20-idp' => true,
48
            ],
49
            '[ARRAY]',
50
            'simplesaml',
51
        );
52
53
        $this->module_config = Configuration::loadFromArray(
54
            [
55
                'registration' => ['use_inflow_registration' => true],
56
            ],
57
        );
58
59
        Configuration::setPreLoadedConfig($this->config, 'config.php');
60
        Configuration::setPreLoadedConfig($this->module_config, 'module_webauthn.php');
61
62
63
        $this->session = Session::getSessionFromRequest();
64
65
        $this->logger = new class () extends Logger {
66
            public static function info(string $string): void
67
            {
68
                // do nothing
69
            }
70
        };
71
    }
72
73
74
    /**
75
     */
76
    public function testManageTokenWithSubmitNeverMind(): void
77
    {
78
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
79
        $request = Request::create(
80
            '/managetoken?StateId=someStateId',
81
            'POST',
82
            ['submit' => 'NEVERMIND'],
83
        );
84
85
86
        $c = new Controller\ManageToken($this->config, $this->session);
87
        $c->setLogger($this->logger);
88
        $c->setAuthState(new class () extends State {
89
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
90
            {
91
                return [
92
                    'FIDO2AuthSuccessful' => true,
93
                    'FIDO2PasswordlessAuthMode' => false,
94
                ];
95
            }
96
        });
97
98
        $response = $c->main($request);
99
100
        $this->assertTrue($response->isSuccessful());
101
    }
102
103
104
    /**
105
    public function testManageTokenWithSubmitDelete(): void
106
    {
107
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
108
        $_SERVER['REQUEST_METHOD'] = 'POST';
109
        $request = Request::create(
110
            '/managetoken?StateId=someStateId',
111
            'POST',
112
            ['submit' => 'DELETE'],
113
        );
114
115
116
        $c = new Controller\ManageToken($this->config, $this->session);
117
        $c->setLogger($this->logger);
118
        $c->setAuthState(new class () extends State {
119
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
120
            {
121
                return [
122
                    'FIDO2AuthSuccessful' => true,
123
                ];
124
            }
125
        });
126
127
        $response = $c->main($request);
128
129
        $this->assertTrue($response->isSuccessful());
130
    }
131
     */
132
133
134
    /**
135
     */
136
    public function testManageTokenWithoutSubmitThrowsException(): void
137
    {
138
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
139
        $_SERVER['REQUEST_METHOD'] = 'POST';
140
        $request = Request::create(
141
            '/managetoken?StateId=someStateId',
142
            'POST',
143
            ['submit' => 'submit'],
144
        );
145
146
147
        $c = new Controller\ManageToken($this->config, $this->session);
148
        $c->setLogger($this->logger);
149
        $c->setAuthState(new class () extends State {
150
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
151
            {
152
                return [
153
                    'FIDO2AuthSuccessful' => true,
154
                    'FIDO2PasswordlessAuthMode' => false,
155
                ];
156
            }
157
        });
158
159
        $this->expectException(Exception::class);
160
        $this->expectExceptionMessage('Unknown submit button state.');
161
162
        $c->main($request);
163
    }
164
165
166
    /**
167
     */
168
    public function testManageTokenWithoutAuthenticationThrowsException(): void
169
    {
170
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
171
        $_SERVER['REQUEST_METHOD'] = 'POST';
172
        $request = Request::create(
173
            '/managetoken?StateId=someStateId',
174
            'POST',
175
            ['submit' => 'submit'],
176
        );
177
178
179
        $c = new Controller\ManageToken($this->config, $this->session);
180
        $c->setLogger($this->logger);
181
        $c->setAuthState(new class () extends State {
182
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
183
            {
184
                return [
185
                    'FIDO2AuthSuccessful' => false,
186
                    'FIDO2Tokens' => [0 => "foo"],
187
                    'FIDO2WantsRegister' => false,
188
                    'UseInflowRegistration' => false,
189
                    'FIDO2PasswordlessAuthMode' => false,
190
                ];
191
            }
192
        });
193
194
        $this->expectException(Exception::class);
195
        $this->expectExceptionMessage('Attempt to access the token management page unauthenticated.');
196
197
        $c->main($request);
198
    }
199
}
200