You are here

class AuthorizationCodeGrantService in OAuth2 Client 8.3

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

Handles Authorization Grants for the OAuth2 Client module.

Hierarchy

Expanded class hierarchy of AuthorizationCodeGrantService

1 string reference to 'AuthorizationCodeGrantService'
oauth2_client.services.yml in ./oauth2_client.services.yml
oauth2_client.services.yml

File

src/Service/Grant/AuthorizationCodeGrantService.php, line 19

Namespace

Drupal\oauth2_client\Service\Grant
View source
class AuthorizationCodeGrantService extends Oauth2ClientGrantServiceBase {

  /**
   * The Drupal tempstore.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStore
   */
  protected $tempstore;

  /**
   * 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.
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempstoreFactory
   *   The Drupal private tempstore factory.
   */
  public function __construct(RequestStack $requestStack, StateInterface $state, UrlGeneratorInterface $urlGenerator, Oauth2ClientPluginManagerInterface $oauth2ClientPluginManager, PrivateTempStoreFactory $tempstoreFactory) {
    parent::__construct($requestStack, $state, $urlGenerator, $oauth2ClientPluginManager);
    $this->tempstore = $tempstoreFactory
      ->get('oauth2_client');
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken($pluginId) {
    $provider = $this
      ->getProvider($pluginId);

    // Get the authorization URL. This also generates the state.
    $authorization_url = $provider
      ->getAuthorizationUrl();

    // Save the state to Drupal's tempstore.
    $this->tempstore
      ->set('oauth2_client_state-' . $pluginId, $provider
      ->getState());
    if ($this->currentRequest
      ->hasSession()) {

      // If we have a session, save before redirect.
      $this->currentRequest
        ->getSession()
        ->save();
    }

    // Redirect to the authorization URL.
    $redirect = new RedirectResponse($authorization_url);
    $redirect
      ->send();
    exit;
  }

  /**
   * Executes an authorization_code grant request with the give code.
   *
   * @param string $pluginId
   *   The client plugin id.
   * @param string $code
   *   The authorization code.
   *
   * @return bool
   *   Was a valid token retrieved?
   *
   * @throws \Drupal\oauth2_client\Exception\InvalidOauth2ClientException
   *   Exception thrown when trying to retrieve a non-existent OAuth2 Client.
   */
  public function requestAccessToken($pluginId, $code) {
    $provider = $this
      ->getProvider($pluginId);

    // Try to get an access token using the authorization code grant.
    try {
      $accessToken = $provider
        ->getAccessToken('authorization_code', [
        'code' => $code,
      ]);
      if ($accessToken instanceof AccessTokenInterface) {
        $this
          ->storeAccessToken($pluginId, $accessToken);
        return TRUE;
      }
    } catch (IdentityProviderException $e) {
      watchdog_exception('OAuth2 Client', $e);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getGrantProvider($pluginId) {
    return $this
      ->getProvider($pluginId);
  }

  /**
   * Provide a redirect for use following authorization code capture.
   *
   * @param string $pluginId
   *   The client plugin id.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   The redirect response.
   */
  public function getPostCaptureRedirect($pluginId) {
    $clientPlugin = $this
      ->getClient($pluginId);
    if ($clientPlugin instanceof Oauth2ClientPluginRedirectInterface) {
      return $clientPlugin
        ->getPostCaptureRedirect();
    }
    $url = Url::fromRoute('oauth2_client.oauth2_client_plugin_list');
    return new RedirectResponse($url
      ->toString(TRUE)
      ->getGeneratedUrl());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthorizationCodeGrantService::$tempstore protected property The Drupal tempstore.
AuthorizationCodeGrantService::getAccessToken public function Get an OAuth2 access token. Overrides Oauth2ClientGrantServiceInterface::getAccessToken
AuthorizationCodeGrantService::getGrantProvider public function Get the league/oauth2 provider. Overrides Oauth2ClientGrantServiceInterface::getGrantProvider
AuthorizationCodeGrantService::getPostCaptureRedirect public function Provide a redirect for use following authorization code capture.
AuthorizationCodeGrantService::requestAccessToken public function Executes an authorization_code grant request with the give code.
AuthorizationCodeGrantService::__construct public function Construct an OAuth2Client object. Overrides Oauth2ClientGrantServiceBase::__construct
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.
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