You are here

public function OAuth2Storage::setAccessToken in OAuth2 Server 2.0.x

Same name and namespace in other branches
  1. 8 src/OAuth2Storage.php \Drupal\oauth2_server\OAuth2Storage::setAccessToken()

Set access token.

Parameters

string $access_token: The access token string.

string $client_id: The client id string.

int $uid: The user id.

int $expires: The timestamp the token expires.

string|null $scope: The scope string.

Return value

int Whether the access token could be saved or not.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/OAuth2Storage.php, line 405

Class

OAuth2Storage
Provides Drupal OAuth2 storage for the library.

Namespace

Drupal\oauth2_server

Code

public function setAccessToken($access_token, $client_id, $uid, $expires, $scope = NULL) {
  $client = $this
    ->getStorageClient($client_id);
  if (!$client) {
    throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
  }

  // If no token was found, start with a new entity.
  $token = $this
    ->getStorageToken($access_token);
  if (!$token) {

    // The username is not required, the "Client credentials" grant type
    // doesn't provide it, for instance.
    if (!$uid || !$this->entityTypeManager
      ->getStorage('user')
      ->load($uid)) {
      $uid = 0;
    }
    $token = $this->entityTypeManager
      ->getStorage('oauth2_server_token')
      ->create([
      'type' => 'access',
    ]);
    $token->client_id = $client
      ->id();
    $token->uid = $uid;
    $token->token = $access_token;
  }
  $token->expires = $expires;
  $this
    ->setScopeData($token, $client
    ->getServer(), $scope);
  return $token
    ->save();
}