You are here

abstract class Oauth2ClientGrantServiceBase in OAuth2 Client 8.3

Same name and namespace in other branches
  1. 8.2 src/Service/Grant/Oauth2ClientGrantServiceBase.php \Drupal\oauth2_client\Service\Grant\Oauth2ClientGrantServiceBase

Base class for OAuth2 Client grant services.

Hierarchy

Expanded class hierarchy of Oauth2ClientGrantServiceBase

File

src/Service/Grant/Oauth2ClientGrantServiceBase.php, line 16

Namespace

Drupal\oauth2_client\Service\Grant
View source
abstract class Oauth2ClientGrantServiceBase extends Oauth2ClientServiceBase implements Oauth2ClientGrantServiceInterface {

  /**
   * The Request Stack.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

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

  /**
   * The URL generator service.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The OAuth2 Client plugin manager.
   *
   * @var \Drupal\oauth2_client\PluginManager\Oauth2ClientPluginManager
   */
  protected $oauth2ClientPluginManager;

  /**
   * Client provider cache.
   *
   * @var array
   */
  protected $clientProviderCache;

  /**
   * Construct an OAuth2Client object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The Request Stack.
   * @param \Drupal\Core\State\StateInterface $state
   *   The Drupal state.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $urlGenerator
   *   The URL generator service.
   * @param \Drupal\oauth2_client\PluginManager\Oauth2ClientPluginManager $oauth2ClientPluginManager
   *   The OAuth2 Client plugin manager.
   */
  public function __construct(RequestStack $requestStack, StateInterface $state, UrlGeneratorInterface $urlGenerator, Oauth2ClientPluginManager $oauth2ClientPluginManager) {
    $this->currentRequest = $requestStack
      ->getCurrentRequest();
    $this->state = $state;
    $this->urlGenerator = $urlGenerator;
    $this->oauth2ClientPluginManager = $oauth2ClientPluginManager;
    $this->clientProviderCache = [];
  }

  /**
   * Creates a new provider object.
   *
   * @param string $pluginId
   *   The client for which a provider should be created.
   *
   * @return \League\OAuth2\Client\Provider\GenericProvider
   *   The provider of the OAuth2 Server.
   *
   * @throws \Drupal\oauth2_client\Exception\InvalidOauth2ClientException
   *   Exception thrown when trying to retrieve a non-existent OAuth2 Client.
   */
  protected function getProvider($pluginId) {
    if (isset($this->clientProviderCache[$pluginId])) {
      $provider = $this->clientProviderCache[$pluginId];
    }
    else {
      $client = $this
        ->getClient($pluginId);
      $provider = new GenericProvider([
        'clientId' => $client
          ->getClientId(),
        'clientSecret' => $client
          ->getClientSecret(),
        'redirectUri' => $client
          ->getRedirectUri(),
        'urlAuthorize' => $client
          ->getAuthorizationUri(),
        'urlAccessToken' => $client
          ->getTokenUri(),
        'urlResourceOwnerDetails' => $client
          ->getResourceUri(),
        'scopes' => $client
          ->getScopes(),
        'scopeSeparator' => $client
          ->getScopeSeparator(),
      ]);
      $this->clientProviderCache[$pluginId] = $provider;
    }
    return $provider;
  }

  /**
   * Store an access token using plugin specific storage.
   *
   * @param string $pluginId
   *   The client for which a provider should be created.
   * @param \League\OAuth2\Client\Token\AccessTokenInterface $accessToken
   *   The Access Token to be stored.
   */
  protected function storeAccessToken($pluginId, AccessTokenInterface $accessToken) {
    $client = $this->oauth2ClientPluginManager
      ->createInstance($pluginId);
    $client
      ->storeAccessToken($accessToken);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Oauth2ClientGrantServiceBase::$clientProviderCache protected property Client provider cache.
Oauth2ClientGrantServiceBase::$currentRequest protected property The Request Stack.
Oauth2ClientGrantServiceBase::$oauth2ClientPluginManager protected property The OAuth2 Client plugin manager. Overrides Oauth2ClientServiceBase::$oauth2ClientPluginManager
Oauth2ClientGrantServiceBase::$state protected property The Drupal state.
Oauth2ClientGrantServiceBase::$urlGenerator protected property The URL generator service.
Oauth2ClientGrantServiceBase::getProvider protected function Creates a new provider object.
Oauth2ClientGrantServiceBase::storeAccessToken protected function Store an access token using plugin specific storage.
Oauth2ClientGrantServiceBase::__construct public function Construct an OAuth2Client object. 1
Oauth2ClientGrantServiceInterface::getAccessToken public function Get an OAuth2 access token. 4
Oauth2ClientGrantServiceInterface::getGrantProvider public function Get the league/oauth2 provider. 4
Oauth2ClientServiceBase::clearAccessToken public function Clears the access token for the given client. Overrides Oauth2ClientServiceInterface::clearAccessToken
Oauth2ClientServiceBase::getClient public function Retrieve an OAuth2 Client Plugin. Overrides Oauth2ClientServiceInterface::getClient
Oauth2ClientServiceBase::retrieveAccessToken public function Retrieve an access token from storage. Overrides Oauth2ClientServiceInterface::retrieveAccessToken