You are here

public function Storage::setAccessToken in OAuth2 Server 7

File

lib/Drupal/oauth2_server/Storage.php, line 134

Class

Storage
Provides Drupal storage (through the underlying Entity API) for the library.

Namespace

Drupal\oauth2_server

Code

public function setAccessToken($access_token, $client_key, $uid, $expires, $scope = null) {
  $client = oauth2_server_client_load($client_key);
  if (!$client) {
    throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
  }

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

    // The username is not required, the "Client credentials" grant type
    // doesn't provide it, for instance.
    if (!$uid || !user_load($uid)) {
      $uid = 0;
    }
    $token = entity_create('oauth2_server_token', array(
      'type' => 'access',
    ));
    $token->client_id = $client->client_id;
    $token->uid = $uid;
    $token->token = $access_token;
  }
  $token->expires = $expires;
  $this
    ->setScopeData($token, $client->server, $scope);
  $status = $token
    ->save();
  return $status;
}