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 ( f24dc9...334f9c )
by François
02:13
created

InfoModule::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace fkooman\VPN\Server\Info;
4
5
use fkooman\Http\Request;
6
use fkooman\Rest\Service;
7
use fkooman\Rest\ServiceModuleInterface;
8
use fkooman\Http\JsonResponse;
9
use fkooman\Http\Exception\ForbiddenException;
10
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo;
11
use fkooman\VPN\Server\Utils;
12
13
class InfoModule implements ServiceModuleInterface
14
{
15
    /** @var array */
16
    private $v4;
17
18
    /** @var array */
19
    private $v6;
20
21
    public function __construct(array $v4, array $v6)
22
    {
23
        $this->v4 = $v4;
24
        $this->v6 = $v6;
25
    }
26
27
    public function init(Service $service)
28
    {
29
        $service->get(
30
            '/info/net',
31
            function (Request $request, TokenInfo $tokenInfo) {
32
                self::requireScope($tokenInfo, 'info_get');
33
34
                return $this->getInfo();
35
            }
36
        );
37
    }
38
39
    private function getInfo()
40
    {
41
        $prefix6 = $this->v6['prefix'];
42
        $net4 = $this->v4['range'];
43
44
        $responseData = [];
45
        $responseData['range'] = $net4;
46
        $responseData['range6'] = Utils::convert4to6($prefix6, $net4);
47
        $responseData['pools'] = [];
48
49
        foreach ($this->v4['pools'] as $id => $pool) {
50
            $poolInfo = [
51
                'name' => $pool['name'],
52
                'range' => $pool['range'],
53
                'range6' => Utils::convert4to6($prefix6, $pool['range']),
54
            ];
55
            if (!array_key_exists('firewall', $pool)) {
56
                $pool['firewall'] = [];
57
            }
58
            if (!array_key_exists('dst_net', $pool['firewall'])) {
59
                $pool['firewall']['dst_net'] = ['0.0.0.0/0', '::/0'];
60
            }
61
            if (!array_key_exists('dst_port', $pool['firewall'])) {
62
                $pool['firewall']['dst_port'] = ['*/*'];
63
            }
64
            $poolInfo['firewall'] = $pool['firewall'];
65
66
            $responseData['pools'][$id] = $poolInfo;
67
        }
68
69
        $response = new JsonResponse();
70
        $response->setBody($responseData);
71
72
        return $response;
73
    }
74
75
    private static function requireScope(TokenInfo $tokenInfo, $requiredScope)
76
    {
77
        if (!$tokenInfo->getScope()->hasScope($requiredScope)) {
78
            throw new ForbiddenException('insufficient_scope', sprintf('"%s" scope required', $requiredScope));
79
        }
80
    }
81
}
82