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.
Completed
Push — master ( 8128d3...2550f8 )
by François
02:58
created

TwoFactorHook::executeBefore()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 6
nop 2
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Common\Http;
19
20
use SURFnet\VPN\Common\HttpClient\ServerClient;
21
use SURFnet\VPN\Common\TplInterface;
22
use SURFnet\VPN\Common\Http\Exception\HttpException;
23
24
class TwoFactorHook implements BeforeHookInterface
25
{
26
    /** @var SessionInterface */
27
    private $session;
28
29
    /** @var \SURFnet\VPN\Common\TplInterface */
30
    private $tpl;
31
32
    /** @var \SURFnet\VPN\Common\HttpClient\ServerClient */
33
    private $serverClient;
34
35
    public function __construct(SessionInterface $session, TplInterface $tpl, ServerClient $serverClient)
36
    {
37
        $this->session = $session;
38
        $this->tpl = $tpl;
39
        $this->serverClient = $serverClient;
40
    }
41
42
    public function executeBefore(Request $request, array $hookData)
43
    {
44
        if (!array_key_exists('auth', $hookData)) {
45
            throw new HttpException('authentication hook did not run before', 500);
46
        }
47
        $userId = $hookData['auth'];
48
49
        if ($this->session->has('_two_factor_verified')) {
50
            if ($userId !== $this->session->get('_two_factor_verified')) {
51
                throw new HttpException('two-factor code not bound to authenticated user', 400);
52
            }
53
54
            return true;
55
        }
56
57
        // some URIs are allowed as they are used for either logging in, or
58
        // verifying the OTP key
59
        $allowedUris = [
60
            '/_form/auth/verify',
61
            '/_form/auth/logout',
62
            '/_two_factor/auth/verify',
63
        ];
64
65
        if (in_array($request->getPathInfo(), $allowedUris) && 'POST' === $request->getRequestMethod()) {
66
            return false;
67
        }
68
69
        // check if the user is enrolled for 2FA, if not we are fine, for this
70
        // session we assume we are verified!
71
        if (!$this->serverClient->hasOtpSecret($userId)) {
72
            $this->session->set('_two_factor_verified', $userId);
73
74
            return false;
75
        }
76
77
        // any other URL, enforce 2FA
78
        $response = new Response(200, 'text/html');
79
        $response->setBody(
80
            $this->tpl->render(
81
                'twoFactorAuthentication',
82
                [
83
                    '_two_factor_auth_invalid_key' => false,
84
                    '_two_factor_auth_redirect_to' => $request->getUri(),
85
                ]
86
            )
87
        );
88
89
        return $response;
90
    }
91
}
92