You are here

abstract class Oauth2ClientPluginBase in OAuth2 Client 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Oauth2Client/Oauth2ClientPluginBase.php \Drupal\oauth2_client\Plugin\Oauth2Client\Oauth2ClientPluginBase

Base class for Oauth2Client plugins.

Hierarchy

Expanded class hierarchy of Oauth2ClientPluginBase

4 files declare their use of Oauth2ClientPluginBase
AuthCodeAccessExample.php in examples/oauth2_client_example_plugins/src/Plugin/Oauth2Client/AuthCodeAccessExample.php
AuthCodeExample.php in examples/oauth2_client_example_plugins/src/Plugin/Oauth2Client/AuthCodeExample.php
AuthCodeRedirectExample.php in examples/oauth2_client_example_plugins/src/Plugin/Oauth2Client/AuthCodeRedirectExample.php
ResourceOwnerExample.php in examples/oauth2_client_example_plugins/src/Plugin/Oauth2Client/ResourceOwnerExample.php

File

src/Plugin/Oauth2Client/Oauth2ClientPluginBase.php, line 21

Namespace

Drupal\oauth2_client\Plugin\Oauth2Client
View source
abstract class Oauth2ClientPluginBase extends PluginBase implements Oauth2ClientPluginInterface {
  use ConfigFormBaseTrait;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Injected credential service.
   *
   * @var \Drupal\oauth2_client\Service\CredentialProvider
   */
  protected $credentialService;

