/** * Constructs a TokenAuthUser object. * * @param AccessTokenInterface $token * The underlying token. * @throws \Exception * When there is no user. */ public function __construct(AccessTokenInterface $token) { if (!($this->subject = $token->get('auth_user_id')->entity)) { throw new \Exception('The access token does not link to a user.'); } $this->token = $token; }
/** * Serializes the token either using the serializer or manually. * * @param AccessTokenInterface $token * The token. * * @return string * The serialized token. */ protected function normalize(AccessTokenInterface $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)) { // TODO: Add appropriate error handling. Maybe throw an exception? return []; } $refresh_token = $storage->load(reset($ids)); if (!$refresh_token || !$refresh_token->isRefreshToken()) { // TODO: Add appropriate error handling. Maybe throw an exception? return []; } return ['access_token' => $token->get('value')->value, 'token_type' => 'Bearer', 'expires_in' => $token->get('expire')->value - REQUEST_TIME, 'refresh_token' => $refresh_token->get('value')->value]; }