You are here

class CredentialProvider in OAuth2 Client 8.3

Class KeyProvider.

@package Drupal\oauth2_client\Service

Hierarchy

Expanded class hierarchy of CredentialProvider

1 file declares its use of CredentialProvider
Oauth2ClientPluginBase.php in src/Plugin/Oauth2Client/Oauth2ClientPluginBase.php
1 string reference to 'CredentialProvider'
oauth2_client.services.yml in ./oauth2_client.services.yml
oauth2_client.services.yml

File

src/Service/CredentialProvider.php, line 15

Namespace

Drupal\oauth2_client\Service
View source
class CredentialProvider {

  /**
   * Key module service conditionally injected.
   *
   * @var \Drupal\key\KeyRepositoryInterface
   */
  protected $keyRepository;

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

  /**
   * KeyService constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The key value store to use.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * Provides a means to our services.yml file to conditionally inject service.
   *
   * @param \Drupal\key\KeyRepositoryInterface $repository
   *   The injected service, if it exists.
   */
  public function setKeyRepository(KeyRepositoryInterface $repository) {
    $this->keyRepository = $repository;
  }

  /**
   * Detects if key module service was injected.
   *
   * @return bool
   *   True if the KeyRepository is present.
   */
  public function additionalProviders() {
    return $this->keyRepository instanceof KeyRepositoryInterface;
  }

  /**
   * Get the provided credentials.
   *
   * @param \Drupal\oauth2_client\Plugin\Oauth2Client\Oauth2ClientPluginInterface $plugin
   *   An authorization plugin.
   *
   * @return array|string
   *   The value of the configured key.
   */
  public function getCredentials(Oauth2ClientPluginInterface $plugin) {
    $provider = $plugin
      ->getCredentialProvider();
    $credentials = [];
    if (empty($provider)) {
      return $credentials;
    }
    switch ($provider) {
      case 'key':
        $keyEntity = $this->keyRepository
          ->getKey($plugin
          ->getStorageKey());
        if ($keyEntity instanceof Key) {

          // A key was found in the repository.
          $credentials = $keyEntity
            ->getKeyValues();
        }
        break;
      default:
        $credentials = $this->state
          ->get($plugin
          ->getStorageKey());
    }
    return $credentials;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CredentialProvider::$keyRepository protected property Key module service conditionally injected.
CredentialProvider::$state protected property The Drupal state api.
CredentialProvider::additionalProviders public function Detects if key module service was injected.
CredentialProvider::getCredentials public function Get the provided credentials.
CredentialProvider::setKeyRepository public function Provides a means to our services.yml file to conditionally inject service.
CredentialProvider::__construct public function KeyService constructor.