Skip to content

Commit 862dfa6

Browse files
authored
Merge pull request #24 from itk-dev/feature/allow-key-and-form-configuration
Allowed both key and form configuration of providers
2 parents cfe9500 + 9318ed3 commit 862dfa6

3 files changed

Lines changed: 121 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
## [Unreleased]
1010

1111
* [PR-20](https://github.com/itk-dev/os2forms_nemlogin_openid_connect/pull/20)
12-
Added support for Key
12+
Added support for Key.
1313

1414
## [2.2.1]
1515

src/Controller/OpenIDConnectController.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,27 @@ public function main(string $id) {
147147
private function getOpenIdConfigurationProvider(): OpenIdConfigurationProvider {
148148
$pluginConfiguration = $this->plugin->getConfiguration();
149149

150-
try {
151-
$keyId = $pluginConfiguration[OpenIDConnect::KEY] ?? '';
152-
$key = $this->keyRepository->getKey($keyId);
153-
if (NULL === $key) {
154-
throw new \RuntimeException(sprintf('Cannot get key %s', $keyId));
155-
}
156-
[
157-
OidcKeyType::DISCOVERY_URL => $discoveryUrl,
158-
OidcKeyType::CLIENT_ID => $clientId,
159-
OidcKeyType::CLIENT_SECRET => $clientSecret,
160-
] = $this->keyHelper->getOidcValues($key);
150+
if (OpenIDConnect::PROVIDER_TYPE_KEY === $pluginConfiguration[OpenIDConnect::PROVIDER_TYPE_KEY]) {
151+
$discoveryUrl = $pluginConfiguration[OpenIDConnect::DISCOVERY_URL];
152+
$clientId = $pluginConfiguration[OpenIDConnect::CLIENT_ID];
153+
$clientSecret = $pluginConfiguration[OpenIDConnect::CLIENT_SECRET];
161154
}
162-
catch (\Exception $e) {
163-
throw new AuthenticationException('Cannot get client id and secret', $e->getCode(), $e);
155+
else {
156+
try {
157+
$keyId = $pluginConfiguration[OpenIDConnect::KEY] ?? '';
158+
$key = $this->keyRepository->getKey($keyId);
159+
if (NULL === $key) {
160+
throw new \RuntimeException(sprintf('Cannot get key %s', $keyId));
161+
}
162+
[
163+
OidcKeyType::DISCOVERY_URL => $discoveryUrl,
164+
OidcKeyType::CLIENT_ID => $clientId,
165+
OidcKeyType::CLIENT_SECRET => $clientSecret,
166+
] = $this->keyHelper->getOidcValues($key);
167+
}
168+
catch (\Exception $e) {
169+
throw new AuthenticationException('Cannot get client id and secret', $e->getCode(), $e);
170+
}
164171
}
165172

166173
$providerOptions = [

src/Plugin/os2web/NemloginAuthProvider/OpenIDConnect.php

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ class OpenIDConnect extends AuthProviderBase {
3232
use LoggerTrait;
3333
use LoggerAwareTrait;
3434

35-
/**
36-
* Session name for storing OIDC user token.
37-
*/
38-
private const SESSION_TOKEN = 'os2forms_nemlogin_openid_connect.user_token';
35+
public const SESSION_TOKEN = 'os2forms_nemlogin_openid_connect.user_token';
36+
public const KEY = 'nemlogin_openid_connect_key';
37+
public const SECRET_PROVIDER = 'secret_provider';
38+
public const PROVIDER_TYPE_FORM = 'form';
39+
public const PROVIDER_TYPE_KEY = 'key';
40+
public const FETCH_ONCE = 'nemlogin_openid_connect_fetch_once';
41+
public const POST_LOGOUT_REDIRECT_URI = 'nemlogin_openid_connect_post_logout_redirect_uri';
42+
public const USER_CLAIMS = 'nemlogin_openid_connect_user_claims';
43+
public const DISCOVERY_URL = 'nemlogin_openid_connect_discovery_url';
44+
public const CLIENT_ID = 'nemlogin_openid_connect_client_id';
45+
public const CLIENT_SECRET = 'nemlogin_openid_connect_client_secret';
46+
3947

4048
/**
4149
* Fetch only mode flag.
@@ -115,8 +123,8 @@ public static function create(ContainerInterface $container, array $configuratio
115123
* {@inheritdoc}
116124
*/
117125
public function isInitialized() {
118-
$configuration = $configuration = $this->getConfiguration();
119-
if (!empty($configuration['nemlogin_openid_connect_discovery_url'])) {
126+
$configuration = $this->getConfiguration();
127+
if (!empty($configuration[self::DISCOVERY_URL])) {
120128
return TRUE;
121129
}
122130

@@ -265,11 +273,6 @@ public function fetchValue($key) {
265273
return $value;
266274
}
267275

268-
public const KEY = 'nemlogin_openid_connect_key';
269-
public const FETCH_ONCE = 'nemlogin_openid_connect_fetch_once';
270-
public const POST_LOGOUT_REDIRECT_URI = 'nemlogin_openid_connect_post_logout_redirect_uri';
271-
public const USER_CLAIMS = 'nemlogin_openid_connect_user_claims';
272-
273276
/**
274277
* {@inheritdoc}
275278
*
@@ -280,6 +283,10 @@ public function fetchValue($key) {
280283
*/
281284
public function defaultConfiguration() {
282285
return parent::defaultConfiguration() + [
286+
self::SECRET_PROVIDER => self::PROVIDER_TYPE_FORM,
287+
self::DISCOVERY_URL => '',
288+
self::CLIENT_ID => '',
289+
self::CLIENT_SECRET => '',
283290
self::KEY => '',
284291
self::FETCH_ONCE => '',
285292
self::POST_LOGOUT_REDIRECT_URI => '',
@@ -294,15 +301,79 @@ public function defaultConfiguration() {
294301
* @phpstan-return array<string, mixed>
295302
*/
296303
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
304+
305+
$form[self::SECRET_PROVIDER] = [
306+
'#type' => 'select',
307+
'#title' => $this->t('Provider'),
308+
'#options' => [
309+
self::PROVIDER_TYPE_FORM => $this->t('Form'),
310+
self::PROVIDER_TYPE_KEY => $this->t('Key'),
311+
],
312+
'#default_value' => $this->configuration[self::SECRET_PROVIDER] ?? self::PROVIDER_TYPE_FORM,
313+
];
314+
315+
$form[self::DISCOVERY_URL] = [
316+
'#type' => 'textfield',
317+
'#title' => $this->t('OpenID Connect Discovery url'),
318+
// Our urls are very long.
319+
'#maxlength' => 256,
320+
'#default_value' => $this->configuration[self::DISCOVERY_URL] ?? NULL,
321+
'#description' => $this->t('OpenID Connect Discovery url (cf. <a href="https://swagger.io/docs/specification/authentication/openid-connect-discovery/">https://swagger.io/docs/specification/authentication/openid-connect-discovery/</a>)'),
322+
'#states' => [
323+
'visible' => [
324+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
325+
],
326+
'required' => [
327+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
328+
],
329+
],
330+
];
331+
332+
$form[self::CLIENT_ID] = [
333+
'#type' => 'textfield',
334+
'#title' => $this->t('Client id'),
335+
'#default_value' => $this->configuration[self::CLIENT_ID] ?? NULL,
336+
'#states' => [
337+
'visible' => [
338+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
339+
],
340+
'required' => [
341+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
342+
],
343+
],
344+
];
345+
346+
$form[self::CLIENT_SECRET] = [
347+
'#type' => 'textfield',
348+
'#title' => $this->t('Client secret'),
349+
'#default_value' => $this->configuration[self::CLIENT_SECRET] ?? NULL,
350+
'#states' => [
351+
'visible' => [
352+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
353+
],
354+
'required' => [
355+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_FORM]],
356+
],
357+
],
358+
];
359+
297360
$form[self::KEY] = [
298361
'#type' => 'key_select',
299362
'#key_filters' => [
300363
'type' => 'os2web_key_oidc',
301364
],
302365
'#title' => $this->t('Key'),
303-
'#required' => TRUE,
304366
'#default_value' => $this->configuration[self::KEY] ?? NULL,
367+
'#states' => [
368+
'visible' => [
369+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_KEY]],
370+
],
371+
'required' => [
372+
[':input[name="secret_provider"]' => ['value' => self::PROVIDER_TYPE_KEY]],
373+
],
374+
],
305375
];
376+
306377
$form[self::FETCH_ONCE] = [
307378
'#type' => 'checkbox',
308379
'#title' => $this->t('Use fetch only mode.'),
@@ -334,9 +405,20 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
334405
* @phpstan-param array<string, mixed> $form
335406
*/
336407
public function validateConfigurationForm(array &$form, FormStateInterface $form_state): void {
337-
$url = $form_state->getValue(self::POST_LOGOUT_REDIRECT_URI);
408+
409+
if (self::PROVIDER_TYPE_FORM === $form_state->getValue(self::SECRET_PROVIDER)) {
410+
411+
$discoveryUrl = $form_state->getValue(self::DISCOVERY_URL);
412+
413+
if (!UrlHelper::isValid($discoveryUrl, TRUE)) {
414+
$form_state->setErrorByName(self::DISCOVERY_URL, $this->t('Discovery url is not valid'));
415+
}
416+
}
417+
418+
$redirectUrl = $form_state->getValue(self::POST_LOGOUT_REDIRECT_URI);
419+
338420
try {
339-
UrlHelper::isExternal($url) ? Url::fromUri($url) : Url::fromUserInput($url);
421+
UrlHelper::isExternal($redirectUrl) ? Url::fromUri($redirectUrl) : Url::fromUserInput($redirectUrl);
340422
}
341423
catch (\Exception $exception) {
342424
$form_state->setErrorByName(self::POST_LOGOUT_REDIRECT_URI, $this->t('Post logout redirect url is not valid (@message)', ['@message' => $exception->getMessage()]));
@@ -381,6 +463,10 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
381463
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
382464
$configuration = $this->getConfiguration();
383465

466+
$configuration[self::SECRET_PROVIDER] = $form_state->getValue(self::SECRET_PROVIDER);
467+
$configuration[self::DISCOVERY_URL] = $form_state->getValue(self::DISCOVERY_URL);
468+
$configuration[self::CLIENT_ID] = $form_state->getValue(self::CLIENT_ID);
469+
$configuration[self::CLIENT_SECRET] = $form_state->getValue(self::CLIENT_SECRET);
384470
$configuration[self::KEY] = $form_state->getValue(self::KEY);
385471
$configuration[self::FETCH_ONCE] = $form_state->getValue(self::FETCH_ONCE);
386472
$configuration[self::POST_LOGOUT_REDIRECT_URI] = $form_state->getValue(self::POST_LOGOUT_REDIRECT_URI);

0 commit comments

Comments
 (0)