Skip to content

Commit

Permalink
Added OIDC key type
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed May 7, 2024
1 parent e9891b9 commit 66f35dc
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ drush pm:install os2web_key

`@todo`

### OpenID Connect

`@todo`

## Example

`@todo`
Expand Down
69 changes: 69 additions & 0 deletions src/Plugin/KeyInput/OidcKeyInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Drupal\os2web_key\Plugin\KeyInput;

use Drupal\Core\Form\FormStateInterface;
use Drupal\key\Plugin\KeyInputBase;
use Drupal\os2web_key\Plugin\KeyType\OidcKeyType;

/**
* Input for OpenID Connect authentication.
*
* @KeyInput(
* id = "os2web_key_oidc",
* label = @Translation("OpenID Connect (OIDC)")
* )
*/
class OidcKeyInput extends KeyInputBase {

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
OidcKeyType::DISCOVERY_URL => '',
OidcKeyType::CLIENT_ID => '',
OidcKeyType::CLIENT_SECRET => '',
];
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[OidcKeyType::DISCOVERY_URL] = [
'#type' => 'url',
'#title' => $this->t('Discovery url'),
'#default_value' => $this->configuration[OidcKeyType::DISCOVERY_URL],
'#required' => TRUE,
];

$form[OidcKeyType::CLIENT_ID] = [
'#type' => 'textfield',
'#title' => $this->t('Client ID'),
'#default_value' => $this->configuration[OidcKeyType::CLIENT_ID],
'#required' => TRUE,
];

$form[OidcKeyType::CLIENT_SECRET] = [
'#type' => 'textfield',
'#title' => $this->t('Client Secret'),
'#default_value' => $this->configuration[OidcKeyType::CLIENT_SECRET],
'#required' => TRUE,
];

return $form;
}

/**
* {@inheritdoc}
*/
public function processSubmittedKeyValue(FormStateInterface $form_state) {
$values = $form_state->getValues();
return [
'submitted' => $values,
'processed_submitted' => $values,
];
}

}
97 changes: 97 additions & 0 deletions src/Plugin/KeyType/OidcKeyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\os2web_key\Plugin\KeyType;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\key\Plugin\KeyTypeBase;
use Drupal\key\Plugin\KeyTypeMultivalueInterface;

/**
* Defines a custom key type for OpenID Connect authentication.
*
* @KeyType(
* id = "os2web_key_oidc",
* label = @Translation("OpenID Connect (OIDC)"),
* description = @Translation("A set of credentials for a OpenID Connect."),
* group = "authentication",
* key_value = {
* "plugin" = "os2web_key_oidc",
* "accepted" = FALSE,
* },
* multivalue = {
* "enabled" = true,
* "fields" = {
* "discovery_url" = {
* "label" = @Translation("Discovery url"),
* "required" = true
* },
* "client_id" = {
* "label" = @Translation("Client ID"),
* "required" = true
* },
* "client_secret" = {
* "label" = @Translation("Client secret"),
* "required" = true
* },
* }
* }
* )
*/
class OidcKeyType extends KeyTypeBase implements KeyTypeMultivalueInterface {
public const DISCOVERY_URL = 'discovery_url';
public const CLIENT_ID = 'client_id';
public const CLIENT_SECRET = 'client_secret';

/**
* {@inheritdoc}
*/
public static function generateKeyValue(array $configuration) {
return Json::encode($configuration);
}

/**
* {@inheritdoc}
*/
public function validateKeyValue(array $form, FormStateInterface $form_state, $key_value): void {
if (empty($key_value)) {
$form_state->setError($form, $this->t('The key value is empty.'));
return;
}

$definition = $this->getPluginDefinition();
$fields = $definition['multivalue']['fields'];

foreach ($fields as $id => $field) {
if (!is_array($field)) {
$field = ['label' => $field];
}

if (isset($field['required']) && $field['required'] === FALSE) {
continue;
}

if (!isset($key_value[$id])) {
$form_state->setError($form, $this->t('The key value is missing the field %field.', ['%field' => $id]));
}
elseif (empty($key_value[$id])) {
$form_state->setError($form, $this->t('The key value field %field is empty.', ['%field' => $id]));
}
}
}

/**
* {@inheritdoc}
*/
public function serialize(array $array) {
return Json::encode($array);
}

/**
* {@inheritdoc}
*/
public function unserialize($value) {
return Json::decode($value);
}

}

0 comments on commit 66f35dc

Please sign in to comment.