  /**
   * The Drupal state api.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Injected UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuid;

  /**
   * Storage for credentials retrieved from credential service.
   *
   * @var array
   */
  private $credentials;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a Oauth2ClientPluginBase object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definitions.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory service.
   * @param \Drupal\oauth2_client\Service\CredentialProvider $credProvider
   *   Injected credential service.
   * @param \Drupal\Core\State\StateInterface $state
   *   Injected state service.
   * @param \Drupal\Component\Uuid\UuidInterface $uuid
   *   Injected UUID service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Injected message service.
   */
  public final function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $configFactory, CredentialProvider $credProvider, StateInterface $state, UuidInterface $uuid, MessengerInterface $messenger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $configFactory;
    $this->credentialService = $credProvider;
    $this->state = $state;
    $this->uuid = $uuid;
    $this->messenger = $messenger;
    $this
      ->clearCredentials();
    $this
      ->loadConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('oauth2_client.service.credentials'), $container
      ->get('state'), $container
      ->get('uuid'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'uuid' => $this->uuid
        ->generate(),
      'credentials' => [],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'oauth2_client.credentials.' . $this
        ->getId(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * Helper function to initialize the internal configuration array.
   *
   * @param array $configuration
   *   Provided configuration.
   * @param bool $save
   *   Flags if the loaded configuration should also be saved.
   */
  protected function loadConfiguration(array $configuration, $save = FALSE) {
    $configName = 'oauth2_client.credentials.' . $this
      ->getId();
    $config = $this
      ->config($configName);
    $savedConfig = $config
      ->getRawData();
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $savedConfig, $configuration);
    if ($save) {
      foreach ($this->configuration as $key => $value) {
        $config
          ->set($key, $value);
      }
      $config
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this
      ->loadConfiguration($configuration, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $credentials = $this
      ->retrieveCredentials();
    $grantType = $this
      ->getGrantType();
    $form = [
      'credential_provider' => [
        '#type' => 'hidden',
        '#value' => 'oauth2_client',
      ],
      'oauth2_client' => [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Stored locally'),
        'client_id' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Client ID'),
          '#default_value' => $credentials['client_id'] ?? '',
        ],
        'client_secret' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Client secret'),
          '#default_value' => $credentials['client_secret'] ?? '',
        ],
      ],
    ];
    if ($grantType == 'resource_owner') {
      $form['oauth2_client']['username'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Username'),
        '#description' => $this
          ->t('The username and password entered here are not saved, but are only used to request the token.'),
      ];
      $form['oauth2_client']['password'] = [
        '#type' => 'password',
        '#title' => $this
          ->t('Password'),
      ];
    }

    // If Key module or some other future additional provider is available:
    if ($this->credentialService
      ->additionalProviders()) {
      $this
        ->expandedProviderOptions($form);
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (empty($values['credential_provider'])) {
      $form_state
        ->setError($form['credential_provider'], 'A credential provider is required.');
    }
    else {
      $provider = $values['credential_provider'];
      foreach ($values[$provider] as $key => $value) {
        if (empty($value)) {
          $form_state
            ->setError($form[$provider][$key], 'All credential values are required.');
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $configuration = $this
      ->getConfiguration();
    $values = $form_state
      ->getValues();
    $provider = $values['credential_provider'];
    $credentials = $values[$provider];
    array_walk($credentials, function (&$value) {
      $value = trim($value);
    });
    $key = $configuration['uuid'];
    if ($provider == 'key') {
      $key = $credentials['id'];
    }
    $configuration['credentials'] = [
      'credential_provider' => $provider,
      'storage_key' => $key,
    ];
    $this
      ->setConfiguration($configuration);
    if ($provider == 'oauth2_client') {

      // Remove the username and password.
      if (isset($credentials['username'])) {
        unset($credentials['username']);
      }
      if (isset($credentials['password'])) {
        unset($credentials['password']);
      }
      $this->state
        ->set($configuration['uuid'], $credentials);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    $this
      ->checkKeyDefined('name');
    return $this->pluginDefinition['name'];
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    $this
      ->checkKeyDefined('id');
    return $this->pluginDefinition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getClientId() {
    $credentials = $this
      ->retrieveCredentials();
    if (empty($credentials['client_id'])) {
      throw new Oauth2ClientPluginMissingKeyException('client_id');
    }
    return $credentials['client_id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getClientSecret() {
    $credentials = $this
      ->retrieveCredentials();
    if (empty($credentials['client_secret'])) {
      throw new Oauth2ClientPluginMissingKeyException('client_secret');
    }
    return $credentials['client_secret'];
  }

  /**
   * {@inheritdoc}
   */
  public function getGrantType() {
    $this
      ->checkKeyDefined('grant_type');
    return $this->pluginDefinition['grant_type'];
  }

  /**
   * {@inheritdoc}
   */
  public function getRedirectUri() {
    $url = Url::fromRoute('oauth2_client.code', [
      'plugin' => $this
        ->getId(),
    ], [
      'absolute' => TRUE,
    ]);
    return $url
      ->toString(TRUE)
      ->getGeneratedUrl();
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthorizationUri() {
    $this
      ->checkKeyDefined('authorization_uri');
    return $this->pluginDefinition['authorization_uri'];
  }

  /**
   * {@inheritdoc}
   */
  public function getTokenUri() {
    $this
      ->checkKeyDefined('token_uri');
    return $this->pluginDefinition['token_uri'];
  }

  /**
   * {@inheritdoc}
   */
  public function getResourceUri() {
    $this
      ->checkKeyDefined('resource_owner_uri');
    return $this->pluginDefinition['resource_owner_uri'];
  }

  /**
   * {@inheritdoc}
   */
  public function getScopes() {
    if (!isset($this->pluginDefinition['scopes'])) {
      return [];
    }
    return $this->pluginDefinition['scopes'] ?: [];
  }

  /**
   * {@inheritdoc}
   */
  public function getScopeSeparator() {
    if (!isset($this->pluginDefinition['scope_separator'])) {
      return ',';
    }
    return $this->pluginDefinition['scope_separator'];
  }

  /**
   * Check that a key is defined when requested. Throw an exception if not.
   *
   * @param string $key
   *   The key to check.
   *
   * @throws \Drupal\oauth2_client\Exception\Oauth2ClientPluginMissingKeyException
   *   Thrown if the key being checked is not defined.
   */
  private function checkKeyDefined($key) {
    if (!isset($this->pluginDefinition[$key])) {
      throw new Oauth2ClientPluginMissingKeyException($key);
    }
  }

  /**
   * Helper function to retrieve and cache credentials.
   *
   * @return array
   *   The credentials array.
   */
  private function retrieveCredentials() {
    if (empty($this->credentials)) {
      $this->credentials = $this->credentialService
        ->getCredentials($this);
    }
    return $this->credentials;
  }

  /**
   * Helper function to clear cached credentials.
   */
  private function clearCredentials() {
    $this->credentials = [];
  }

  /**
   * Helper method to build the credential provider elements of the form.
   *
   * Only needed if we have more than one provider.  Currently supporting
   * oauth2_client controlled local storage and Key module controlled optional
   * storage.
   *
   * @param array $form
   *   The configuration form.
   */
  protected function expandedProviderOptions(array &$form) {
    $provider = $this
      ->getCredentialProvider();
    $grantType = $this
      ->getGrantType();

    // Provide selectors for the api key credential provider.
    $form['credential_provider'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Credential provider'),
      '#default_value' => empty($provider) ? 'oauth2_client' : $provider,
      '#options' => [
        'oauth2_client' => $this
          ->t('Local storage'),
        'key' => $this
          ->t('Key module'),
      ],
      '#attributes' => [
        'data-states-selector' => 'provider',
      ],
      '#weight' => -99,
    ];
    $form['oauth2_client']['#states'] = [
      'required' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'oauth2_client',
        ],
      ],
      'visible' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'oauth2_client',
        ],
      ],
      'enabled' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'oauth2_client',
        ],
      ],
    ];
    $key_id = $provider == 'key' ? $this
      ->getStorageKey() : '';
    $form['key'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Managed by the Key module'),
      '#states' => [
        'required' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
        'visible' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
        'enabled' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
      ],
      'id' => [
        '#type' => 'key_select',
        '#title' => $this
          ->t('Select a stored Key'),
        '#default_value' => $key_id,
        '#empty_option' => $this
          ->t('- Please select -'),
        '#key_filters' => [
          'type' => 'oauth2_client',
        ],
        '#description' => $this
          ->t('Select the key you have configured to hold the Oauth credentials.'),
      ],
    ];
    if ($grantType == 'resource_owner') {
      $form['key']['username'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Username'),
        '#description' => $this
          ->t('The username and password entered here are not saved, but are only used to request the token.'),
      ];
      $form['key']['password'] = [
        '#type' => 'password',
        '#title' => $this
          ->t('Password'),
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCredentialProvider() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['credentials']['credential_provider'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageKey() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['credentials']['storage_key'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function displaySuccessMessage() {
    return $this->pluginDefinition['success_message'] ?? FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Oauth2ClientPluginBase::$configFactory protected property The configuration factory.
Oauth2ClientPluginBase::$credentials private property Storage for credentials retrieved from credential service.
Oauth2ClientPluginBase::$credentialService protected property Injected credential service.
Oauth2ClientPluginBase::$messenger protected property The messenger service. Overrides MessengerTrait::$messenger
Oauth2ClientPluginBase::$state protected property The Drupal state api.
Oauth2ClientPluginBase::$uuid protected property Injected UUID service.
Oauth2ClientPluginBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
Oauth2ClientPluginBase::checkKeyDefined private function Check that a key is defined when requested. Throw an exception if not.
Oauth2ClientPluginBase::clearCredentials private function Helper function to clear cached credentials.
Oauth2ClientPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
Oauth2ClientPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
Oauth2ClientPluginBase::displaySuccessMessage public function Check the plugin definition for success_message or return a static value. Overrides Oauth2ClientPluginInterface::displaySuccessMessage
Oauth2ClientPluginBase::expandedProviderOptions protected function Helper method to build the credential provider elements of the form.
Oauth2ClientPluginBase::getAuthorizationUri public function Retrieves the authorization_uri of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getAuthorizationUri
Oauth2ClientPluginBase::getClientId public function Retrieves the client_id of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getClientId
Oauth2ClientPluginBase::getClientSecret public function Retrieves the client_secret of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getClientSecret
Oauth2ClientPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
Oauth2ClientPluginBase::getCredentialProvider public function Returns the plugin credentials if they are set, otherwise returns NULL. Overrides Oauth2ClientPluginInterface::getCredentialProvider
Oauth2ClientPluginBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
Oauth2ClientPluginBase::getGrantType public function Retrieves the grant type of the plugin. Overrides Oauth2ClientPluginInterface::getGrantType
Oauth2ClientPluginBase::getId public function Retrieves the id of the OAuth2 Client plugin. Overrides Oauth2ClientPluginInterface::getId
Oauth2ClientPluginBase::getName public function Retrieves the human-readable name of the Oauth2 Client plugin. Overrides Oauth2ClientPluginInterface::getName
Oauth2ClientPluginBase::getRedirectUri public function Retrieves the redirect_uri of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getRedirectUri
Oauth2ClientPluginBase::getResourceUri public function Retrieves the resource_uri of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getResourceUri
Oauth2ClientPluginBase::getScopes public function Get the set of scopes for the provider to use by default. Overrides Oauth2ClientPluginInterface::getScopes
Oauth2ClientPluginBase::getScopeSeparator public function Get the separator used to join the scopes in the OAuth2 query string. Overrides Oauth2ClientPluginInterface::getScopeSeparator
Oauth2ClientPluginBase::getStorageKey public function Returns the credential storage key if it is set, otherwise returns NULL. Overrides Oauth2ClientPluginInterface::getStorageKey
Oauth2ClientPluginBase::getTokenUri public function Retrieves the token_uri of the OAuth2 server. Overrides Oauth2ClientPluginInterface::getTokenUri
Oauth2ClientPluginBase::loadConfiguration protected function Helper function to initialize the internal configuration array.
Oauth2ClientPluginBase::retrieveCredentials private function Helper function to retrieve and cache credentials.
Oauth2ClientPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
Oauth2ClientPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
Oauth2ClientPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
Oauth2ClientPluginBase::__construct final public function Constructs a Oauth2ClientPluginBase object. Overrides PluginBase::__construct
Oauth2ClientPluginInterface::clearAccessToken public function Clears the access token from storage. 4
Oauth2ClientPluginInterface::retrieveAccessToken public function Retrieve the access token storage. 4
Oauth2ClientPluginInterface::storeAccessToken public function Stores access tokens obtained by this client. 4
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.