Completed
Push — master ( bab30e...5d9c47 )
by Stefan
18s queued 14s
created

AuthProcessTest.php$1 ➔ loadState()   A

Complexity

Conditions 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.8666
cc 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\Error;
12
use SimpleSAML\HTTP\RunnableResponse;
13
use SimpleSAML\Logger;
14
use SimpleSAML\Module\webauthn\Controller;
15
use SimpleSAML\Session;
16
use SimpleSAML\Utils;
17
use SimpleSAML\XHTML\Template;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Set of tests for the controllers in the "webauthn" module.
22
 *
23
 * @package SimpleSAML\Test
24
 */
25
class AuthProcessTest extends TestCase
26
{
27
    /** @var \SimpleSAML\Configuration */
28
    protected $config;
29
30
    /** @var \SimpleSAML\Logger */
31
    protected $logger;
32
33
    /** @var \SimpleSAML\Session */
34
    protected $session;
35
36
37
    /**
38
     * Set up for each test.
39
     * @return void
40
     */
41
    protected function setUp(): void
42
    {
43
        parent::setUp();
44
45
        $this->config = Configuration::loadFromArray(
46
            [
47
                'module.enable' => ['webauthn' => true],
48
                'secretsalt' => 'abc123',
49
                'enable.saml20-idp' => true,
50
            ],
51
            '[ARRAY]',
52
            'simplesaml'
53
        );
54
55
        $this->session = Session::getSessionFromRequest();
56
57
        $this->logger = new class () extends Logger {
58
            public static function info(string $str): void
59
            {
60
                // do nothing
61
            }
62
        };
63
    }
64
65
66
    /**
67
     * @return void
68
     */
69
    public function testAuthProcessWithoutProperTokenRaisesException(): void
70
    {
71
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/authprocess';
72
        $request = Request::create(
73
            '/authprocess',
74
            'POST',
75
            ['StateId' => 'someStateId', 'response_id' => 'abc123']
76
        );
77
78
79
        $c = new Controller\AuthProcess($this->config, $this->session);
80
        $c->setLogger($this->logger);
81
        $c->setAuthState(new class () extends State {
82
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
83
            {
84
                return [
85
                    'FIDO2Displayname' => 'Donald Duck',
86
                    'FIDO2Username' => 'dduck',
87
                    'FIDO2Scope' => 'Ducktown',
88
                    'FIDO2Tokens' => [],
89
                    'FIDO2SignupChallenge' => 'abc123',
90
                    'FIDO2AuthSuccessful' => true,
91
                    'requestTokenModel' => 'something',
92
                    'Source' => [
93
                        'entityid' => 'https://idp.example.com',
94
                    ],
95
                ];
96
            }
97
        });
98
99
        $this->expectException(Exception::class);
100
        $this->expectExceptionMessage("User attempted to authenticate with an unknown credential ID. This should already have been prevented by the browser!");
101
        $c->main($request);
102
    }
103
}
104