|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Config; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Http\Request; |
|
21
|
|
|
use fkooman\Rest\Service; |
|
22
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
23
|
|
|
use fkooman\Http\JsonResponse; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use fkooman\VPN\Server\InputValidation; |
|
26
|
|
|
use fkooman\Json\Json; |
|
27
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
28
|
|
|
use fkooman\Http\Exception\ForbiddenException; |
|
29
|
|
|
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
30
|
|
|
|
|
31
|
|
|
class ConfigModule implements ServiceModuleInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var ConfigStorageInterface */ |
|
34
|
|
|
private $configStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
private $allowedPools; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
40
|
|
|
private $logger; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(ConfigStorageInterface $configStorage, array $allowedPools, LoggerInterface $logger) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->configStorage = $configStorage; |
|
45
|
|
|
$this->allowedPools = $allowedPools; |
|
46
|
|
|
$this->logger = $logger; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function init(Service $service) |
|
50
|
|
|
{ |
|
51
|
|
|
// get all configurations |
|
52
|
|
|
$service->get( |
|
53
|
|
|
'/config/', |
|
54
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
55
|
|
|
$userId = $request->getUrl()->getQueryParameter('user_id'); |
|
56
|
|
|
if (!is_null($userId)) { |
|
57
|
|
|
self::requireScope($tokenInfo, ['config_get', 'config_get_user']); |
|
58
|
|
|
InputValidation::userId($userId); |
|
59
|
|
|
} else { |
|
60
|
|
|
self::requireScope($tokenInfo, ['config_get']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$response = new JsonResponse(); |
|
64
|
|
|
$response->setBody( |
|
65
|
|
|
[ |
|
66
|
|
|
'items' => $this->configStorage->getAllConfig($userId), |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
return $response; |
|
71
|
|
|
} |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
// get configuration for a particular common_name |
|
75
|
|
|
$service->get( |
|
76
|
|
|
'/config/:commonName', |
|
77
|
|
|
function (Request $request, TokenInfo $tokenInfo, $commonName) { |
|
78
|
|
|
self::requireScope($tokenInfo, ['config_get']); |
|
79
|
|
|
|
|
80
|
|
|
InputValidation::commonName($commonName); |
|
81
|
|
|
|
|
82
|
|
|
$response = new JsonResponse(); |
|
83
|
|
|
$response->setBody( |
|
84
|
|
|
$this->configStorage->getConfig($commonName)->toArray() |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
return $response; |
|
88
|
|
|
} |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
// set configuration for a particular common_name |
|
92
|
|
|
$service->put( |
|
93
|
|
|
'/config/:commonName', |
|
94
|
|
|
function (Request $request, TokenInfo $tokenInfo, $commonName) { |
|
95
|
|
|
self::requireScope($tokenInfo, ['config_update']); |
|
96
|
|
|
|
|
97
|
|
|
// XXX check content type |
|
98
|
|
|
// XXX allow for disconnect as well when updating config |
|
99
|
|
|
|
|
100
|
|
|
InputValidation::commonName($commonName); |
|
101
|
|
|
|
|
102
|
|
|
$configData = new ConfigData(Json::decode($request->getBody())); |
|
103
|
|
|
if (!in_array($configData->getPool(), $this->allowedPools)) { |
|
104
|
|
|
throw new BadRequestException('invalid "pool"'); |
|
105
|
|
|
} |
|
106
|
|
|
$this->configStorage->setConfig($commonName, $configData); |
|
107
|
|
|
|
|
108
|
|
|
$response = new JsonResponse(); |
|
109
|
|
|
$response->setBody( |
|
110
|
|
|
['ok' => true] |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
return $response; |
|
114
|
|
|
} |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private static function requireScope(TokenInfo $tokenInfo, array $requiredScope) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($requiredScope as $s) { |
|
121
|
|
|
if ($tokenInfo->getScope()->hasScope($s)) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
throw new ForbiddenException('insufficient_scope', sprintf('"%s" scope required', implode(',', $requiredScope))); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|