From 9ff5c45c58f6e2016481be97ab3f27721dc9d5c6 Mon Sep 17 00:00:00 2001 From: Faisal Date: Sat, 23 Dec 2023 22:42:33 +0300 Subject: [PATCH 1/4] Added Bearer for the JWTValidateMiddleware --- src/JwtValidateMiddleware.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/JwtValidateMiddleware.php b/src/JwtValidateMiddleware.php index c1376e1..191d314 100644 --- a/src/JwtValidateMiddleware.php +++ b/src/JwtValidateMiddleware.php @@ -74,9 +74,15 @@ protected function parseAuthorizationHeader($header) return $decodedParts[1]; } + if (strpos($header, "Bearer") === 0) { + list($tokenString) = sscanf($header, "Bearer %s"); + + return base64_decode($tokenString); + } + // Otherwise we expect the token to be specific directly (not encoded) with the "Token" label list($tokenString) = sscanf($header, "Token %s"); return $tokenString; } -} \ No newline at end of file +} From fa1a138e72646c37522dff53c979e984d05e552f Mon Sep 17 00:00:00 2001 From: Faisal Date: Sat, 23 Dec 2023 22:51:43 +0300 Subject: [PATCH 2/4] No need to be base64 decoded --- src/JwtValidateMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JwtValidateMiddleware.php b/src/JwtValidateMiddleware.php index 191d314..4e4ed89 100644 --- a/src/JwtValidateMiddleware.php +++ b/src/JwtValidateMiddleware.php @@ -77,7 +77,7 @@ protected function parseAuthorizationHeader($header) if (strpos($header, "Bearer") === 0) { list($tokenString) = sscanf($header, "Bearer %s"); - return base64_decode($tokenString); + return $tokenString; } // Otherwise we expect the token to be specific directly (not encoded) with the "Token" label From 044f9861679e0eee08efb975bf84355d9a442595 Mon Sep 17 00:00:00 2001 From: Faisal Date: Sun, 24 Dec 2023 07:02:52 +0300 Subject: [PATCH 3/4] Added testing for Bearer authorization header --- tests/MiddlewareTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index 5f47342..d35529d 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -73,12 +73,15 @@ public function testTokenInAuthorizationHeader() $request = new \Illuminate\Http\Request(); $request->headers->set('Authorization', 'Basic' . base64_encode('username:foobar')); - $this->assertEquals("foobar", $middleware->findJWT($request)); $request = new \Illuminate\Http\Request(); $request->headers->set('Authorization', 'Token baz'); $this->assertEquals("baz", $middleware->findJWT($request)); + + $request = new \Illuminate\Http\Request(); + $request->headers->set('Authorization', 'Token baz'); + $this->assertEquals("baz", $middleware->findJWT($request)); } public function testIdFromRouteName() @@ -118,4 +121,4 @@ public function testSpecifiedId() $this->expectExceptionMessage('The token is not identified with the expected ID'); $this->assertEquals("success", $middleware->handle($request, function() { return "success"; }, 'different-id')); } -} \ No newline at end of file +} From 56a81a7f007e91c4d7a670dbd7adb53138081add Mon Sep 17 00:00:00 2001 From: Faisal Date: Sun, 24 Dec 2023 07:06:40 +0300 Subject: [PATCH 4/4] :) --- tests/MiddlewareTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index d35529d..4238cc7 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -76,7 +76,7 @@ public function testTokenInAuthorizationHeader() $this->assertEquals("foobar", $middleware->findJWT($request)); $request = new \Illuminate\Http\Request(); - $request->headers->set('Authorization', 'Token baz'); + $request->headers->set('Authorization', 'Bearer baz'); $this->assertEquals("baz", $middleware->findJWT($request)); $request = new \Illuminate\Http\Request();