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 ( 9650d4...b69b8b )
by François
02:36
created

CertificatesModule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 96
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 9
dl 21
loc 96
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A init() 21 73 1

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