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::init()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 80
Code Lines 49

Duplication

Lines 70
Ratio 87.5 %

Importance

Changes 0
Metric Value
dl 70
loc 80
rs 8.4041
c 0
b 0
f 0
cc 5
eloc 49
nc 1
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
/**
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