You are here

public function JanrainCaptureApi::getAccessToken in Janrain Registration 8

Returns an access token from the database and prolongs it automatically.

IMPORTANT: this method requires a user to be authenticated in Drupal.

Parameters

bool $force_refresh: An indicator to forcibly refresh an access token.

Return value

\Drupal\janrain_capture\Authentication\AccessToken The access token.

Throws

\InvalidArgumentException

\GuzzleHttp\Exception\GuzzleException

\Drupal\janrain_capture\Exception\JsonParseError

\Drupal\janrain_capture\Exception\JanrainApiCallError

\Drupal\janrain_capture\Exception\JanrainUnauthorizedError

Overrides JanrainCaptureApiInterface::getAccessToken

1 call to JanrainCaptureApi::getAccessToken()
JanrainCaptureApi::getUserProfile in src/JanrainCaptureApi.php
Returns the user's profile data.

File

src/JanrainCaptureApi.php, line 193

Class

JanrainCaptureApi
The integration between Janrain and Drupal.

Namespace

Drupal\janrain_capture

Code

public function getAccessToken(bool $force_refresh = FALSE) : AccessToken {

  /* @var \Drupal\janrain_capture\Authentication\AccessToken $access_token */

  // Read the token stored after successful authentication.
  $access_token = $this
    ->cache();

  // No token in the database - the user is not authenticated and probably
  // has never been (could be the case the database entry was truncated).
  if ($access_token === NULL) {
    throw new JanrainUnauthorizedError('The user has never been authenticated.', -1);
  }

  // Forcible refresh wasn't requested and expiration date hasn't passed.
  if (!$force_refresh && !$access_token
    ->isExpired()) {
    return $access_token;
  }

  // Prolong the access token and update it in the database.
  return $this
    ->cache($this
    ->getToken(static::GRANT_TYPE_REFRESH_TOKEN, [
    'refresh_token' => $access_token
      ->getRefreshToken()
      ->getToken(),
  ]));
}