Skip to content

Commit 66f35dc

Browse files
committed
Added OIDC key type
1 parent e9891b9 commit 66f35dc

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ drush pm:install os2web_key
1515

1616
`@todo`
1717

18+
### OpenID Connect
19+
20+
`@todo`
21+
1822
## Example
1923

2024
`@todo`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Drupal\os2web_key\Plugin\KeyInput;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\key\Plugin\KeyInputBase;
7+
use Drupal\os2web_key\Plugin\KeyType\OidcKeyType;
8+
9+
/**
10+
* Input for OpenID Connect authentication.
11+
*
12+
* @KeyInput(
13+
* id = "os2web_key_oidc",
14+
* label = @Translation("OpenID Connect (OIDC)")
15+
* )
16+
*/
17+
class OidcKeyInput extends KeyInputBase {
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function defaultConfiguration() {
23+
return [
24+
OidcKeyType::DISCOVERY_URL => '',
25+
OidcKeyType::CLIENT_ID => '',
26+
OidcKeyType::CLIENT_SECRET => '',
27+
];
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
34+
$form[OidcKeyType::DISCOVERY_URL] = [
35+
'#type' => 'url',
36+
'#title' => $this->t('Discovery url'),
37+
'#default_value' => $this->configuration[OidcKeyType::DISCOVERY_URL],
38+
'#required' => TRUE,
39+
];
40+
41+
$form[OidcKeyType::CLIENT_ID] = [
42+
'#type' => 'textfield',
43+
'#title' => $this->t('Client ID'),
44+
'#default_value' => $this->configuration[OidcKeyType::CLIENT_ID],
45+
'#required' => TRUE,
46+
];
47+
48+
$form[OidcKeyType::CLIENT_SECRET] = [
49+
'#type' => 'textfield',
50+
'#title' => $this->t('Client Secret'),
51+
'#default_value' => $this->configuration[OidcKeyType::CLIENT_SECRET],
52+
'#required' => TRUE,
53+
];
54+
55+
return $form;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function processSubmittedKeyValue(FormStateInterface $form_state) {
62+
$values = $form_state->getValues();
63+
return [
64+
'submitted' => $values,
65+
'processed_submitted' => $values,
66+
];
67+
}
68+
69+
}

src/Plugin/KeyType/OidcKeyType.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Drupal\os2web_key\Plugin\KeyType;
4+
5+
use Drupal\Component\Serialization\Json;
6+
use Drupal\Core\Form\FormStateInterface;
7+
use Drupal\key\Plugin\KeyTypeBase;
8+
use Drupal\key\Plugin\KeyTypeMultivalueInterface;
9+
10+
/**
11+
* Defines a custom key type for OpenID Connect authentication.
12+
*
13+
* @KeyType(
14+
* id = "os2web_key_oidc",
15+
* label = @Translation("OpenID Connect (OIDC)"),
16+
* description = @Translation("A set of credentials for a OpenID Connect."),
17+
* group = "authentication",
18+
* key_value = {
19+
* "plugin" = "os2web_key_oidc",
20+
* "accepted" = FALSE,
21+
* },
22+
* multivalue = {
23+
* "enabled" = true,
24+
* "fields" = {
25+
* "discovery_url" = {
26+
* "label" = @Translation("Discovery url"),
27+
* "required" = true
28+
* },
29+
* "client_id" = {
30+
* "label" = @Translation("Client ID"),
31+
* "required" = true
32+
* },
33+
* "client_secret" = {
34+
* "label" = @Translation("Client secret"),
35+
* "required" = true
36+
* },
37+
* }
38+
* }
39+
* )
40+
*/
41+
class OidcKeyType extends KeyTypeBase implements KeyTypeMultivalueInterface {
42+
public const DISCOVERY_URL = 'discovery_url';
43+
public const CLIENT_ID = 'client_id';
44+
public const CLIENT_SECRET = 'client_secret';
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public static function generateKeyValue(array $configuration) {
50+
return Json::encode($configuration);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function validateKeyValue(array $form, FormStateInterface $form_state, $key_value): void {
57+
if (empty($key_value)) {
58+
$form_state->setError($form, $this->t('The key value is empty.'));
59+
return;
60+
}
61+
62+
$definition = $this->getPluginDefinition();
63+
$fields = $definition['multivalue']['fields'];
64+
65+
foreach ($fields as $id => $field) {
66+
if (!is_array($field)) {
67+
$field = ['label' => $field];
68+
}
69+
70+
if (isset($field['required']) && $field['required'] === FALSE) {
71+
continue;
72+
}
73+
74+
if (!isset($key_value[$id])) {
75+
$form_state->setError($form, $this->t('The key value is missing the field %field.', ['%field' => $id]));
76+
}
77+
elseif (empty($key_value[$id])) {
78+
$form_state->setError($form, $this->t('The key value field %field is empty.', ['%field' => $id]));
79+
}
80+
}
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function serialize(array $array) {
87+
return Json::encode($array);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function unserialize($value) {
94+
return Json::decode($value);
95+
}
96+
97+
}

0 commit comments

Comments
 (0)