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.

TwoFactorModule   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 66.09 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 10
dl 76
loc 115
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B init() 70 80 5
A validateRedirectTo() 6 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception\ApiException;
15
use SURFnet\VPN\Common\HttpClient\ServerClient;
16
use SURFnet\VPN\Common\TplInterface;
17
18
class TwoFactorModule implements ServiceModuleInterface
19
{
20
    /** @var \SURFnet\VPN\Common\HttpClient\ServerClient */
21
    private $serverClient;
22
23
    /** @var \fkooman\SeCookie\SessionInterface; */
24
    private $session;
25
26
    /** @var \SURFnet\VPN\Common\TplInterface */
27
    private $tpl;
28
29
    public function __construct(ServerClient $serverClient, SessionInterface $session, TplInterface $tpl)
30
    {
31
        $this->serverClient = $serverClient;
32
        $this->session = $session;
33
        $this->tpl = $tpl;
34
    }
35
36
    public function init(Service $service)
37
    {
38
        $service->post(
39
            '/_two_factor/auth/verify/totp',
40 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                if (!array_key_exists('auth', $hookData)) {
42
                    throw new HttpException('authentication hook did not run before', 500);
43
                }
44
                $userId = $hookData['auth'];
45
46
                $this->session->delete('_two_factor_verified');
47
48
                $totpKey = InputValidation::totpKey($request->getPostParameter('_two_factor_auth_totp_key'));
49
                $redirectTo = $request->getPostParameter('_two_factor_auth_redirect_to');
50
                self::validateRedirectTo($request, $redirectTo);
51
52
                try {
53
                    $this->serverClient->post('verify_totp_key', ['user_id' => $userId, 'totp_key' => $totpKey]);
54
                    $this->session->regenerate(true);
55
                    $this->session->set('_two_factor_verified', $userId);
56
57
                    return new RedirectResponse($redirectTo, 302);
58
                } catch (ApiException $e) {
59
                    // unable to validate the OTP
60
                    $response = new Response(200, 'text/html');
61
                    $response->setBody(
62
                        $this->tpl->render(
63
                            'twoFactorTotp',
64
                            [
65
                                '_two_factor_auth_invalid' => true,
66
                                '_two_factor_auth_error_msg' => $e->getMessage(),
67
                                '_two_factor_auth_redirect_to' => $redirectTo,
68
                            ]
69
                        )
70
                    );
71
72
                    return $response;
73
                }
74
            }
75
        );
76
77
        $service->post(
78
            '/_two_factor/auth/verify/yubi',
79 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
                if (!array_key_exists('auth', $hookData)) {
81
                    throw new HttpException('authentication hook did not run before', 500);
82
                }
83
                $userId = $hookData['auth'];
84
85
                $this->session->delete('_two_factor_verified');
86
87
                $yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('_two_factor_auth_yubi_key_otp'));
88
                $redirectTo = $request->getPostParameter('_two_factor_auth_redirect_to');
89
                self::validateRedirectTo($request, $redirectTo);
90
91
                try {
92
                    $this->serverClient->post('verify_yubi_key_otp', ['user_id' => $userId, 'yubi_key_otp' => $yubiKeyOtp]);
93
                    $this->session->regenerate(true);
94
                    $this->session->set('_two_factor_verified', $userId);
95
96
                    return new RedirectResponse($redirectTo, 302);
97
                } catch (ApiException $e) {
98
                    // unable to validate the OTP
99
                    $response = new Response(200, 'text/html');
100
                    $response->setBody(
101
                        $this->tpl->render(
102
                            'twoFactorYubiKeyOtp',
103
                            [
104
                                '_two_factor_auth_invalid' => true,
105
                                '_two_factor_auth_error_msg' => $e->getMessage(),
106
                                '_two_factor_auth_redirect_to' => $redirectTo,
107
                            ]
108
                        )
109
                    );
110
111
                    return $response;
112
                }
113
            }
114
        );
115
    }
116
117
    private static function validateRedirectTo(Request $request, $redirectTo)
118
    {
119
        // validate the URL
120 View Code Duplication
        if (false === filter_var($redirectTo, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
            throw new HttpException('invalid redirect_to URL', 400);
122
        }
123
124
        // extract the "host" part of the URL
125 View Code Duplication
        if (false === $redirectToHost = parse_url($redirectTo, PHP_URL_HOST)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
            throw new HttpException('invalid redirect_to URL, unable to extract host', 400);
127
        }
128
        if ($request->getServerName() !== $redirectToHost) {
129
            throw new HttpException('redirect_to does not match expected host', 400);
130
        }
131
    }
132
}
133