Supercharged::main()   B
last analyzed

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