mambo
mambo

Reputation: 5

SLIM BasicAuth no work

sorry for my english.

I test SLIM framework for my new project API REST.

I installed SLIM on my Apache CGI server online with certicate SSL : okay it works ! I can access my ressource from my computer (https://domaine.fr/v1/test for example)

I tested with basic auth by htaccess file. I tape user/password to have access my ressource : it works !

Now, I'd like to test with basic auth in SLIM with https://github.com/tuupola/slim-basic-auth

But it doesn't work ! it keeps asking me the login and password !

my htaccess file :

Satisfy any
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

my index.php (slim) :

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->add(new \Slim\Middleware\HttpBasicAuthentication(array(
    "realm" => "Here be dragons.",
    "users" => array(
        "root" => "t00r",
        "test" => "test"
    )
)));

I don't understand why it doesn't work.

Who can help to me ?

thank you

Upvotes: 0

Views: 1255

Answers (2)

mambo
mambo

Reputation: 5

sorry for my late reply, I started with Composer/Slim and I spent a sleepless night to test locally on my computer (Windows Seven). I installed Composer for Windows, Slim with Composer and tuupola/slim-basic-auth with Composer. I created two routes, one public and one private. And it works locally : "http: // mycomputer/ws-bots" and "http: // mycomputer/ws-bots/admin", I type "test/test" and the message "admin" is displayed.

So I tested on my VPS Linux (Centos) in two ways:

First way) I copied filezilla in /httpdocs/ws-bots/v2/ so I have:

- Vendor folder,
- Composer.json,
- composer.lock
- And composer.phar

In the vendor folder, there are:

- Compose folder,
- Ircmaxell folder,
- Slim folder,
- Tuupola folder,
- And autoload.php

I created a virtual host: https: // ws2-bots.faistescourses.fr => /httpdocs/ws-bots/v2/ vendor/Slim/slim: Okay

I changed autoload path in index.php "/var/www/vhosts/faistescourses.fr/httpdocs/ws-bots/v2/vendor/autoload.php": Okay

$app-> add (new \ Slim \ Middleware \ HttpBasicAuthentication (array (
"Path" => "/ admin"
"realm" => "Private"
"users" => array (
"root" => "t00r"
"test" => "test"
)
)));

And I can access : https: / /ws2-bots.faistescourses.fr : okay

$ app-> get ('/', function () {
print "Public";
});

If I test https://ws2-bots.faistescourses.fr/admin: not working

$ app-> get ("/ admin", function () {
print "Private";
});

I type "root/t00r" or "test/test" for login/password: it does not work! He asks me to type still login/password.

If I disable authentication with /* .. */, I can access /admin.

Second way) I have installed on my VPS, Composer, Slim with Composer and tuupola/slim-basic-auth with Composer in the "/var/www/vhosts/faistescourses.fr/httpdocs/ws-bots/v2/." I changed the path and autoload.php index.php as above. And I have the same problems. If I disable the authentication on with /../, it works.

So I do not understand, do I have a problem elsewhere? path?

You can test https: // ws2-bots.faistescourses.fr and https: // ws2-bots.faistescourses.fr/admin with "root/t00r" or "test/test".

Thank you for your help!

Upvotes: 0

Mika Tuupola
Mika Tuupola

Reputation: 20387

Just tested and here is working code for you. Note that since you are installing dependencies with composer it is a good idea to use composer autoloading.

require_once "../vendor/autoload.php";

$app = new \Slim\Slim();

$app->add(new \Slim\Middleware\HttpBasicAuthentication(array(
    "realm" => "Here be dragons.",
    "users" => array(
        "root" => "t00r",
        "test" => "test"
    )
)));

$app->get("/", function() {
    print "Foo";
});

$app->run();

Upvotes: 1

Related Questions