public function loginByToken(Token $token) { $uid = $token->getClaim('uid'); try { $this->user = $this->usersRepository->getById($uid); } catch (UserNotFoundException $e) { // do nothing here } }
/** * Validates JWT token * * @param Token $token * @throws ExpiredTokenException when token has expired and can be refreshed * @throws InvalidTokenException when token has expired or is invalid */ protected function validateToken(Token $token = null) { if (null === $token) { throw new InvalidTokenException(); } $exp = (new \DateTime())->setTimestamp($token->getClaim('exp')); $now = date_create(); $refreshTtl = \DateInterval::createFromDateString($this->refreshTtl); if ($now < $exp) { return; } if ($exp->add($refreshTtl) > $now) { throw new ExpiredTokenException(); } throw new InvalidTokenException(); }
/** * @test * * @uses Lcobucci\JWT\Token::__construct * @uses Lcobucci\JWT\Token::hasClaim * @uses Lcobucci\JWT\Claim\Basic * * @covers Lcobucci\JWT\Token::getClaim */ public function getClaimShouldReturnTheClaimValueWhenItExists() { $token = new Token([], ['testing' => new Basic('testing', 'test')]); $this->assertEquals('test', $token->getClaim('testing')); }
public function deSerialize(Token $token) : Data { return Data::fromJsonString($token->getClaim('data')); }
/** * {@inheritDoc} */ private function shouldTokenBeRefreshed(Token $token) : bool { if (!$token->hasClaim(self::ISSUED_AT_CLAIM)) { return false; } return $this->timestamp() >= $token->getClaim(self::ISSUED_AT_CLAIM) + $this->refreshTime; }
/** * Get the unique key held within the blacklist. * * @param \Lcobucci\JWT\Token $token * * @return mixed */ public function getKey(Token $token) { return $token->getClaim($this->key); }
protected function getTokenCredentials(Token $token) { $creds = []; foreach ($this->config['payload'] as $property) { $creds[$property] = $token->getClaim($property); } return $creds; }
/** * @inheritdoc */ public function isValid(Token $token) { $signer = new Sha256(); $key = new Key($this->pathPublicKey); if (!$token->verify($signer, $key)) { throw new InvalidDefinitionException('Invalid token'); } $data = new ValidationData(); $data->setIssuer($token->getClaim('iss')); $data->setAudience($token->getClaim('aud')); $data->setId($token->getClaim('jti')); $isValid = $token->validate($data); if (!$isValid) { throw new AuthenticationExpiredException('The access token has expired'); } return $isValid; }