You are here

public function OAuth2Storage::setRefreshToken in OAuth2 Server 8

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

Set refresh token.

Parameters

string $refresh_token: The refresh token string.

string $client_id: The client id string.

int $uid: The user id integer.

int $expires: The expiration timestamp.

string|null $scope: The scope string.

Return value

int Whether the token was saved or not.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/OAuth2Storage.php, line 861

Class

OAuth2Storage
Provides Drupal OAuth2 storage for the library.

Namespace

Drupal\oauth2_server

Code

public function setRefreshToken($refresh_token, $client_id, $uid, $expires, $scope = NULL) {

  /** @var \Drupal\oauth2_server\ClientInterface $client */
  $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.

  /** @var \Drupal\oauth2_server\TokenInterface $token */
  $token = $this
    ->getStorageToken($refresh_token);
  if (!$token) {
    $user = $this->entityTypeManager
      ->getStorage('user')
      ->load($uid);
    if (!$user) {
      throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
    }
    $token = $this->entityTypeManager
      ->getStorage('oauth2_server_token')
      ->create([
      'type' => 'refresh',
    ]);
    $token->client_id = $client
      ->id();
    $token->uid = $uid;
    $token->token = $refresh_token;
  }
  $token->expires = $expires;
  $this
    ->setScopeData($token, $client
    ->getServer(), $scope);
  return $token
    ->save();
}