Restler 3.0 Basic Authentication

Я пытаюсь Рестлер создать веб-API для моего нового проекта.
Одним из требований является простая аутентификация.
Я нашел хороший пример на SO /questions/7963093/autentifikatsiya-luracast-restler/7963102#7963102 но это для Restler 2. С помощью руководства мне удалось преобразовать этот класс в 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;
    }
}
?>

Мой пример класса API выглядит так:

<?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";
    }
}

любой мой index.php

<?php
require_once 'vendor/restler.php';
use Luracast\Restler\Restler;

$r = new Restler();
$r->addAPIClass('Api', '');
$r->addAuthenticationClass('BasicAuthentication');
$r->handle();
?>

Когда я перехожу на мою веб-страницу, отображается стандартная форма логина и пароля, но даже если я введу правильное имя пользователя и пароль, ничего не происходит - окно входа снова открывается.

Возможно, я делаю какую-то глупую ошибку, но не могу ее найти. Это моя первая попытка базовой аутентификации, поэтому будьте добры.

Нужна ли специальная конфигурация на сервере?

РЕДАКТИРОВАТЬ:
Похоже, мне нужна особая конфигурация, потому что мой сервер работает как CGI/FastCGI.
Я попытался добавить код из этого комментария: http://php.net/manual/en/features.http-auth.php но я не могу правильно настроить.htaccess.
Это файл htaccess Рестлера по умолчанию:

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>

и эти строки необходимы для обхода предыдущей ссылки на работу:

RewriteEngine on
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

Как совместить оба?

1 ответ

Решение

Я не знаю почему, но я должен был реализовать iUseAuthentication в моем BasicAuthentication учебный класс. (в документации нет ничего, вероятно, потому что это релиз RC, а документы и образцы изменены).
Таким образом, все начало работать.
Также я изменил htaccess так:

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]
    RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
</IfModule>
<IfModule mod_php5.c>
    php_flag display_errors Off
</IfModule>

Надеюсь, это поможет кому-то:)

РЕДАКТИРОВАТЬ:
Я нашел свой код и выкладываю его здесь, может быть, кто-то найдет его полезным.

<?php
use \Luracast\Restler\iAuthenticate;
use \Luracast\Restler\Resources;
class BasicAuthentication implements iAuthenticate
{
    const REALM = 'Restricted API';
    public static $requires = 'user';
    public static $role = 'user';

    public function __isAllowed()
    {
        //set http auth headers for apache+php-cgi work around
        if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches))
        {
            list($name, $password) = explode(':', base64_decode($matches[1]));
            $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
            $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
        }

        //set http auth headers for apache+php-cgi work around if variable gets renamed by apache
        if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches))
        {
            list($name, $password) = explode(':', base64_decode($matches[1]));
            $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
            $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
        }
        if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
        {
            $user = $_SERVER['PHP_AUTH_USER'];
            $pass = $_SERVER['PHP_AUTH_PW'];

            $roles = array('12345' => 'user', '67890' => 'admin');

            if (!isset($pass) || !array_key_exists($pass, $roles))
            {
                return false;
            }

            static ::$role = $roles[$pass];
            Resources::$accessControlFunction = 'BasicAuthentication::verifyAccess';
            $x = static ::$requires == static ::$role || static ::$role == 'admin';

                        $file = 'a.txt';
            $current = file_get_contents($file);
            $current .= static ::$requires." ".static::$role . "\n";
            file_put_contents($file, $current);

            return $x;
        }
        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']['BasicAuthentication']['properties']['requires']) ? $m['class']['BasicAuthentication']['properties']['requires'] : false;

                                $file = 'a.txt';
            $current = file_get_contents($file);
            $current .= $requires." - ".static::$role . "\n";
            file_put_contents($file, $current);
        return $requires ? static ::$role == 'admin' || static ::$role == $requires : true;
    }
}
?>

и мой пример класса API:

<?php
class Api implements iUseAuthentication
{
    private $_authenticated = false;
    /**
     * This method will be called first for filter classes and api classes so
     * that they can respond accordingly for filer method call and api method
     * calls
     *
     *
     * @param bool $isAuthenticated passes true when the authentication is
     *                              done, false otherwise
     *
     * @return mixed
     */
    public function __setAuthenticationStatus($isAuthenticated = false)
    {
        $this->_authenticated = $isAuthenticated;
    }

    /**
     * @access protected
     * @class  BasicAuthentication {@requires user}
     */
    public function user()
    {
        return "protected api, only user and admin can access";
    }
    /**
     * @access protected
     * @class  BasicAuthentication {@requires admin}
     */
    public function admin()
    {
        return "protected api, only admin can access";
    }
}
?>
Другие вопросы по тегам