Passed
Push — master ( f07e2d...f5be48 )
by Stefan
11:00 queued 08:45
created

Supercharged::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\webauthn\Controller;
6
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Module;
12
use SimpleSAML\Session;
13
use SimpleSAML\Utils;
14
use SimpleSAML\XHTML\Template;
15
use Symfony\Component\HttpFoundation\Request;
16
use SimpleSAML\Module\webauthn\Store;
17
18
/**
19
 * Controller class for the webauthn module.
20
 *
21
 * This class serves the different views available in the module.
22
 *
23
 * @package SimpleSAML\Module\webauthn
24
 */
25
class Supercharged extends WebAuthn
26
{
27
    /**
28
     * Controller constructor.
29
     *
30
     * It initializes the global configuration and session for the controllers implemented here.
31
     *
32
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
33
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
34
     *
35
     * @throws \Exception
36
     */
37
    public function __construct(
38
        Configuration $config,
39
        Session $session
40
    ) {
41
        parent::__construct($config, $session);
42
    }
43
44
    /**
45
     * @param \Symfony\Component\HttpFoundation\Request $request
46
     * @return \SimpleSAML\XHTML\Template  A Symfony Response-object.
47
     */
48
    public function main(Request $request): Template
49
    {
50
        $this->logger::info('FIDO2 - Accessing Supercharged interface');
51
52
        $stateId = $request->query->get('StateId');
53
        if ($stateId === null) {
54
            throw new Error\BadRequest('Missing required StateId query parameter.');
55
        }
56
57
        $state = $this->authState::loadState($stateId, 'webauthn:request');
58
59
        $templateFile = 'webauthn:supercharged.twig';
60
61
        // Make, populate and layout consent form
62
        $t = new Template($this->config, $templateFile);
63
        $t->data['UserID'] = $state['FIDO2Username'];
64
        $t->data['FIDO2Tokens'] = $state['FIDO2Tokens'];
65
66
        $challenge = str_split($state['FIDO2SignupChallenge'], 2);
67
        $configUtils = new Utils\Config();
68
        $username = str_split(
69
            hash('sha512', $state['FIDO2Username'] . '|' . $configUtils->getSecretSalt()),
70
            2
71
        );
72
73
        $challengeEncoded = [];
74
        foreach ($challenge as $oneChar) {
75
            $challengeEncoded[] = hexdec($oneChar);
76
        }
77
78
        $credentialIdEncoded = [];
79
        foreach ($state['FIDO2Tokens'] as $number => $token) {
80
            $idSplit = str_split($token[0], 2);
81
            $credentialIdEncoded[$number] = [];
82
            foreach ($idSplit as $credIdBlock) {
83
                $credentialIdEncoded[$number][] = hexdec($credIdBlock);
84
            }
85
        }
86
87
        $usernameEncoded = [];
88
        foreach ($username as $oneChar) {
89
            $usernameEncoded[] = hexdec($oneChar);
90
        }
91
92
        $frontendData = [];
93
        $frontendData['challengeEncoded'] = $challengeEncoded;
94
        $frontendData['state'] = [];
95
        foreach (['FIDO2Scope','FIDO2Username','FIDO2Displayname','requestTokenModel'] as $stateItem) {
96
            $frontendData['state'][$stateItem] = $state[$stateItem];
97
        }
98
99
        $t->data['showExitButton'] = !array_key_exists('Registration', $state);
100
        $frontendData['usernameEncoded'] = $usernameEncoded;
101
        $frontendData['attestation'] = $state['requestTokenModel'] ? "indirect" : "none";
102
        $frontendData['credentialIdEncoded'] = $credentialIdEncoded;
103
        $frontendData['FIDO2PasswordlessAuthMode'] = $state['FIDO2PasswordlessAuthMode'];
104
        $t->data['hasPreviouslyDonePasswordless'] = $_COOKIE['SuccessfullyUsedPasswordlessBefore'] ?? "NO";
105
        $t->data['frontendData'] = json_encode($frontendData);
106
107
        $t->data['authForm'] = "";
108
        $t->data['authURL'] = Module::getModuleURL('webauthn/authprocess?StateId=' . urlencode($stateId));
109
        $t->data['pushbackURL'] = Module::getModuleURL('webauthn/pushbackuserpass?StateId=' . urlencode($stateId));
110
        
111
        // dynamically generate the JS code needed for token registration
112
        return $t;
113
    }
114
}
115