Passed
Pull Request — master (#31)
by Tim
02:18
created

ManageToken::main()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 68
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 68
rs 8.0555
c 1
b 0
f 0
eloc 32
cc 9
nc 9
nop 1

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
namespace SimpleSAML\Module\webauthn\Controller;
4
5
use Datetime;
6
use Exception;
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module;
13
use SimpleSAML\Module\webauthn\WebAuthn\StaticProcessHelper;
14
use SimpleSAML\Session;
15
use SimpleSAML\Utils;
16
use Symfony\Component\HttpFoundation\Request;
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 ManageToken
26
{
27
    /** @var \SimpleSAML\Configuration */
28
    protected $config;
29
30
    /** @var \SimpleSAML\Session */
31
    protected $session;
32
33
    /**
34
     * @var \SimpleSAML\Auth\State|string
35
     * @psalm-var \SimpleSAML\Auth\State|class-string
36
     */
37
    protected $authState = Auth\State::class;
38
39
    /**
40
     * @var \SimpleSAML\Logger|string
41
     * @psalm-var \SimpleSAML\Logger|class-string
42
     */
43
    protected $logger = Logger::class;
44
45
46
    /**
47
     * Controller constructor.
48
     *
49
     * It initializes the global configuration and session for the controllers implemented here.
50
     *
51
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
52
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
53
     *
54
     * @throws \Exception
55
     */
56
    public function __construct(
57
        Configuration $config,
58
        Session $session
59
    ) {
60
        $this->config = $config;
61
        $this->session = $session;
62
    }
63
64
65
    /**
66
     * Inject the \SimpleSAML\Auth\State dependency.
67
     *
68
     * @param \SimpleSAML\Auth\State $authState
69
     */
70
    public function setAuthState(Auth\State $authState): void
71
    {
72
        $this->authState = $authState;
73
    }
74
75
76
    /**
77
     * Inject the \SimpleSAML\Logger dependency.
78
     *
79
     * @param \SimpleSAML\Logger $logger
80
     */
81
    public function setLogger(Logger $logger): void
82
    {
83
        $this->logger = $logger;
84
    }
85
86
87
    /**
88
     * @param \Symfony\Component\HttpFoundation\Request $request
89
     * @return \SimpleSAML\HTTP\RunnableResponse  A Symfony Response-object.
90
     */
91
    public function main(Request $request): RunnableResponse
92
    {
93
//        if (session_status() != PHP_SESSION_ACTIVE) {
94
//            session_cache_limiter('nocache');
95
//        }
96
97
        $this->logger::info('FIDO2 - Accessing WebAuthn token management');
98
99
        $stateId = $request->request->get('StateId');
100
        if ($stateId === null) {
101
            throw new Error\BadRequest('Missing required StateId query parameter.');
102
        }
103
104
        /** @var array $state */
105
        $state = $this->authState::loadState($stateId, 'webauthn:request');
106
107
        if ($state['FIDO2AuthSuccessful'] === false) {
108
            throw new Exception("Attempt to access the token management page unauthenticated.");
109
        }
110
111
        switch ($request->request->get('submit')) {
112
            case "NEVERMIND":
113
                $response = new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
114
                break;
115
            case "DELETE":
116
                $credId = $request->request->get('credId');
117
                if ($state['FIDO2AuthSuccessful'] == $credId) {
118
                    throw new Exception("Attempt to delete the currently used credential despite UI preventing this.");
119
                }
120
121
                $store = $state['webauthn:store'];
122
                $store->deleteTokenData($credId);
123
124
                if (array_key_exists('Registration', $state)) {
125
                    foreach ($state['FIDO2Tokens'] as $key => $value) {
126
                        if ($state['FIDO2Tokens'][$key][0] == $credId) {
127
                            unset($state['FIDO2Tokens'][$key]);
128
                            break;
129
                        }
130
                    }
131
132
                    $response = new RunnableResponse([StaticProcessHelper::class, 'saveStateAndRedirect'], [$state]);
133
                } else {
134
                    $response = new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
135
                }
136
                break;
137
            default:
138
                throw new Exception("Unknown submit button state.");
139
        }
140
141
        $response->headers->set('Expires', 'Thu, 19 Nov 1981 08:52:00 GMT');
142
        $response->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
143
        $response->headers->set('Pragma', 'no-cache');
144
145
        /** Symfony 5 style */
146
        /**
147
        $response->setCache([
148
            'must_revalidate'  => true,
149
            'no_cache'         => true,
150
            'no_store'         => true,
151
            'no_transform'     => false,
152
            'public'           => false,
153
            'private'          => false,
154
        ]);
155
        $response->setExpires(new DateTime('Thu, 19 Nov 1981 08:52:00 GMT'));
156
        */
157
158
        return $response;
159
    }
160
}
161