Passed
Push — master ( 329aee...0397a8 )
by Tim
02:54
created

AuthProcessTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 115
rs 10
c 1
b 1
f 0
eloc 57
wmc 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$1 ➔ loadState() 0 11 1
setUp() 0 19 ?
A hp$1 ➔ testAuthProcessWithoutProperTokenRaisesException() 0 34 1
A hp$0 ➔ setUp() 0 19 1
A hp$0 ➔ info() 0 2 1
testAuthProcessWithoutProperTokenRaisesException() 0 34 ?
A hp$2 ➔ testPasswordlessAuthProcessWithoutProperTokenRaisesException() 0 34 1
A hp$2 ➔ loadState() 0 11 1
testPasswordlessAuthProcessWithoutProperTokenRaisesException() 0 34 ?
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 AuthProcessTest extends TestCase
22
{
23
    /** @var \SimpleSAML\Configuration */
24
    protected Configuration $config;
25
26
    /** @var \SimpleSAML\Logger */
27
    protected Logger $logger;
28
29
    /** @var \SimpleSAML\Session */
30
    protected Session $session;
31
32
33
    /**
34
     * Set up for each test.
35
     */
36
    protected function setUp(): void
37
    {
38
        parent::setUp();
39
40
        $this->config = Configuration::loadFromArray(
41
            [
42
                'module.enable' => ['webauthn' => true],
43
                'secretsalt' => 'abc123',
44
                'enable.saml20-idp' => true,
45
            ],
46
            '[ARRAY]',
47
            'simplesaml',
48
        );
49
50
        $this->session = Session::getSessionFromRequest();
51
52
        $this->logger = new class () extends Logger {
53
            public static function info(string $string): void
54
            {
55
                // do nothing
56
            }
57
        };
58
    }
59
60
61
    /**
62
     */
63
    public function testAuthProcessWithoutProperTokenRaisesException(): void
64
    {
65
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/authprocess?StateId=someStateId';
66
        $request = Request::create(
67
            '/authprocess?StateId=someStateId',
68
            'POST',
69
            ['response_id' => 'abc123'],
70
        );
71
72
73
        $c = new Controller\AuthProcess($this->config, $this->session);
74
        $c->setLogger($this->logger);
75
        $c->setAuthState(new class () extends State {
76
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
77
            {
78
                return [
79
                    'FIDO2Displayname' => 'Donald Duck',
80
                    'FIDO2Username' => 'dduck',
81
                    'FIDO2Scope' => 'Ducktown',
82
                    'FIDO2Tokens' => [],
83
                    'FIDO2SignupChallenge' => 'abc123',
84
                    'FIDO2AuthSuccessful' => true,
85
                    'FIDO2PasswordlessAuthMode' => false,
86
                    'requestTokenModel' => 'something',
87
                ];
88
            }
89
        });
90
91
        $this->expectException(Exception::class);
92
        $this->expectExceptionMessage(
93
            "User attempted to authenticate with an unknown credential ID. This"
94
            . " should already have been prevented by the browser!",
95
        );
96
        $c->main($request);
97
    }
98
99
100
    /**
101
     */
102
    public function testPasswordlessAuthProcessWithoutProperTokenRaisesException(): void
103
    {
104
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/authprocess?StateId=someStateId';
105
        $request = Request::create(
106
            '/authprocess?StateId=someStateId',
107
            'POST',
108
            ['response_id' => 'abc123'],
109
        );
110
111
112
        $c = new Controller\AuthProcess($this->config, $this->session);
113
        $c->setLogger($this->logger);
114
        $c->setAuthState(new class () extends State {
115
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
116
            {
117
                return [
118
                    'FIDO2Displayname' => 'Donald Duck',
119
                    'FIDO2Username' => 'dduck',
120
                    'FIDO2Scope' => 'Ducktown',
121
                    'FIDO2Tokens' => [],
122
                    'FIDO2SignupChallenge' => 'abc123',
123
                    'FIDO2AuthSuccessful' => true,
124
                    'FIDO2PasswordlessAuthMode' => true,
125
                    'requestTokenModel' => 'something',
126
                ];
127
            }
128
        });
129
130
        $this->expectException(Exception::class);
131
        $this->expectExceptionMessage(
132
            "User attempted to authenticate with an unknown credential ID. This"
133
            . " should already have been prevented by the browser!",
134
        );
135
        $c->main($request);
136
    }
137
}
138