GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TwoFactorHook   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 77
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C executeBefore() 0 58 9
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\Http;
11
12
use fkooman\SeCookie\SessionInterface;
13
use SURFnet\VPN\Common\Http\Exception\HttpException;
14
use SURFnet\VPN\Common\HttpClient\ServerClient;
15
use SURFnet\VPN\Common\TplInterface;
16
17
class TwoFactorHook implements BeforeHookInterface
18
{
19
    /** @var \fkooman\SeCookie\SessionInterface; */
20
    private $session;
21
22
    /** @var \SURFnet\VPN\Common\TplInterface */
23
    private $tpl;
24
25
    /** @var \SURFnet\VPN\Common\HttpClient\ServerClient */
26
    private $serverClient;
27
28
    public function __construct(SessionInterface $session, TplInterface $tpl, ServerClient $serverClient)
29
    {
30
        $this->session = $session;
31
        $this->tpl = $tpl;
32
        $this->serverClient = $serverClient;
33
    }
34
35
    public function executeBefore(Request $request, array $hookData)
36
    {
37
        // some URIs are allowed as they are used for either logging in, or
38
        // verifying the OTP key
39
        $allowedUris = [
40
            '/_form/auth/verify',
41
            '/_form/auth/logout',
42
            '/_two_factor/auth/verify/totp',
43
            '/_two_factor/auth/verify/yubi',
44
            '/_oauth/token',
45
        ];
46
47
        if (in_array($request->getPathInfo(), $allowedUris) && 'POST' === $request->getRequestMethod()) {
48
            return false;
49
        }
50
51
        if (!array_key_exists('auth', $hookData)) {
52
            throw new HttpException('authentication hook did not run before', 500);
53
        }
54
        $userId = $hookData['auth'];
55
56
        if ($this->session->has('_two_factor_verified')) {
57
            if ($userId !== $this->session->get('_two_factor_verified')) {
58
                throw new HttpException('two-factor code not bound to authenticated user', 400);
59
            }
60
61
            return true;
62
        }
63
64
        $hasTotpSecret = $this->serverClient->get('has_totp_secret', ['user_id' => $userId]);
65
        $hasYubiId = $this->serverClient->get('has_yubi_key_id', ['user_id' => $userId]);
66
67
        // check if the user is enrolled for 2FA, if not we are fine, for this
68
        // session we assume we are verified!
69
        if (!$hasTotpSecret && !$hasYubiId) {
70
            $this->session->regenerate(true);
71
            $this->session->set('_two_factor_verified', $userId);
72
73
            return false;
74
        }
75
76
        // if not Yubi, then TOTP
77
        $templateName = $hasYubiId ? 'twoFactorYubiKeyOtp' : 'twoFactorTotp';
78
79
        // any other URL, enforce 2FA
80
        $response = new Response(200, 'text/html');
81
        $response->setBody(
82
            $this->tpl->render(
83
                $templateName,
84
                [
85
                    '_two_factor_auth_invalid' => false,
86
                    '_two_factor_auth_redirect_to' => $request->getUri(),
87
                ]
88
            )
89
        );
90
91
        return $response;
92
    }
93
}
94