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 ( 6c4153...d181b6 )
by François
03:10
created

CertificatesModule::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 46

Duplication

Lines 24
Ratio 30.38 %

Importance

Changes 0
Metric Value
dl 24
loc 79
rs 8.8701
c 0
b 0
f 0
cc 1
eloc 46
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
 *  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
19
namespace SURFnet\VPN\Server\Api;
20
21
use SURFnet\VPN\Common\Http\AuthUtils;
22
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
23
use SURFnet\VPN\Common\Http\Service;
24
use SURFnet\VPN\Common\Http\Request;
25
use SURFnet\VPN\Common\Http\ApiResponse;
26
use SURFnet\VPN\Server\Storage;
27
use SURFnet\VPN\Server\CA\CaInterface;
28
use SURFnet\VPN\Server\TlsAuth;
29
use SURFnet\VPN\Common\RandomInterface;
30
31
class CertificatesModule implements ServiceModuleInterface
32
{
33
    /** @var \SURFnet\VPN\Server\CA\CaInterface */
34
    private $ca;
35
36
    /** @var \SURFnet\VPN\Server\Storage */
37
    private $storage;
38
39
    /** @var \SURFnet\VPN\Server\TlsAuth */
40
    private $tlsAuth;
41
42
    /** @var \SURFnet\VPN\Common\RandomInterface */
43
    private $random;
44
45
    public function __construct(CaInterface $ca, Storage $storage, TlsAuth $tlsAuth, RandomInterface $random)
46
    {
47
        $this->ca = $ca;
48
        $this->storage = $storage;
49
        $this->tlsAuth = $tlsAuth;
50
        $this->random = $random;
51
    }
52
53
    public function init(Service $service)
54
    {
55
        /* CERTIFICATES */
56
        $service->post(
57
            '/add_client_certificate',
58
            function (Request $request, array $hookData) {
59
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
60
61
                $userId = $request->getPostParameter('user_id');
62
                InputValidation::userId($userId);
63
                $displayName = $request->getPostParameter('display_name');
64
                InputValidation::displayName($displayName);
65
66
                // generate a random string as the certificate's CN
67
                $commonName = $this->random->get(16);
68
                $certInfo = $this->ca->clientCert($commonName);
69
                // add TLS Auth
70
                $certInfo['ta'] = $this->tlsAuth->get();
71
                $certInfo['ca'] = $this->ca->caCert();
72
73
                $this->storage->addCertificate($userId, $commonName, $displayName, $certInfo['valid_from'], $certInfo['valid_to']);
74
75
                return new ApiResponse('add_client_certificate', $certInfo, 201);
76
            }
77
        );
78
79
        $service->post(
80
            '/add_server_certificate',
81
            function (Request $request, array $hookData) {
82
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
83
84
                $commonName = $request->getPostParameter('common_name');
85
                InputValidation::commonName($commonName);
86
87
                $certInfo = $this->ca->serverCert($commonName);
88
                // add TLS Auth
89
                $certInfo['ta'] = $this->tlsAuth->get();
90
                $certInfo['ca'] = $this->ca->caCert();
91
92
                return new ApiResponse('add_server_certificate', $certInfo, 201);
93
            }
94
        );
95
96
        $service->post(
97
            '/disable_client_certificate',
98 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...
99
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
100
101
                $commonName = $request->getPostParameter('common_name');
102
                InputValidation::commonName($commonName);
103
104
                return new ApiResponse('disable_client_certificate', $this->storage->disableCertificate($commonName));
105
            }
106
        );
107
108
        $service->post(
109
            '/enable_client_certificate',
110 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...
111
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
112
113
                $commonName = $request->getPostParameter('common_name');
114
                InputValidation::commonName($commonName);
115
116
                return new ApiResponse('enable_client_certificate', $this->storage->enableCertificate($commonName));
117
            }
118
        );
119
120
        $service->get(
121
            '/list_client_certificates',
122 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...
123
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
124
125
                $userId = $request->getQueryParameter('user_id');
126
                InputValidation::userId($userId);
127
128
                return new ApiResponse('list_client_certificates', $this->storage->getCertificates($userId));
129
            }
130
        );
131
    }
132
}
133