Skip to content

Commit

Permalink
Merge pull request #3 from 200-0K/patch-1
Browse files Browse the repository at this point in the history
Added Bearer authorization header for the JWTValidateMiddleware
  • Loading branch information
jszobody authored Jan 8, 2024
2 parents 4dd8301 + 56a81a7 commit b725930
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/JwtValidateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ protected function parseAuthorizationHeader($header)
return $decodedParts[1];
}

if (strpos($header, "Bearer") === 0) {
list($tokenString) = sscanf($header, "Bearer %s");

return $tokenString;
}

// Otherwise we expect the token to be specific directly (not encoded) with the "Token" label
list($tokenString) = sscanf($header, "Token %s");

return $tokenString;
}
}
}
7 changes: 5 additions & 2 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ 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', 'Bearer baz');
$this->assertEquals("baz", $middleware->findJWT($request));

$request = new \Illuminate\Http\Request();
$request->headers->set('Authorization', 'Token baz');
$this->assertEquals("baz", $middleware->findJWT($request));
Expand Down Expand Up @@ -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'));
}
}
}

0 comments on commit b725930

Please sign in to comment.