protected static function authorization() { $token = new Emarref\Jwt\Token(); $parameter = new Emarref\Jwt\HeaderParameter\Custom('typ', 'JWT'); $token->addHeader($parameter, true); $token->addClaim(new Emarref\Jwt\Claim\Expiration(new \DateTime(self::$duration))); $jwt = new Emarref\Jwt\Jwt(); $algorithm = new Emarref\Jwt\Algorithm\Hs256(self::$appSecret); $encryption = Emarref\Jwt\Encryption\Factory::create($algorithm); $serializedToken = $jwt->serialize($token, $encryption); return $serializedToken; }
/** * Verify the token * @param Request $request * @return bool */ public function isAuthorised(Request $request) { $header = $request->headers->get('Authorization'); $context = new Context(EncryptionFactory::create($this->algorithm)); if ($header) { $this->token = $this->jwt->deserialize($header); } if ($this->algorithm instanceof None) { return false; } if ($this->isExpired() || !$this->hasUserId() || !$this->isAllowedAppId()) { return false; } try { return $this->jwt->verify($this->token, $context); } catch (VerificationException $e) { return false; } }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Algorithm of class "Emarref\Jwt\Encryption\UnknownAlgorithmStub" is neither symmetric or asymmetric. */ public function testUnknownEncryption() { $algorithm = new UnknownAlgorithmStub(); Factory::create($algorithm); }
/** * @test */ public function givenNoneAlgorithm_returnFalse() { $auth = new JwtTokenAuthenticator($jwt = new Jwt(), new None(), $this->appIds); $this->assertFalse($auth->isAuthorised(new MockTokenRequest($jwt->serialize(new Token(), EncryptionFactory::create(new None()))))); }