-
Notifications
You must be signed in to change notification settings - Fork 19
/
ConfigController.php
204 lines (189 loc) · 6.97 KB
/
ConfigController.php
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
<?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\AppFramework\Http\RedirectResponse;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\OpenProject\Service\OpenProjectAPIService;
use OCA\OpenProject\AppInfo\Application;
class ConfigController extends Controller {
/**
* @var IConfig
*/
private $config;
/**
* @var IURLGenerator
*/
private $urlGenerator;
/**
* @var IL10N
*/
private $l;
/**
* @var OpenProjectAPIService
*/
private $openprojectAPIService;
/**
* @var string|null
*/
private $userId;
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator,
IL10N $l,
OpenProjectAPIService $openprojectAPIService,
?string $userId) {
parent::__construct($appName, $request);
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->l = $l;
$this->openprojectAPIService = $openprojectAPIService;
$this->userId = $userId;
}
/**
* 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($values['token']);
} else {
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'token');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'login');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_id');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'refresh_token');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'last_notification_check');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'token_type');
$result = [
'user_name' => '',
];
}
}
if (isset($result['error'])) {
return new DataResponse($result, 401);
} else {
return new DataResponse($result);
}
}
/**
* set admin config values
*
* @param array<string, string> $values
* @return DataResponse
*/
public function setAdminConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setAppValue(Application::APP_ID, $key, trim($value));
}
return new DataResponse(1);
}
/**
* 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');
// anyway, reset state
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'oauth_state');
if ($clientID && $clientSecret && $configState !== '' && $configState === $state) {
$redirect_uri = $this->config->getUserValue($this->userId, Application::APP_ID, 'redirect_uri');
$openprojectUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url');
$result = $this->openprojectAPIService->requestOAuthAccessToken($openprojectUrl, [
'client_id' => $clientID,
'client_secret' => $clientSecret,
'code' => $code,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
], 'POST');
if (isset($result['access_token']) && isset($result['refresh_token'])) {
$accessToken = $result['access_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'token', $accessToken);
$this->config->setUserValue($this->userId, Application::APP_ID, 'token_type', 'oauth');
$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($accessToken);
return new RedirectResponse(
$this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'connected-accounts']) .
'?openprojectToken=success'
);
}
$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 {
$result = $this->l->t('Error during OAuth exchanges');
}
return new RedirectResponse(
$this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'connected-accounts']) .
'?openprojectToken=error&message=' . urlencode($result)
);
}
/**
* @param string $accessToken
* @return array{error?: string, user_name?: string, errorMesssage?: string}
*/
private function storeUserInfo(string $accessToken): array {
$tokenType = $this->config->getUserValue($this->userId, Application::APP_ID, 'token_type');
$refreshToken = $this->config->getUserValue($this->userId, Application::APP_ID, 'refresh_token');
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
$openprojectUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url');
if (!$openprojectUrl || !OpenProjectAPIService::validateOpenProjectURL($openprojectUrl)) {
return ['error' => 'OpenProject URL is invalid'];
}
$info = $this->openprojectAPIService->request($openprojectUrl, $accessToken, $tokenType, $refreshToken, $clientID, $clientSecret, $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['errorMessage'] = 'Not found';
} else {
$info['errorMessage'] = 'Invalid token';
}
return $info;
}
}
}