You are here

protected function Oauth::getAccessToken in Entity Share 8.3

Obtains the stored or renewed access token based on expiration state.

Parameters

string $url: The url to the remote.

Return value

string The token value.

2 calls to Oauth::getAccessToken()
Oauth::getClient in modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php
Prepares a guzzle client for http operations with the supported auth.
Oauth::getJsonApiClient in modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php
Prepares a guzzle client for JSON operations with the supported auth.

File

modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php, line 79

Class

Oauth
Provides Oauth2 based client authorization.

Namespace

Drupal\entity_share_client\Plugin\ClientAuthorization

Code

protected function getAccessToken($url) {
  $configuration = $this
    ->getConfiguration();
  $accessToken = $this->keyValueStore
    ->get($configuration['uuid'] . '-' . $this
    ->getPluginId());
  $credentials = $this->keyService
    ->getCredentials($this);
  if ($accessToken instanceof AccessTokenInterface) {
    if ($accessToken
      ->hasExpired()) {

      // Get the oauth client.
      $oauth_client = $this
        ->getOauthClient($url, $credentials);

      // Try to get an access token using the authorization code grant.
      try {
        $newAccessToken = $oauth_client
          ->getAccessToken('refresh_token', [
          'refresh_token' => $accessToken
            ->getRefreshToken(),
        ]);
      } catch (\Throwable $e) {

        // If refresh token failed try to get an access token using the
        // client credentials grant.
        $this->logger
          ->critical('Entity Share refresh oauth token request failed with Exception: %exception_type and error: %error.', [
          '%exception_type' => get_class($e),
          '%error' => $e
            ->getMessage(),
        ]);
        try {
          $newAccessToken = $oauth_client
            ->getAccessToken('client_credentials');
        } catch (\Throwable $e) {
          $this->logger
            ->critical('Entity Share new oauth token request failed with Exception: %exception_type and error: %error.', [
            '%exception_type' => get_class($e),
            '%error' => $e
              ->getMessage(),
          ]);
          return '';
        }
      }
      $this->keyValueStore
        ->set($configuration['uuid'] . '-' . $this
        ->getPluginId(), $newAccessToken);
      return $newAccessToken
        ->getToken();
    }
    return $accessToken
      ->getToken();
  }
  return '';
}