Skip to content

Commit

Permalink
Test cases for secret array (#149)
Browse files Browse the repository at this point in the history
* Add test cases for secrets as array (see #138)
* Make sure secret is either string or array
  • Loading branch information
tuupola committed Jan 26, 2019
1 parent b1f5928 commit ae3c7a4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use Closure;
use DomainException;
use InvalidArgumentException;
use Exception;
use Firebase\JWT\JWT;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -343,6 +344,11 @@ private function relaxed(array $relaxed): void
*/
private function secret($secret): void
{
if (false === is_array($secret) && false === is_string($secret)) {
throw new InvalidArgumentException(
'Secret must be either a string or an array of "kid" => "secret" pairs'
);
}
$this->options["secret"] = $secret;
}

Expand Down
111 changes: 87 additions & 24 deletions tests/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
class JwtAuthenticationTest extends TestCase
{
/* @codingStandardsIgnoreStart */
public static $token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJBY21lIFRvb3RocGljcyBMdGQiLCJpYXQiOjE0Mjg4MTk5NDEsImV4cCI6MTc0NDM1Mjc0MSwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoic29tZW9uZUBleGFtcGxlLmNvbSIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSIsImRlbGV0ZSJdfQ.YzPxtyHLqiJMUaPE6DzBonGUyqLlddxIisxSFk2Gk7Y";
public static $acmeToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImFjbWUifQ.eyJpc3MiOiJBY21lIFRvb3RocGljcyBMdGQiLCJpYXQiOiIxNDI4ODE5OTQxIiwiZXhwIjoiMTc0NDM1Mjc0MSIsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6InNvbWVvbmVAZXhhbXBsZS5jb20iLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiLCJkZWxldGUiXX0.yBhYlsMabKTh31taAiH8i2ScPMKm84jxIDNxft6EiTA";
public static $betaToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImJldGEifQ.eyJraWQiOiJiZXRhIiwiaXNzIjoiQmV0YSBTcG9uc29yc2hpcCBMdGQiLCJpYXQiOiIxNDI4ODE5OTQxIiwiZXhwIjoiMTc0NDM1Mjc0MSIsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6InNvbWVvbmVAZXhhbXBsZS5jb20iLCJzY29wZSI6WyJyZWFkIl19.msxcBx4_ZQtCkkjHyTDWDC0mac4cFNSxLqkzNL30JB8";
public static $expired = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJBY21lIFRvb3RocGljcyBMdGQiLCJpYXQiOjE0Mjg4MTk5NDEsImV4cCI6MTQ4MDcyMzIwMCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoic29tZW9uZUBleGFtcGxlLmNvbSIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSIsImRlbGV0ZSJdfQ.ZydGEHVmca4ofQRCuMOfZrUXprAoe5GcySg4I-lwIjc";
/* @codingStandardsIgnoreEnd */

public static $token_as_array = [
public static $acmeTokenArray = [
"iss" => "Acme Toothpics Ltd",
"iat" => "1428819941",
"exp" => "1744352741",
Expand All @@ -58,6 +59,15 @@ class JwtAuthenticationTest extends TestCase
"scope" => ["read", "write", "delete"]
];

public static $betaTokenArray = [
"iss" => "Beta Sponsorship Ltd",
"iat" => "1428819941",
"exp" => "1744352741",
"aud" => "www.example.com",
"sub" => "[email protected]",
"scope" => ["read"]
];

public function testShouldBeTrue()
{
$this->assertTrue(true);
Expand Down Expand Up @@ -90,7 +100,7 @@ public function testShouldReturn200WithTokenFromHeader()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("X-Token", "Bearer " . self::$token);
->withHeader("X-Token", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -115,7 +125,7 @@ public function testShouldReturn200WithTokenFromHeaderWithCustomRegexp()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("X-Token", self::$token);
->withHeader("X-Token", self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -141,7 +151,7 @@ public function testShouldReturn200WithTokenFromCookie()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withCookieParams(["nekot" => self::$token]);
->withCookieParams(["nekot" => self::$acmeToken]);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -162,11 +172,64 @@ public function testShouldReturn200WithTokenFromCookie()
$this->assertEquals("Success", $response->getBody());
}


public function testShouldReturn200WithSecretArray()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$betaToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
$response->getBody()->write("Success");
return $response;
};

$collection = new MiddlewareCollection([
new JwtAuthentication([
"secret" => [
"acme" =>"supersecretkeyyoushouldnotcommittogithub",
"beta" =>"anothersecretkeyfornevertocommittogithub"
]
])
]);

$response = $collection->dispatch($request, $default);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals("Success", $response->getBody());
}

public function testShouldReturn401WithSecretArray()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$betaToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
$response->getBody()->write("Success");
return $response;
};

$collection = new MiddlewareCollection([
new JwtAuthentication([
"secret" => [
"xxxx" =>"supersecretkeyyoushouldnotcommittogithub",
"yyyy" =>"anothersecretkeyfornevertocommittogithub"
]
])
]);

$response = $collection->dispatch($request, $default);
$this->assertEquals(401, $response->getStatusCode());
$this->assertEquals("", $response->getBody());
}

public function testShouldAlterResponseWithAfter()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -193,7 +256,7 @@ public function testShouldReturn401WithInvalidAlgorithm()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand Down Expand Up @@ -242,7 +305,7 @@ public function testShouldReturn400WithInvalidToken()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer invalid" . self::$token);
->withHeader("Authorization", "Bearer invalid" . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand Down Expand Up @@ -341,7 +404,7 @@ public function testShouldNotAllowInsecure()

$request = (new ServerRequestFactory)
->createServerRequest("GET", "http://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -362,7 +425,7 @@ public function testShoulAllowInsecure()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "http://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -387,7 +450,7 @@ public function testShouldRelaxInsecureInLocalhost()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "http://localhost/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -411,7 +474,7 @@ public function testShouldRelaxInsecureInExampleCom()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "http://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand All @@ -436,13 +499,13 @@ public function testShouldAttachToken()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$token = $request->getAttribute("token");
$acmeToken = $request->getAttribute("token");

$response = (new ResponseFactory)->createResponse();
$response->getBody()->write($token["iss"]);
$response->getBody()->write($acmeToken["iss"]);

return $response;
};
Expand All @@ -463,13 +526,13 @@ public function testShouldAttachCustomToken()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$token = $request->getAttribute("nekot");
$acmeToken = $request->getAttribute("nekot");

$response = (new ResponseFactory)->createResponse();
$response->getBody()->write($token["iss"]);
$response->getBody()->write($acmeToken["iss"]);

return $response;
};
Expand All @@ -491,7 +554,7 @@ public function testShouldCallAfter()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$dummy = null;

Expand All @@ -514,7 +577,7 @@ public function testShouldCallAfter()

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals("Success", $response->getBody());
$this->assertEquals(self::$token_as_array, (array) $dummy);
$this->assertEquals(self::$acmeTokenArray, (array) $dummy);
}

public function testShouldCallError()
Expand Down Expand Up @@ -606,7 +669,7 @@ public function testShouldReturn401FromAfter()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand Down Expand Up @@ -635,7 +698,7 @@ public function testShouldModifyRequestUsingBefore()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand Down Expand Up @@ -735,7 +798,7 @@ public function testShouldBindToMiddleware()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/")
->withHeader("Authorization", "Bearer " . self::$token);
->withHeader("Authorization", "Bearer " . self::$acmeToken);

$default = function (ServerRequestInterface $request) {
$response = (new ResponseFactory)->createResponse();
Expand Down Expand Up @@ -770,7 +833,7 @@ public function testShouldHandlePsr7()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/api")
->withHeader("X-Token", "Bearer " . self::$token);
->withHeader("X-Token", "Bearer " . self::$acmeToken);

$response = (new ResponseFactory)->createResponse();

Expand Down

0 comments on commit ae3c7a4

Please sign in to comment.