-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathConfigController.php
More file actions
executable file
·368 lines (341 loc) · 11.2 KB
/
ConfigController.php
File metadata and controls
executable file
·368 lines (341 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* Nextcloud - openproject
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2021
*/
namespace OCA\OpenProject\Controller;
use OCP\IURLGenerator;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Controller;
use OCA\OpenProject\Service\OauthService;
use OCA\OpenProject\Service\OpenProjectAPIService;
use OCA\OpenProject\AppInfo\Application;
use Psr\Log\LoggerInterface;
class ConfigController extends Controller {
/**
* @var IConfig
*/
private $config;
/**
* @var IURLGenerator
*/
private $urlGenerator;
/**
* @var IUserManager
*/
private $userManager;
/**
* @var IL10N
*/
private $l;
/**
* @var OpenProjectAPIService
*/
private $openprojectAPIService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var string|null
*/
private $userId;
/**
* @var OauthService
*/
private $oauthService;
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator,
IUserManager $userManager,
IL10N $l,
OpenProjectAPIService $openprojectAPIService,
LoggerInterface $logger,
OauthService $oauthService,
?string $userId) {
parent::__construct($appName, $request);
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->l = $l;
$this->openprojectAPIService = $openprojectAPIService;
$this->logger = $logger;
$this->userId = $userId;
$this->oauthService = $oauthService;
}
/**
* @param string|null $userId
* @return void
*/
public function clearUserInfo(string $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
$this->config->deleteUserValue($userId, Application::APP_ID, 'token');
$this->config->deleteUserValue($userId, Application::APP_ID, 'login');
$this->config->deleteUserValue($userId, Application::APP_ID, 'user_id');
$this->config->deleteUserValue($userId, Application::APP_ID, 'user_name');
$this->config->deleteUserValue($userId, Application::APP_ID, 'refresh_token');
}
/**
* set config values
* @NoAdminRequired
*
* @param array<string, string> $values
* @return DataResponse
*/
public function setConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setUserValue($this->userId, Application::APP_ID, $key, trim($value));
}
$result = [];
if (isset($values['token'])) {
if ($values['token'] && $values['token'] !== '') {
$result = $this->storeUserInfo();
} else {
$this->clearUserInfo();
$result = [
'user_name' => '',
];
}
}
if (isset($result['error'])) {
return new DataResponse($result, Http::STATUS_UNAUTHORIZED);
} else {
return new DataResponse($result);
}
}
/**
* set admin config values
* @NoCSRFRequired
*
* @param array<string, string|null> $values
*
* @return DataResponse
*/
public function setAdminConfig(array $values): DataResponse {
$allowedKeys = [
'oauth_instance_url',
'client_id',
'client_secret',
'default_enable_navigation',
'default_enable_notifications',
'default_enable_unified_search'
];
// if values contains a key that is not in the allowedKeys array,
// return a response with status code 400 and an error message
foreach ($values as $key => $value) {
if (!in_array($key, $allowedKeys)) {
return new DataResponse([
'error' => $this->l->t('Invalid key')
], Http::STATUS_BAD_REQUEST);
}
}
$oldOpenProjectOauthUrl = $this->config->getAppValue(
Application::APP_ID, 'oauth_instance_url', ''
);
$oldClientId = $this->config->getAppValue(
Application::APP_ID, 'client_id', ''
);
$oldClientSecret = $this->config->getAppValue(
Application::APP_ID, 'client_secret', ''
);
foreach ($values as $key => $value) {
$this->config->setAppValue(Application::APP_ID, $key, trim($value));
}
if (key_exists('oauth_instance_url', $values)
&& $oldOpenProjectOauthUrl !== $values['oauth_instance_url']
) {
if (is_null($values['oauth_instance_url']) || $values['oauth_instance_url'] === '') {
$this->deleteOauthClient();
} else {
$oauthClientInternalId = $this->config->getAppValue(
Application::APP_ID, 'nc_oauth_client_id', ''
);
$this->oauthService->setClientRedirectUri(
(int) $oauthClientInternalId, $values['oauth_instance_url']
);
}
}
if ((key_exists('client_id', $values) && $values['client_id'] !== $oldClientId) ||
(key_exists('client_secret', $values) && $values['client_secret'] !== $oldClientSecret)) {
$this->userManager->callForAllUsers(function (IUser $user) {
$this->clearUserInfo($user->getUID());
});
}
return new DataResponse([
"status" => OpenProjectAPIService::isAdminConfigOk($this->config)
]);
}
/**
* receive oauth code and get oauth access token
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $code
* @param string $state
* @return RedirectResponse
*/
public function oauthRedirect(string $code = '', string $state = ''): RedirectResponse {
$configState = $this->config->getUserValue($this->userId, Application::APP_ID, 'oauth_state');
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
$codeVerifier = $this->config->getUserValue(
$this->userId, Application::APP_ID, 'code_verifier', false
);
$oauthJourneyStartingPage = $this->config->getUserValue(
$this->userId, Application::APP_ID, 'oauth_journey_starting_page'
);
try {
$oauthJourneyStartingPageDecoded = \Safe\json_decode($oauthJourneyStartingPage);
if ($oauthJourneyStartingPageDecoded->page === 'dashboard') {
$newUrl = $this->urlGenerator->linkToRoute('dashboard.dashboard.index');
} elseif ($oauthJourneyStartingPageDecoded->page === 'settings') {
$newUrl = $this->urlGenerator->linkToRoute(
'settings.PersonalSettings.index', ['section' => 'openproject']
);
} elseif ($oauthJourneyStartingPageDecoded->page === 'files') {
$newUrl = $this->urlGenerator->linkToRoute('files.view.index', [
'dir' => $oauthJourneyStartingPageDecoded->file->dir,
'scrollto' => $oauthJourneyStartingPageDecoded->file->name
]);
} else {
$this->logger->error(
'could not determine where the OAuth journey ' .
'to connect to OpenProject started'
);
throw new \Exception();
}
} catch (\Exception $e) {
$newUrl = $this->urlGenerator->linkToRoute('files.view.index');
}
// anyway, reset state
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'oauth_state');
$validCodeVerifier = false;
if (is_string($codeVerifier)) {
$validCodeVerifier = (\Safe\preg_match('/^[A-Za-z0-9\-._~]{43,128}$/', $codeVerifier) === 1);
}
$validClientSecret = false;
if (is_string($clientSecret)) {
$validClientSecret = (\Safe\preg_match('/^.{10,}$/', $clientSecret) === 1);
}
if ($clientID && $validClientSecret && $validCodeVerifier && $configState !== '' && $configState === $state) {
$openprojectUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
$result = $this->openprojectAPIService->requestOAuthAccessToken($openprojectUrl, [
'client_id' => $clientID,
'client_secret' => $clientSecret,
'code' => $code,
'redirect_uri' => openprojectAPIService::getOauthRedirectUrl($this->urlGenerator),
'grant_type' => 'authorization_code',
'code_verifier' => $codeVerifier
], 'POST');
if (isset($result['access_token']) && isset($result['refresh_token'])) {
$accessToken = $result['access_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'token', $accessToken);
$refreshToken = $result['refresh_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'refresh_token', $refreshToken);
// get user info
// ToDo check response for errors
$this->storeUserInfo();
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'oauth_connection_result', 'success'
);
return new RedirectResponse($newUrl);
}
$error = '';
if (!isset($result['access_token'])) {
$error = $this->l->t('Error getting OAuth access token');
} elseif (!isset($result['refresh_token'])) {
$error = $this->l->t('Error getting OAuth refresh token');
}
if (isset($result['error'])) {
$error = $error . '. ' . $result['error'];
}
$result = $error;
} else {
if (!$validCodeVerifier) {
$this->logger->error('invalid OAuth code verifier', ['app' => $this->appName]);
}
if (!$validClientSecret) {
$this->logger->error('invalid OAuth client secret', ['app' => $this->appName]);
}
$result = $this->l->t('Error during OAuth exchanges');
}
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'oauth_connection_result', 'error'
);
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'oauth_connection_error_message', $result
);
return new RedirectResponse($newUrl);
}
/**
* @return array{error?: string, user_name?: string, statusCode?: int}
*/
private function storeUserInfo(): array {
$info = $this->openprojectAPIService->request($this->userId, 'users/me');
if (isset($info['lastName'], $info['firstName'], $info['id'])) {
$fullName = $info['firstName'] . ' ' . $info['lastName'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_id', $info['id']);
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_name', $fullName);
return ['user_name' => $fullName];
} else {
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_id');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name');
if (isset($info['statusCode']) && $info['statusCode'] === 404) {
$info['error'] = 'Not found';
} else {
if (!isset($info['error'])) {
$info['error'] = 'Invalid token';
}
}
return $info;
}
}
/**
* @return DataResponse
*/
public function autoOauthCreation(): DataResponse {
$this->deleteOauthClient();
$opUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url', '');
$clientInfo = $this->oauthService->createNcOauthClient('OpenProject client', rtrim($opUrl, '/') .'/oauth_clients/%s/callback');
$this->config->setAppValue(Application::APP_ID, 'nc_oauth_client_id', $clientInfo['id']);
return new DataResponse($clientInfo);
}
private function deleteOauthClient(): void {
$oauthClientInternalId = $this->config->getAppValue(
Application::APP_ID, 'nc_oauth_client_id', ''
);
if ($oauthClientInternalId !== '') {
$id = (int) $oauthClientInternalId;
$this->oauthService->deleteClient($id);
$this->config->deleteAppValue(Application::APP_ID, 'nc_oauth_client_id');
}
}
/**
* @NoCSRFRequired
* @NoAdminRequired
* @PublicPage
*
* @return DataResponse
*/
public function checkConfig(): DataResponse {
return new DataResponse([
'user_id' => $this->userId ?? '',
'authorization_header' => $_SERVER['HTTP_AUTHORIZATION'],
]);
}
}