You are here

abstract class Oauth2ClientGrantServiceBase in OAuth2 Client 8.2

Same name and namespace in other branches
  1. 8.3 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 17

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\Oauth2ClientPluginManagerInterface
   */
  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\Oauth2ClientPluginManagerInterface $oauth2ClientPluginManager
   *   The OAuth2 Client plugin manager.
   */
  public function __construct(RequestStack $requestStack, StateInterface $state, UrlGeneratorInterface $urlGenerator, Oauth2ClientPluginManagerInterface $oauth2ClientPluginManager) {
    $this->currentRequest = $requestStack
      ->getCurrentRequest();
    $this->state = $state;
    $this->urlGenerator = $urlGenerator;
    $this->oauth2ClientPluginManager = $oauth2ClientPluginManager;
    $this->clientProviderCache = array();
  }

  /**
   * Creates a new provider object.
   *
   * @param string $clientId
   *   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($clientId) {
    if (isset($this->clientProviderCache[$clientId])) {
      $provider = $this->clientProviderCache[$clientId];
    }
    else {
      $client = $this
        ->getClient($clientId);
      $provider = new GenericProvider([
        'clientId' => $client
          ->getClientId(),
        'clientSecret' => $client
          ->getClientSecret(),
        'redirectUri' => $this
          ->getRedirectUri($client),
        'urlAuthorize' => $client
          ->getAuthorizationUri(),
        'urlAccessToken' => $client
          ->getTokenUri(),
        'urlResourceOwnerDetails' => $client
          ->getResourceUri(),
        'scopes' => $client
          ->getScopes(),
        'scopeSeparator' => $client
          ->getScopeSeparator(),
      ]);
      $this->clientProviderCache[$clientId] = $provider;
    }
    return $provider;
  }

  /**
   * Store an access token to the Drupal state.
   *
   * @param string $clientId
   *   The client for which a provider should be created.
   * @param \League\OAuth2\Client\Token\AccessToken $accessToken
   *   The Access Token to be stored.
   */
  protected function storeAccessToken($clientId, AccessToken $accessToken) {
    $this->state
      ->set('oauth2_client_access_token-' . $clientId, $accessToken);
  }

  /**
   * Retrieves the local redirect URI used for OAuth2 authentication.
   *
   * @param \Drupal\oauth2_client\Plugin\Oauth2Client\Oauth2ClientPluginInterface $client
   *   The OAuth2 Client Plugin for which the redirect URI should be retrieved.
   *
   * @return string
   *   The redirect URI for the given OAuth2 Server client.
   */
  private function getRedirectUri(Oauth2ClientPluginInterface $client) {
    return $this->urlGenerator
      ->generateFromRoute('<current>', [], [
      'absolute' => TRUE,
    ]);
  }

}

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.
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::getRedirectUri private function Retrieves the local redirect URI used for OAuth2 authentication.
Oauth2ClientGrantServiceBase::storeAccessToken protected function Store an access token to the Drupal state.
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 the Drupal state. Overrides Oauth2ClientServiceInterface::retrieveAccessToken