Passed
Push — master ( 0397a8...329aee )
by Tim
04:34 queued 01:46
created

AuthProcessTest.php$2 ➔ testPasswordlessAuthProcessWithoutProperTokenRaisesException()   A

Complexity

Conditions 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
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