I'm trying out Restler to build web api for my new project.
One of requirements is simple authentication.
I found nice example on SO https://stackoverflow.com/a/7969250/965722 but it's for Restler 2.
Using manual I managed to convert that class to Restler 3.0
<?php
class BasicAuthentication implements iAuthenticate
{
const REALM = 'Restricted API';
public static $currentUser;
public static $requires = 'user';
public static $role = 'user';
public function __isAllowed()
{
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$roles = array('12345' => 'user', '67890' => 'admin');
if (!isset($pass) || !array_key_exists($pass, $roles))
{
return false;
}
static ::$role = $roles[$pass];
Resources::$accessControlFunction = 'AccessControl::verifyAccess';
return static ::$requires == static ::$role || static ::$role == 'admin';
}
header('WWW-Authenticate: Basic realm="' . self::REALM . '"');
throw new RestException(401, 'Basic Authentication Required');
}
/**
* @access private
*/
public static function verifyAccess(array $m)
{
$requires = isset($m['class']['AccessControl']['properties']['requires']) ? $m['class']['AccessControl']['properties']['requires'] : false;
return $requires ? static ::$role == 'admin' || static ::$role == $requires : true;
}
}
?>
My sample api class looks like so:
<?php
class Api
{
/**
* @url GET
* @url GET hello
* @url GET hello/{to}
*/
function hello($to = 'world')
{
return "Hello $to!";
}
/**
* @access protected
* @class AccessControl {@requires user}
*/
public function user()
{
return "protected api, only user and admin can access";
}
/**
* @access protected
* @class AccessControl {@requires admin}
*/
public function admin()
{
return "protected api, only admin can access";
}
}
any my index.php
<?php
require_once 'vendor/restler.php';
use Luracast\Restler\Restler;
$r = new Restler();
$r->addAPIClass('Api', '');
$r->addAuthenticationClass('BasicAuthentication');
$r->handle();
?>
When I navigate to my webpage standard login and password form shows, but even if I put correct username and password nothing happens - login window reopens.
I'm probably making some silly mistake, but I can't find it. This is my first attempt to basic authentication, so please be kind.
Do I need some special configuration on server?
EDIT:
It looks like I need some special config because my server runs as CGI/FastCGI.
I've tried adding code from this comment: http://php.net/manual/en/features.http-auth.php#106285
but I am unable to configure .htaccess correctly.
This is the default Restler htaccess file:
Options -MultiViews
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
and these lines are required by workaround from previous link to work:
RewriteEngine on
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
How to combine both?