-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenIDConnectController.php
More file actions
489 lines (431 loc) · 14.5 KB
/
Copy pathOpenIDConnectController.php
File metadata and controls
489 lines (431 loc) · 14.5 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
namespace Drupal\os2forms_nemlogin_openid_connect\Controller;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\LocalRedirectResponse;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\key\KeyRepositoryInterface;
use Drupal\os2forms_nemlogin_openid_connect\Exception\AuthenticationException;
use Drupal\os2forms_nemlogin_openid_connect\Plugin\os2web\NemloginAuthProvider\OpenIDConnect;
use Drupal\os2web_key\KeyHelper;
use Drupal\os2web_key\Plugin\KeyType\OidcKeyType;
use Drupal\os2web_nemlogin\Service\AuthProviderService;
use ItkDev\OpenIdConnect\Security\OpenIdConfigurationProvider;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Controller used for actual OpenID Connect authentication.
*/
class OpenIDConnectController implements ContainerInjectionInterface {
use LoggerAwareTrait;
use LoggerTrait;
use StringTranslationTrait;
/**
* Session name for storing auth provider login location.
*/
private const SESSION_LOGIN_LOCATION = 'os2forms_nemlogin_openid_connect.login_location';
/**
* Session name for storing OAuth2 state.
*/
private const SESSION_STATE = 'os2forms_nemlogin_openid_connect.oauth2state';
/**
* Session name for storing OAuth2 nonce.
*/
private const SESSION_NONCE = 'os2forms_nemlogin_openid_connect.oauth2nonce';
/**
* Session name for storing is token.
*/
private const SESSION_ID_TOKEN = 'os2forms_nemlogin_openid_connect.id_token';
/**
* Name of login destination query parameter.
*
* Important: Must not be 'destination'!
*/
public const QUERY_LOCATION_NAME = 'login-destination';
/**
* The plugin.
*
* @var \Drupal\os2forms_nemlogin_openid_connect\Plugin\os2web\NemloginAuthProvider\OpenIDConnect
*/
private $plugin;
/**
* The constructor.
*/
public function __construct(
private readonly AuthProviderService $authProviderService,
private readonly RequestStack $requestStack,
private readonly SessionInterface $session,
private readonly CacheItemPoolInterface $cacheItemPool,
private readonly LanguageManagerInterface $languageManager,
private readonly RendererInterface $renderer,
private readonly KeyRepositoryInterface $keyRepository,
private readonly KeyHelper $keyHelper,
LoggerInterface $logger,
) {
$this->setLogger($logger);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('os2web_nemlogin.auth_provider'),
$container->get('request_stack'),
$container->get('session'),
$container->get('drupal_psr6_cache.cache_item_pool'),
$container->get('language_manager'),
$container->get('renderer'),
$container->get('key.repository'),
$container->get(KeyHelper::class),
$container->get('logger.channel.os2forms_nemlogin_openid_connect'),
);
}
/**
* The main controller action.
*
* Delegates to other functions for actual handling of requests.
*
* @return array|Response
* The renderable array or response.
*
* @phpstan-return array<string, mixed>|Response
*/
public function main(string $id) {
$this->initialize($id);
try {
$request = $this->requestStack->getCurrentRequest();
if (NULL !== ($location = $request->query->get(self::QUERY_LOCATION_NAME))) {
$this->setLoginLocation($location);
}
if ($request->query->has('error')) {
return $this->displayError($request->query->get('error'), $request->query->get('error_description'));
}
if ($request->query->has('state')) {
return $this->process();
}
return $this->start();
}
catch (\Exception $exception) {
$error = $exception->getMessage();
$description = $exception instanceof AuthenticationException ? $exception->getDescription() : NULL;
return $this->displayError($error, $description);
}
}
/**
* Get an OpenIdConfigurationProvider instance.
*/
private function getOpenIdConfigurationProvider(): OpenIdConfigurationProvider {
$pluginConfiguration = $this->plugin->getConfiguration();
if (OpenIDConnect::PROVIDER_TYPE_KEY === $pluginConfiguration[OpenIDConnect::PROVIDER_TYPE_KEY]) {
$discoveryUrl = $pluginConfiguration[OpenIDConnect::DISCOVERY_URL];
$clientId = $pluginConfiguration[OpenIDConnect::CLIENT_ID];
$clientSecret = $pluginConfiguration[OpenIDConnect::CLIENT_SECRET];
}
else {
try {
$keyId = $pluginConfiguration[OpenIDConnect::KEY] ?? '';
$key = $this->keyRepository->getKey($keyId);
if (NULL === $key) {
throw new \RuntimeException(sprintf('Cannot get key %s', $keyId));
}
[
OidcKeyType::DISCOVERY_URL => $discoveryUrl,
OidcKeyType::CLIENT_ID => $clientId,
OidcKeyType::CLIENT_SECRET => $clientSecret,
] = $this->keyHelper->getOidcValues($key);
}
catch (\Exception $e) {
throw new AuthenticationException('Cannot get client id and secret', $e->getCode(), $e);
}
}
$providerOptions = [
'redirectUri' => $this->getRedirectUri(),
'openIDConnectMetadataUrl' => $discoveryUrl,
'cacheItemPool' => $this->cacheItemPool,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'localTestMode' => FALSE,
'allowHttp' => (bool) ($this->getSettings()['allow_http'] ?? FALSE),
];
return new OpenIdConfigurationProvider($providerOptions);
}
/**
* Get redirect URI.
*/
private function getRedirectUri(): string {
return Url::fromRoute(
'os2forms_nemlogin_openid_connect.openid_connect_authenticate',
[
'id' => $this->getPluginId(),
],
[
'absolute' => TRUE,
'language' => $this->languageManager->getLanguage(LanguageInterface::LANGCODE_NOT_APPLICABLE),
]
)->toString(TRUE)->getGeneratedUrl();
}
/**
* Set session attribute value.
*
* @param string $name
* The session attribute name.
* @param mixed $value
* The session attribute value.
*/
private function setSessionValue(string $name, $value): void {
$this->session->set($name, $value);
}
/**
* Get and remove session attribute value.
*
* By default this function removes the value from the session, but it's
* possible to just peek at the value of need be.
*
* @param string $name
* The session attribute name.
* @param bool $peek
* If set, the session value will not be removed.
*
* @return mixed
* The session value.
*/
private function getSessionValue(string $name, bool $peek = FALSE) {
$value = $this->session->get($name);
if (!$peek) {
$this->session->remove($name);
}
return $value;
}
/**
* {@inheritdoc}
*
* @phpstan-param mixed $level
* @phpstan-param string $message
* @phpstan-param array<string, mixed> $context
*/
public function log($level, $message, array $context = []): void {
if (NULL !== $this->logger) {
$this->logger->log($level, $message, $context);
}
}
/**
* Start OpenID Connect flow.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*/
private function start(): Response {
$provider = $this->getOpenIdConfigurationProvider();
$state = $provider->generateState();
$nonce = $provider->generateNonce();
$this->setSessionValue(self::SESSION_STATE, $state);
$this->setSessionValue(self::SESSION_NONCE, $nonce);
$options = [
'state' => $state,
'nonce' => $nonce,
'response_type' => 'code',
'scope' => 'openid email profile',
];
$authorizationUrl = $this->isLocalTestMode()
? Url::fromRoute('os2forms_nemlogin_openid_connect.openid_connect_authenticate', $options + [
'id' => $this->getPluginId(),
'test' => TRUE,
])->toString(TRUE)->getGeneratedUrl()
: $provider->getAuthorizationUrl($options);
$this->setSessionValue(self::SESSION_STATE, $provider->getState());
return new TrustedRedirectResponse($authorizationUrl);
}
/**
* End OpenID Connect session.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*/
public function endSession(string $id): Response {
$this->initialize($id);
$provider = $this->getOpenIdConfigurationProvider();
$postLogoutRedirectUri = $this->getPostLogoutRedirectUri();
$endSessionUrl = $this->isLocalTestMode()
? $postLogoutRedirectUri
: $provider->getEndSessionUrl($postLogoutRedirectUri, NULL, $this->getSessionValue(self::SESSION_ID_TOKEN));
return new TrustedRedirectResponse($endSessionUrl);
}
/**
* Get post logout redirect uri.
*/
private function getPostLogoutRedirectUri(): string {
try {
$pluginConfiguration = $this->plugin->getConfiguration();
$url = $pluginConfiguration[OpenIDConnect::POST_LOGOUT_REDIRECT_URI] ?? '/';
$options = [
'absolute' => TRUE,
'path_processing' => FALSE,
];
$url = UrlHelper::isExternal($url)
? Url::fromUri($url, $options)
: Url::fromUserInput($url, $options);
return $url->toString(TRUE)->getGeneratedUrl();
}
catch (\Exception $exception) {
// Fallback if all other things fail.
return '/';
}
}
/**
* Is local test mode?
*/
private function isLocalTestMode(): bool {
return (bool) ($this->getSettings()['local_test_mode'] ?? FALSE);
}
/**
* Get local test users.
*
* @phpstan-return array<string, mixed>
*/
private function getLocalTestUsers(): array {
return (array) ($this->getSettings()['local_test_users'] ?? []);
}
/**
* Get this module's settings.
*
* @phpstan-return array<string, mixed>
*/
private function getSettings(): array {
$settings = Settings::get('os2forms_nemlogin_openid_connect', NULL);
if (!is_array($settings)) {
$settings = [];
}
// Merge in plugin specific settings if any.
return isset($settings[$this->getPluginId()]) && is_array($settings[$this->getPluginId()])
? array_merge($settings, $settings[$this->getPluginId()])
: $settings;
}
/**
* Process OpenID Connect response.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function process(): Response {
$request = $this->requestStack->getCurrentRequest();
if ($this->isLocalTestMode() && (bool) $request->get('test')) {
$users = $this->getLocalTestUsers();
$userId = $request->get('user');
if (isset($users[$userId])) {
$token = $users[$userId] + ['local_test' => TRUE];
}
else {
$renderable = [
'#theme' => 'os2forms_nemlogin_openid_connect_local_test_users',
'#users' => $users,
'#plugin' => $this->plugin,
'#query' => ['id' => $this->getPluginId()] + $request->query->all(),
];
return new Response($this->renderer->renderInIsolation($renderable));
}
}
else {
if (!$request->query->has('state')) {
$this->error('Missing state in response', ['query' => $request->query->all()]);
throw new BadRequestHttpException('Missing state in response');
}
$state = $this->getSessionValue(self::SESSION_STATE);
if ($state !== $request->query->get('state')) {
$this->error('Invalid state', ['state' => $request->query->get('state')]);
throw new BadRequestHttpException('Invalid state');
}
$provider = $this->getOpenIdConfigurationProvider();
$idToken = match (TRUE) {
$request->query->has('code') => $provider->getIdToken($request->query->get('code')),
$request->query->has('id_token') => $request->query->get('id_token'),
default => throw new BadRequestHttpException('Missing code or id_token in response')
};
$token = (array) $provider->validateIdToken($idToken, $this->getSessionValue(self::SESSION_NONCE));
$this->setSessionValue(self::SESSION_ID_TOKEN, $idToken);
}
// Store the token for use by the authentication plugin.
$this->plugin->setToken($token);
$location = $this->getLoginLocation();
return new LocalRedirectResponse($location);
}
/**
* Set the location of where login flow is started.
*/
private function setLoginLocation(string $location): self {
$this->setSessionValue(self::SESSION_LOGIN_LOCATION, $location);
return $this;
}
/**
* Get the location of where login flow was started.
*
* Fall back to the frontpage.
*
* @return string
* The login location.
*/
public function getLoginLocation(): string {
$location = $this->getSessionValue(self::SESSION_LOGIN_LOCATION);
return $location ?? Url::fromRoute('<front>')->toString(TRUE)->getGeneratedUrl();
}
/**
* Display an error.
*
* @return array
* The render array.
*
* @phpstan-return array<string, mixed>
*/
private function displayError(string $message, ?string $description = NULL): array {
$request = $this->requestStack->getCurrentRequest();
$this->error('Error', [
'query' => $request->query->all(),
]);
return [
'error' => [
'#markup' => $message,
'#prefix' => '<h1>',
'#suffix' => '</h1>',
],
'error_description' => [
'#markup' => $description,
'#prefix' => '<pre>',
'#suffix' => '</pre>',
'#access' => NULL !== $description,
],
'authenticate' => [
'#type' => 'link',
'#title' => $this->t('Try again'),
'#url' => $this->getLoginLocation(),
],
];
}
/**
* Initialize.
*/
private function initialize(string $id): void {
$plugin = $this->authProviderService->getPluginInstance($id);
assert($plugin instanceof OpenIDConnect);
$this->plugin = $plugin;
}
/**
* Get plugin id.
*/
private function getPluginId(): string {
return $this->plugin->getPluginId();
}
}