Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticator misinterpretation of "authorization" header #105

Closed
Mo0812 opened this issue Feb 24, 2021 · 5 comments
Closed

Authenticator misinterpretation of "authorization" header #105

Mo0812 opened this issue Feb 24, 2021 · 5 comments

Comments

@Mo0812
Copy link

Mo0812 commented Feb 24, 2021

Hey,
I work with Slim 4.7 and slim-basic-auth 3.3 and realize a Basic Auth Authentification with a custom authenticator:

middleware.php

...
$app->add(new \Tuupola\Middleware\HttpBasicAuthentication([
        "authenticator" => $app->getContainer()->get(BasicAuthMiddleware::class),
        "secure" => false,
        "error" => function (Response $response, array $arguments) {
            $data = [];
            $data["status"] = "AuthError";
            $data["message"] = $arguments["message"];
            $response->getBody()->write(json_encode($arguments, JSON_PRETTY_PRINT));
            $response->withHeader('Content-Type', 'application/json')
                ->withStatus(403);
            return $response;
        }
    ]));

authenticator.php

...
class BasicAuthMiddleware implements AuthenticatorInterface
{

    private $db_handler;

    public function __construct(DatabaseConnectionHandler $db_handler)
    {
        $this->db_handler = $db_handler;
    }

    public function __invoke(array $arguments): bool
    {
        $username = $arguments["user"];
        $pw = $arguments["password"];
        try {
            $result = $this->db_handler->query("SELECT * FROM user WHERE username = ? AND pw = ?;", array($username, $pw));

            if ($result->getSelectedRows() > 0) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            throw new DomainUnauthenticatedException("Unauthenticated login");
        }
    }
}

I have the following problem:

When I request a secured endpoint with the HTTP Header "Authentication: Basic ..." the arguments "user" and "password" in the $arguments variable of the Authenticator __invoke method are interpreted and used correctly.

When I access the endpoint from a JS frontend with fetch, which converts the custom HTTP Headers to lowercase, like "authentication: Basic ...", the request get rejected all the time.

I logged the $arguments input in the __invoke method and see a difference:

  • With "Authentication": Array([user] => admin, [password] => admin)
  • With "authentication": Array([user] => admin [password] => admin\x01j\xc8\x9cadmin:admin)

Even without fetch and JS the problem can easily reproduced with CURL by changing the spelling of "Authentication" to "authentication" as the HTTP Header field.

I am not using multiple users in authentication.

Has anyone an idea where this behavior is coming from or how I can workaround/fix it?

Regards

@tuupola
Copy link
Owner

tuupola commented Feb 24, 2021

It seems this code is somehow failing. Header names are case insensitive. Changing the header to lowercase should not affect anything. I will setup a test case and see what is happening.

@tuupola
Copy link
Owner

tuupola commented Mar 1, 2021

This seems to be an issue with slim/psr-7 which creates two Authorization headers when request has a lowercase header. When using some other PSR-7 implementation such as nyholm/psr7 and nyholm/psr7-server problem does not exist.

In other words quick workaround at the moment is:

$ composer remove slim/psr7
$ composer require nyholm/psr7
$ composer require nyholm/psr7-server

@Mo0812
Copy link
Author

Mo0812 commented Mar 3, 2021

Thanks for your research, the workaround fixes my problem for now. Do you already have created an issue for this in the slim/psr-7 repo?

@tuupola
Copy link
Owner

tuupola commented Mar 4, 2021

I did. It is the slimphp/Slim-Psr7#188.

@tuupola
Copy link
Owner

tuupola commented Sep 13, 2021

This has been fixed in slimphp/Slim-Psr7#195

@tuupola tuupola closed this as completed Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants