Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/HttpBasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/* Just in case. */
$params = ["user" => null, "password" => null];

if (preg_match("/Basic\s+(.*)$/i", $request->getHeaderLine("Authorization"), $matches)) {
$explodedCredential = explode(":", base64_decode($matches[1]), 2);
if (count($explodedCredential) == 2) {
list($params["user"], $params["password"]) = $explodedCredential;
$authheader = explode(",", $request->getHeaderLine("Authorization"));
foreach ($authheader as $h) {
if (preg_match("/Basic\s+(.*)$/i", $h, $matches)) {
$explodedCredential = explode(":", base64_decode($matches[1]), 2);
if (count($explodedCredential) == 2) {
list($params["user"], $params["password"]) = $explodedCredential;
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/BasicAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ public function testShouldReturn200WithPassword()
$this->assertEquals("Success", $response->getBody());
}

public function testShouldReturn200WithMultipleHeaders()
{
$request = (new ServerRequestFactory)
->createServerRequest("GET", "https://example.com/admin/item")
->withHeader("Authorization", "Basic cm9vdDp0MDBy,Basic cm9vdDp0MDBy");

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

$auth = new HttpBasicAuthentication([
"path" => "/admin",
"realm" => "Protected",
"users" => [
"root" => "t00r",
"user" => "passw0rd"
]
]);

$next = function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write("Success");
return $response;
};

$response = $auth($request, $response, $next);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals("Success", $response->getBody());
}


public function testShouldReturn200WithOptions()
{
$request = (new ServerRequestFactory)
Expand Down