-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ drush pm:install os2web_key | |
|
||
`@todo` | ||
|
||
### OpenID Connect | ||
|
||
`@todo` | ||
|
||
## Example | ||
|
||
`@todo` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |