Passed
Push — master ( 84fc8e...b36567 )
by Tim
02:42
created

Supercharged::main()   B

Complexity

Conditions 8
Paths 49

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 42
c 1
b 0
f 0
nc 49
nop 1
dl 0
loc 67
rs 8.0035

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\webauthn\Controller;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Module;
10
use SimpleSAML\Session;
11
use SimpleSAML\Utils;
12
use SimpleSAML\XHTML\Template;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Controller class for the webauthn module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package SimpleSAML\Module\webauthn
21
 */
22
class Supercharged extends WebAuthn
23
{
24
    /**
25
     * @param \Symfony\Component\HttpFoundation\Request $request
26
     * @return \SimpleSAML\XHTML\Template  A Symfony Response-object.
27
     */
28
    public function main(Request $request): Template
29
    {
30
        $this->logger::info('FIDO2 - Accessing Supercharged interface');
31
32
        $stateId = $request->query->get('StateId');
33
        if ($stateId === null) {
34
            throw new Error\BadRequest('Missing required StateId query parameter.');
35
        }
36
37
        $state = $this->authState::loadState($stateId, 'webauthn:request');
38
39
        $templateFile = 'webauthn:supercharged.twig';
40
41
        // Make, populate and layout consent form
42
        $t = new Template($this->config, $templateFile);
43
        $t->data['UserID'] = $state['FIDO2Username'];
44
        $t->data['FIDO2Tokens'] = $state['FIDO2Tokens'];
45
        // in case IdPs want to override UI and display SP-specific content
46
        $t->data['entityid'] = $state['SPMetadata']['entityid'] ?? 'WEBAUTHN-SP-NONE';
47
48
        $challenge = str_split($state['FIDO2SignupChallenge'], 2);
49
        $configUtils = new Utils\Config();
50
        $username = str_split(
51
            hash('sha512', $state['FIDO2Username'] . '|' . $configUtils->getSecretSalt()),
52
            2,
53
        );
54
55
        $challengeEncoded = [];
56
        foreach ($challenge as $oneChar) {
57
            $challengeEncoded[] = hexdec($oneChar);
58
        }
59
60
        $credentialIdEncoded = [];
61
        foreach ($state['FIDO2Tokens'] as $number => $token) {
62
            $idSplit = str_split($token[0], 2);
63
            $credentialIdEncoded[$number] = [];
64
            foreach ($idSplit as $credIdBlock) {
65
                $credentialIdEncoded[$number][] = hexdec($credIdBlock);
66
            }
67
        }
68
69
        $usernameEncoded = [];
70
        foreach ($username as $oneChar) {
71
            $usernameEncoded[] = hexdec($oneChar);
72
        }
73
74
        $frontendData = [];
75
        $frontendData['challengeEncoded'] = $challengeEncoded;
76
        $frontendData['state'] = [];
77
        foreach (['FIDO2Scope','FIDO2Username','FIDO2Displayname','requestTokenModel'] as $stateItem) {
78
            $frontendData['state'][$stateItem] = $state[$stateItem];
79
        }
80
81
        $t->data['showExitButton'] = !array_key_exists('Registration', $state);
82
        $frontendData['usernameEncoded'] = $usernameEncoded;
83
        $frontendData['attestation'] = $state['requestTokenModel'] ? "indirect" : "none";
84
        $frontendData['credentialIdEncoded'] = $credentialIdEncoded;
85
        $frontendData['FIDO2PasswordlessAuthMode'] = $state['FIDO2PasswordlessAuthMode'];
86
        $t->data['hasPreviouslyDonePasswordless'] = $_COOKIE['SuccessfullyUsedPasswordlessBefore'] ?? "NO";
87
        $t->data['frontendData'] = json_encode($frontendData);
88
89
        $t->data['authForm'] = "";
90
        $t->data['authURL'] = Module::getModuleURL('webauthn/authprocess?StateId=' . urlencode($stateId));
91
        $t->data['pushbackURL'] = Module::getModuleURL('webauthn/pushbackuserpass?StateId=' . urlencode($stateId));
92
93
        // dynamically generate the JS code needed for token registration
94
        return $t;
95
    }
96
}
97