You are here

protected function AccessToken::addRefreshToken in Simple OAuth (OAuth2) & OpenID Connect 8

Adds a refresh token and links it to this entity.

1 call to AccessToken::addRefreshToken()
AccessToken::postSave in src/Entity/AccessToken.php
Acts on a saved entity before the insert or update hook is invoked.

File

src/Entity/AccessToken.php, line 309

Class

AccessToken
Defines the Access Token entity.

Namespace

Drupal\simple_oauth\Entity

Code

protected function addRefreshToken() {

  // Only add the refresh token of there is none associated.
  $has_refresh_token = (bool) $this
    ->entityManager()
    ->getStorage($this
    ->getEntityTypeId())
    ->getQuery()
    ->condition('access_token_id', $this
    ->id())
    ->count()
    ->execute();
  if ($has_refresh_token) {
    return;
  }
  $extension = \Drupal::config('simple_oauth.settings')
    ->get('refresh_extension') ?: static::REFRESH_EXTENSION_TIME;
  $values = [
    'expire' => $this
      ->get('expire')->value + $extension,
    'auth_user_id' => $this
      ->get('auth_user_id')->target_id,
    'access_token_id' => $this
      ->id(),
    'resource' => 'authentication',
    'created' => $this
      ->getCreatedTime(),
    'changed' => $this
      ->getChangedTime(),
  ];
  $refresh_token = $this
    ->entityManager()
    ->getStorage($this
    ->getEntityType()
    ->id())
    ->create($values);
  $refresh_token
    ->save();
}