|
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\UserConfig; |
|
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\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
28
|
|
|
use fkooman\IO\IOInterface; |
|
29
|
|
|
use fkooman\VPN\Server\Utils; |
|
30
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
31
|
|
|
|
|
32
|
|
|
class UserConfigModule implements ServiceModuleInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $configDir; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** @var \fkooman\IO\IOInterface */ |
|
41
|
|
|
private $io; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct($configDir, LoggerInterface $logger, IOInterface $io) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->configDir = $configDir; |
|
46
|
|
|
$this->logger = $logger; |
|
47
|
|
|
$this->io = $io; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function init(Service $service) |
|
51
|
|
|
{ |
|
52
|
|
|
$service->get( |
|
53
|
|
|
'/config/users', |
|
54
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
55
|
|
|
Utils::requireScope($tokenInfo, ['admin']); |
|
56
|
|
|
|
|
57
|
|
|
$userConfigArray = []; |
|
58
|
|
View Code Duplication |
foreach ($this->io->readFolder($this->configDir) as $configFile) { |
|
|
|
|
|
|
59
|
|
|
$userConfig = new UserConfig( |
|
60
|
|
|
Json::decode( |
|
61
|
|
|
$this->io->readFile($configFile) |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
// never expose the OTP secret |
|
65
|
|
|
$userConfig->hideOtpSecret(); |
|
66
|
|
|
$userConfigArray[basename($configFile)] = $userConfig->toArray(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$response = new JsonResponse(); |
|
70
|
|
|
$response->setBody($userConfigArray); |
|
71
|
|
|
|
|
72
|
|
|
return $response; |
|
73
|
|
|
} |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$service->get( |
|
77
|
|
|
'/config/users/:userId', |
|
78
|
|
View Code Duplication |
function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
|
|
|
|
|
79
|
|
|
Utils::requireScope($tokenInfo, ['admin', 'portal']); |
|
80
|
|
|
InputValidation::userId($userId); |
|
81
|
|
|
|
|
82
|
|
|
$fileName = sprintf('%s/%s', $this->configDir, $userId); |
|
83
|
|
|
if (!$this->io->isFile($fileName)) { |
|
84
|
|
|
// if the file does not exist, use default values |
|
85
|
|
|
$userConfig = new UserConfig([]); |
|
86
|
|
|
} else { |
|
87
|
|
|
$userConfig = new UserConfig( |
|
88
|
|
|
Json::decode( |
|
89
|
|
|
$this->io->readFile( |
|
90
|
|
|
sprintf('%s/%s', $this->configDir, $userId) |
|
91
|
|
|
) |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
// never expose the OTP secret |
|
95
|
|
|
$userConfig->hideOtpSecret(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$response = new JsonResponse(); |
|
99
|
|
|
$response->setBody($userConfig->toArray()); |
|
100
|
|
|
|
|
101
|
|
|
return $response; |
|
102
|
|
|
} |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$service->put( |
|
106
|
|
|
'/config/users/:userId/otp_secret', |
|
107
|
|
|
function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
108
|
|
|
Utils::requireScope($tokenInfo, ['admin', 'portal']); |
|
109
|
|
|
InputValidation::userId($userId); |
|
110
|
|
|
|
|
111
|
|
|
$fileName = sprintf('%s/%s', $this->configDir, $userId); |
|
112
|
|
|
if (!$this->io->isFile($fileName)) { |
|
113
|
|
|
// if the file does not exist, use default values |
|
114
|
|
|
$userConfig = new UserConfig([]); |
|
115
|
|
|
} else { |
|
116
|
|
|
$userConfig = new UserConfig( |
|
117
|
|
|
Json::decode( |
|
118
|
|
|
$this->io->readFile( |
|
119
|
|
|
sprintf('%s/%s', $this->configDir, $userId) |
|
120
|
|
|
) |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
// never expose the OTP secret |
|
124
|
|
|
$userConfig->hideOtpSecret(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (false !== $userConfig->getOtpSecret()) { |
|
128
|
|
|
// an OTP secret was already set, it is not allowed to |
|
129
|
|
|
// update the otp_secret using this API call |
|
130
|
|
|
throw new BadRequestException('otp_secret already set'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// we wrap the request body in an UserConfig object to validate |
|
134
|
|
|
// whatever is there |
|
135
|
|
|
$requestUserConfig = new UserConfig(Json::decode($request->getBody())); |
|
136
|
|
|
|
|
137
|
|
|
// we extract the OTP secret from the request body and set it |
|
138
|
|
|
$userConfig->setOtpSecret($requestUserConfig->getOtpSecret()); |
|
139
|
|
|
|
|
140
|
|
|
$this->io->writeFile( |
|
141
|
|
|
sprintf('%s/%s', $this->configDir, $userId), |
|
142
|
|
|
Json::encode($userConfig->toArray()) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$response = new JsonResponse(); |
|
146
|
|
|
$response->setBody(['ok' => true]); |
|
147
|
|
|
|
|
148
|
|
|
return $response; |
|
149
|
|
|
} |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
$service->put( |
|
153
|
|
|
'/config/users/:userId', |
|
154
|
|
View Code Duplication |
function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
|
|
|
|
|
155
|
|
|
Utils::requireScope($tokenInfo, ['admin']); |
|
156
|
|
|
InputValidation::userId($userId); |
|
157
|
|
|
|
|
158
|
|
|
// we wrap the request body in an UserConfig object to validate |
|
159
|
|
|
// whatever is there |
|
160
|
|
|
$requestUserConfig = new UserConfig(Json::decode($request->getBody())); |
|
161
|
|
|
|
|
162
|
|
|
$this->io->writeFile( |
|
163
|
|
|
sprintf('%s/%s', $this->configDir, $userId), |
|
164
|
|
|
Json::encode($requestUserConfig->toArray()) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$response = new JsonResponse(); |
|
168
|
|
|
$response->setBody(['ok' => true]); |
|
169
|
|
|
|
|
170
|
|
|
return $response; |
|
171
|
|
|
} |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
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.