-
Notifications
You must be signed in to change notification settings - Fork 6
/
LogoutController.php
281 lines (261 loc) · 9.87 KB
/
LogoutController.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
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
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022-2024 Thorsten Jagel <dev@jagel.net>
*
* @author Thorsten Jagel <dev@jagel.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\OIDCIdentityProvider\Controller;
require_once __DIR__ . '/../../vendor/autoload.php';
use OCA\OIDCIdentityProvider\Exceptions\ClientNotFoundException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IUserSession;
use OCP\IUserManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IInitialStateService;
use OC_App;
use OCP\AppFramework\Utility\ITimeFactory;
use OCA\OIDCIdentityProvider\Db\AccessTokenMapper;
use OCA\OIDCIdentityProvider\Db\AccessToken;
use OCA\OIDCIdentityProvider\Db\ClientMapper;
use OCA\OIDCIdentityProvider\Db\LogoutRedirectUri;
use OCA\OIDCIdentityProvider\Db\LogoutRedirectUriMapper;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use Psr\Log\LoggerInterface;
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use Firebase\JWT\SignatureInvalidException;
use Firebase\JWT\BeforeValidException;
use Firebase\JWT\ExpiredException;
use DomainException;
use InvalidArgumentException;
use UnexpectedValueException;
class LogoutController extends ApiController
{
/** @var IURLGenerator */
private $urlGenerator;
/** @var ClientMapper */
private $clientMapper;
/** @var AccessTokenMapper */
private $accessTokenMapper;
/** @var LogoutRedirectUriMapper */
private $logoutRedirectUriMapper;
/** @var ISession */
private $session;
/** @var IL10N */
private $l;
/** @var ITimeFactory */
private $time;
/** @var IUserSession */
private $userSession;
/** @var IUserManager */
private $userManager;
/** @var IAppConfig */
private $appConfig;
/** @var LoggerInterface */
private $logger;
/**
* @param string $appName
* @param IRequest $request
* @param IURLGenerator $urlGenerator
* @param ClientMapper $clientMapper
* @param ISession $session
* @param IL10N $l
* @param ITimeFactory $time
* @param IUserSession $userSession
* @param IUserManager $userManager
* @param AccessTokenMapper $accessTokenMapper
* @param LogoutRedirectUriMapper $logoutRedirectUriMapper
* @param IAppConfig $appConfig
* @param LoggerInstance $logger
*/
public function __construct(
string $appName,
IRequest $request,
IURLGenerator $urlGenerator,
ClientMapper $clientMapper,
ISession $session,
IL10N $l,
ITimeFactory $time,
IUserSession $userSession,
IUserManager $userManager,
AccessTokenMapper $accessTokenMapper,
LogoutRedirectUriMapper $logoutRedirectUriMapper,
IAppConfig $appConfig,
LoggerInterface $logger
)
{
parent::__construct($appName, $request);
$this->urlGenerator = $urlGenerator;
$this->clientMapper = $clientMapper;
$this->session = $session;
$this->l = $l;
$this->time = $time;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->accessTokenMapper = $accessTokenMapper;
$this->logoutRedirectUriMapper = $logoutRedirectUriMapper;
$this->appConfig = $appConfig;
$this->logger = $logger;
}
/**
* @PublicPage
* @NoCSRFRequired
* @UseSession
* @BruteForceProtection(action=oidc_logout)
*
* @param string $client_id
* @param string $refresh_token
* @return Response
*/
#[BruteForceProtection(action: 'oidc_logout')]
public function logout(
$client_id, // Optional
$refresh_token, // Not standardized - deprecated will not be used any more
$id_token_hint, // Recommended to be used
$post_logout_redirect_uri // Optional url to be redirected to after logout
): Response
{
$userId = null;
if ($id_token_hint) {
// check Token to get user id
$oidcKey = [
'kty' => 'RSA',
'use' => 'sig',
'key_ops' => [ 'verify' ],
'alg' => 'RS256',
'kid' => $this->appConfig->getAppValue('kid'),
'n' => $this->appConfig->getAppValue('public_key_n'),
'e' => $this->appConfig->getAppValue('public_key_e'),
];
$jwks = [
'keys' => [
$oidcKey
]
];
$decodedJwt = null;
try {
$decodedStdClass = JWT::decode($id_token_hint, JWK::parseKeySet($jwks));
$decodedJwt = (array) $decodedStdClass;
} catch (InvalidArgumentException $e) {
// provided key/key-array is empty or malformed.
$this->logger->error('Provided key/key-array is empty or malformed.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided key/key-array is empty or malformed.'
], Http::STATUS_UNAUTHORIZED);
} catch (DomainException $e) {
// provided algorithm is unsupported OR
// provided key is invalid OR
// unknown error thrown in openSSL or libsodium OR
// libsodium is required but not available.
$this->logger->error('Provided algorithm is unsupported OR provided key is invalid OR unknown error thrown in openSSL or libsodium OR libsodium is required but not available.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided algorithm is unsupported OR provided key is invalid OR unknown error thrown in openSSL or libsodium OR libsodium is required but not available.'
], Http::STATUS_UNAUTHORIZED);
} catch (SignatureInvalidException $e) {
// provided JWT signature verification failed.
$this->logger->error('Provided JWT signature verification failed.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided JWT signature verification failed.'
], Http::STATUS_UNAUTHORIZED);
} catch (BeforeValidException $e) {
// provided JWT is trying to be used before "nbf" claim OR
// provided JWT is trying to be used before "iat" claim.
$this->logger->error('Provided JWT is trying to be used before "nbf" claim OR provided JWT is trying to be used before "iat" claim.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided JWT is trying to be used before "nbf" claim OR provided JWT is trying to be used before "iat" claim.'
], Http::STATUS_UNAUTHORIZED);
} catch (ExpiredException $e) {
// provided JWT is trying to be used after "exp" claim.
// $this->logger->error('Provided JWT is trying to be used after "exp" claim.');
// return new JSONResponse([
// 'error' => 'invalid_jwt',
// 'error_description' => 'Provided JWT is trying to be used after "exp" claim.'
// ], Http::STATUS_UNAUTHORIZED);
} catch (UnexpectedValueException $e) {
// provided JWT is malformed OR
// provided JWT is missing an algorithm / using an unsupported algorithm OR
// provided JWT algorithm does not match provided key OR
// provided key ID in key/key-array is empty or invalid.
$this->logger->error('Provided JWT is malformed OR provided JWT is missing an algorithm / using an unsupported algorithm OR provided JWT algorithm does not match provided key OR provided key ID in key/key-array is empty or invalid.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided JWT is malformed OR provided JWT is missing an algorithm / using an unsupported algorithm OR provided JWT algorithm does not match provided key OR provided key ID in key/key-array is empty or invalid.'
], Http::STATUS_UNAUTHORIZED);
}
if ($decodedJwt != null) {
$uid = $decodedJwt['preferred_username'];
$this->logger->notice('JWT token for uid ' . $uid . ' received.' );
// create user session for user with id perform login without pw
$user = $this->userManager->get($uid);
if (null === $user) {
$this->logger->error('Provided user in JWT is unknown.');
return new JSONResponse([
'error' => 'invalid_user',
'error_description' => 'Provided user in JWT is unknown.'
], Http::STATUS_UNAUTHORIZED);
}
$userId = $uid;
if ($client_id !== null && $client_id !== $decodedJwt['aud']) {
$this->logger->error('Provided client_id does not match to the one issued the JWT.');
return new JSONResponse([
'error' => 'invalid_jwt',
'error_description' => 'Provided client_id does not match to the one issued the JWT.'
], Http::STATUS_UNAUTHORIZED);
}
}
}
if ($this->userSession !== null && $this->userSession->isLoggedIn()) {
$userId = $this->userSession->getUser()->getUID();
// Logout user from session
$this->userSession->logout();
}
$this->accessTokenMapper->deleteByUserId($userId);
$this->logger->debug('Logout for user ' . $userId . ' performed.');
$defaultLogoutRedirectUrl = $this->urlGenerator->linkToRoute(
'core.login.showLoginForm',
[
]
);
if ($post_logout_redirect_uri) {
$logoutRedirectUris = $this->logoutRedirectUriMapper->getAll();
foreach ($logoutRedirectUris as $logoutRedirectUri) {
if (str_starts_with($post_logout_redirect_uri, $logoutRedirectUri->getRedirectUri())) {
return new RedirectResponse($post_logout_redirect_uri);
}
}
}
return new RedirectResponse($defaultLogoutRedirectUrl);
}
}