View source
<?php
namespace Drupal\openid_connect\Plugin\OpenIDConnectClient;
use Drupal\Core\Form\FormStateInterface;
use Drupal\openid_connect\Plugin\OpenIDConnectClientBase;
class OpenIDConnectGenericClient extends OpenIDConnectClientBase {
public function defaultConfiguration() : array {
return [
'issuer_url' => '',
'authorization_endpoint' => 'https://example.com/oauth2/authorize',
'token_endpoint' => 'https://example.com/oauth2/token',
'userinfo_endpoint' => 'https://example.com/oauth2/userinfo',
'end_session_endpoint' => '',
'scopes' => [
'openid',
'email',
],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
$form = parent::buildConfigurationForm($form, $form_state);
$form['use_well_known'] = [
'#title' => $this
->t('Auto discover endpoints'),
'#type' => 'checkbox',
'#description' => $this
->t('Requires IDP support for "<a href="@url" target="_blank">OpenID Connect Discovery</a>".', [
'@url' => 'https://openid.net/specs/openid-connect-discovery-1_0.html',
]),
'#default_value' => !empty($this->configuration['issuer_url']),
];
$form['issuer_url'] = [
'#title' => $this
->t('Issuer URL'),
'#type' => 'url',
'#default_value' => $this->configuration['issuer_url'],
'#states' => [
'visible' => [
':input[name="settings[use_well_known]"]' => [
'checked' => TRUE,
],
],
],
];
$form['authorization_endpoint'] = [
'#title' => $this
->t('Authorization endpoint'),
'#type' => 'url',
'#default_value' => $this->configuration['authorization_endpoint'],
'#states' => [
'visible' => [
':input[name="settings[use_well_known]"]' => [
'checked' => FALSE,
],
],
],
];
$form['token_endpoint'] = [
'#title' => $this
->t('Token endpoint'),
'#type' => 'url',
'#default_value' => $this->configuration['token_endpoint'],
'#states' => [
'visible' => [
':input[name="settings[use_well_known]"]' => [
'checked' => FALSE,
],
],
],
];
$form['userinfo_endpoint'] = [
'#title' => $this
->t('UserInfo endpoint'),
'#type' => 'url',
'#default_value' => $this->configuration['userinfo_endpoint'],
'#states' => [
'visible' => [
':input[name="settings[use_well_known]"]' => [
'checked' => FALSE,
],
],
],
];
$form['end_session_endpoint'] = [
'#title' => $this
->t('End Session endpoint'),
'#type' => 'url',
'#default_value' => $this->configuration['end_session_endpoint'],
'#states' => [
'visible' => [
':input[name="settings[use_well_known]"]' => [
'checked' => FALSE,
],
],
],
];
$form['scopes'] = [
'#title' => $this
->t('Scopes'),
'#type' => 'textfield',
'#description' => $this
->t('Custom scopes, separated by spaces, for example: openid email'),
'#default_value' => implode(' ', $this->configuration['scopes']),
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$configuration = $form_state
->getValues();
if ($configuration['use_well_known']) {
$endpoints = $this
->autoDiscoverEndpoints($configuration['issuer_url']);
if ($endpoints === FALSE) {
$form_state
->setErrorByName('issuer_url', $this
->t('The issuer URL @url appears to be invalid.', [
'@url' => $configuration['issuer_url'],
]));
}
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$configuration = $form_state
->getValues();
if ($configuration['use_well_known']) {
$endpoints = $this
->autoDiscoverEndpoints($configuration['issuer_url']);
$this
->setConfiguration([
'authorization_endpoint' => $endpoints['authorization_endpoint'],
'token_endpoint' => $endpoints['token_endpoint'],
'userinfo_endpoint' => $endpoints['userinfo_endpoint'],
]);
}
$this
->unsetConfigurationKeys([
'use_well_known',
]);
if (!empty($configuration['scopes'])) {
$this
->setConfiguration([
'scopes' => explode(' ', $configuration['scopes']),
]);
}
parent::submitConfigurationForm($form, $form_state);
}
public function getClientScopes() : ?array {
return $this->configuration['scopes'];
}
protected function autoDiscoverEndpoints(string $issuer_url = '') {
static $results = [];
if (empty($issuer_url)) {
$issuer_url = $this->configuration['issuer_url'];
}
if (!isset($results[$issuer_url])) {
$results[$issuer_url] = $this->autoDiscover
->fetch($issuer_url);
}
$result = $results[$issuer_url];
if ($result && isset($result['authorization_endpoint']) && isset($result['token_endpoint']) && isset($result['userinfo_endpoint'])) {
return $result;
}
return FALSE;
}
public function getEndpoints() : array {
return [
'authorization' => $this->configuration['authorization_endpoint'],
'token' => $this->configuration['token_endpoint'],
'userinfo' => $this->configuration['userinfo_endpoint'],
'end_session' => $this->configuration['end_session_endpoint'],
];
}
}