You are here

public function OAuth2Storage::getAccessToken in OAuth2 Server 8

Same name and namespace in other branches
  1. 2.0.x src/OAuth2Storage.php \Drupal\oauth2_server\OAuth2Storage::getAccessToken()

Get access token.

Parameters

string $access_token: The access token string.

Return value

array|bool An access token array or false.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/OAuth2Storage.php, line 323

Class

OAuth2Storage
Provides Drupal OAuth2 storage for the library.

Namespace

Drupal\oauth2_server

Code

public function getAccessToken($access_token) {

  /** @var \Drupal\oauth2_server\TokenInterface $token */
  $token = $this
    ->getStorageToken($access_token);
  if (!$token) {
    return FALSE;
  }
  $user = $token
    ->getUser();
  $enabled_grant_types = array_filter($token
    ->getClient()
    ->getServer()
    ->get('settings')['grant_types']);
  if (!in_array('client_credentials', $enabled_grant_types)) {
    if ($user && $user
      ->isBlocked()) {

      // If the user is blocked, deny access.
      return FALSE;
    }
  }
  $scopes = [];

  /** @var \Drupal\oauth2_server\ScopeInterface[] $scope_entities */
  $scope_entities = $token->scopes
    ->referencedEntities();
  foreach ($scope_entities as $scope) {
    $scopes[] = $scope->scope_id;
  }
  sort($scopes);

  // Return a token array in the format expected by the library.
  $token_array = [
    'server' => $token
      ->getClient()
      ->getServer()
      ->id(),
    'client_id' => $token
      ->getClient()->client_id,
    'user_id' => $user
      ->id(),
    'user_uuid' => $user
      ->uuid(),
    'access_token' => $token->token->value,
    'expires' => (int) $token->expires->value,
    'scope' => implode(' ', $scopes),
  ];

  // Track last access on the token.
  $this
    ->logAccessTime($token);
  return $token_array;
}