You are here

protected function AccessTokenIssue::normalize in Simple OAuth (OAuth2) & OpenID Connect 8

Manually normalize a token entity into an structured array.

@todo Use the serialization system.

Parameters

\Drupal\simple_oauth\Entity\AccessToken $token: The token entity to normalize.

Return value

array The structured array.

1 call to AccessTokenIssue::normalize()
AccessTokenIssue::issue in src/Controller/AccessTokenIssue.php

File

src/Controller/AccessTokenIssue.php, line 103

Class

AccessTokenIssue

Namespace

Drupal\simple_oauth\Controller

Code

protected function normalize(AccessToken $token) {
  $output = [
    'access_token' => $token
      ->get('value')->value,
    'token_type' => 'Bearer',
    'expires_in' => $token
      ->get('expire')->value - REQUEST_TIME,
  ];

  // Try to load the associated refresh token.
  $storage = $this
    ->entityManager()
    ->getStorage('access_token');
  $ids = $storage
    ->getQuery()
    ->condition('access_token_id', $token
    ->id())
    ->condition('expire', REQUEST_TIME, '>')
    ->condition('resource', 'authentication')
    ->range(0, 1)
    ->execute();
  if (!empty($ids)) {
    $output['refresh_token'] = $storage
      ->load(reset($ids))
      ->get('value')->value;
  }
  return $output;
}