You are here

public function AccessTokenEntity::convertToJWT in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8.3 src/Entities/AccessTokenEntity.php \Drupal\simple_oauth\Entities\AccessTokenEntity::convertToJWT()
  2. 5.x src/Entities/AccessTokenEntity.php \Drupal\simple_oauth\Entities\AccessTokenEntity::convertToJWT()

File

src/Entities/AccessTokenEntity.php, line 21

Class

AccessTokenEntity

Namespace

Drupal\simple_oauth\Entities

Code

public function convertToJWT(CryptKey $privateKey) {
  $private_claims = [];
  \Drupal::moduleHandler()
    ->alter('simple_oauth_private_claims', $private_claims, $this);
  if (!is_array($private_claims)) {
    $message = 'An implementation of hook_simple_oauth_private_claims_alter ';
    $message .= 'returns an invalid $private_claims value. $private_claims ';
    $message .= 'must be an array.';
    throw new \InvalidArgumentException($message);
  }
  $builder = (new Builder())
    ->setAudience($this
    ->getClient()
    ->getIdentifier())
    ->setId($this
    ->getIdentifier(), TRUE)
    ->setIssuedAt(time())
    ->setNotBefore(time())
    ->setExpiration($this
    ->getExpiryDateTime()
    ->getTimestamp())
    ->setSubject($this
    ->getUserIdentifier())
    ->set('scopes', $this
    ->getScopes());
  foreach ($private_claims as $claim_name => $value) {
    $builder
      ->set($claim_name, $value);
  }
  $key = new Key($privateKey
    ->getKeyPath(), $privateKey
    ->getPassPhrase());
  $token = $builder
    ->sign(new Sha256(), $key)
    ->getToken();
  return $token;
}