Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication failure with Slim Basic Auth

So I'm working with tuupola/slim-basic-auth, with Slim. I think I followed the steps correctly and yet something is not working properly.

So here is my index.php

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"users" => [
    "user" => "Carla",
    "password" => "123"
],
"realm" => "Protected",
"secure" => false,
"path" => '/',
"error" => function ($request, $response, $arguments) {
    $data = [];
    $data["status"] = "error";
    $data["message"] = $arguments["message"];
    return $response->write(json_encode($data, JSON_UNESCAPED_SLASHES));
}
]));

I'm testing it with Postman, I fill the authentication with my credentials, and I get a 401 Unauthorised. Any idea of what I'm doing wrong ?

Thanks in advance.

EDIT:

I finally saw what was wrong. Turns out it was missing this line:

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

on the .htaccess file.

Thanks for the help :)

like image 565
Carla Sousa Avatar asked Dec 06 '25 05:12

Carla Sousa


2 Answers

If username you want is Carla and password 123 then init the middleware like this:

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "users" => [
        "Carla" => "123"
    ]
]));
like image 78
Mika Tuupola Avatar answered Dec 07 '25 19:12

Mika Tuupola


in the

.htaccess

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
like image 33
MUHAMMAD AWAIS Avatar answered Dec 07 '25 18:12

MUHAMMAD AWAIS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!