Carla Sousa
Carla Sousa

Reputation: 433

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 :)

Upvotes: 3

Views: 1307

Answers (2)

MUHAMMAD AWAIS
MUHAMMAD AWAIS

Reputation: 67

in the

.htaccess

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Upvotes: 1

Mika Tuupola
Mika Tuupola

Reputation: 20377

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

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "users" => [
        "Carla" => "123"
    ]
]));

Upvotes: 1

Related Questions