【问题标题】:Using authentication on different method parameters (Restler 3)对不同的方法参数使用身份验证(Restler 3)
【发布时间】:2014-02-22 02:22:03
【问题描述】:

如果参数具有特定值,我想限制对方法的访问。让我们以这个类为例:

简单的.php:

class Simple
{
    function item($name)
    {
        if($name == "somerestricted")
        {
            // Here should be an authentication check (or somewhere else), hopefully, using an iAuthenticate class
            // Later, there will be a check using a database to determine if authentication will be required
            // So user/password may vary
            if($authenticated)
            {
                // Proceed
            }
            else
            {
                // ???
            }
        }
        else
        {
            echo "Hi!";
        }
    }
}

使用这个认证类:

基本身份验证.php:

class BasicAuthentication implements iAuthenticate
{
    const REALM = 'Restricted API';
    function __isAllowed()
    {
        if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
        {
            $user = $_SERVER['PHP_AUTH_USER'];
            $pass = $_SERVER['PHP_AUTH_PW'];
            if($user == 'laterfetched' && $pass == 'fromdatabase')
            {
                return true;
            }
        }
        header('WWW-Authenticate: Basic realm="'.self::REALM.'"');
        throw new RestException(401, 'Basic Authentication Required');
    }
}

Index.php(网关): addAuthenticationClass('BasicAuthentication'); $r->addAPIClass('简单'); $r->句柄();

simple/item 方法现在可以公开访问。但是,如果我将item 转换为protected 函数,每个 请求都需要身份验证。这不是我想做的。只有simple/item/somerestricted 需要身份验证。

那么有没有办法将iAuthenticate 限制为特定的参数值?如果没有,我该如何解决这个问题?

用户名和密码在生产使用中会有所不同(取决于给定的参数)。

我发现了这些相关问题:Restler 3.0 Basic AuthenticationLuracast Restler Authentication

我正在使用 Restler rc4。

【问题讨论】:

    标签: php authentication basic-authentication http-authentication restler


    【解决方案1】:

    您已将自己的 api 设为公开的,并且在用户通过身份验证时会增强结果

    一种方法如下。它在 Restler 中使用了一个隐藏的属性

    class Simple
    {
        /**
         * @var \Luracast\Restler\Restler
         */
        public $restler;
        /**
         * @access hybrid
         */
        function item($name)
        {
            if ($name == "somerestricted") {
                if ($this->restler->_authenticated) {
                    // Proceed
                } else {
                    // ???
                }
            } else {
                echo "Hi!";
            }
        }
    }
    

    另一种(推荐)方式是使用 iUseAuthentication 接口

    use Luracast\Restler\iUseAuthentication;
    
    class Simple implements iUseAuthentication
    {
        protected $authenticated;
    
        /**
         * @access hybrid
         */
        function item($name)
        {
            if ($name == "somerestricted") {
                if ($this->authenticated) {
                    // Proceed
                } else {
                    // ???
                }
            } else {
                echo "Hi!";
            }
        }
    
        public function __setAuthenticationStatus($isAuthenticated = false)
        {
            $this->authenticated = $isAuthenticated;
        }
    }
    

    【讨论】:

    • 如果用于身份验证的用户名/密码取决于$name参数怎么办?
    • 不需要,也不在上述用例中
    • 不是上述情况,对。但是如果...假设我想使用(为该项目)生成的用户名/密码来保护每个项目?这可能吗?
    • 在这种情况下,身份验证类将使用给定的用户名和密码识别用户,并设置用户类的静态属性。 Api 方法将首先获取请求的对象,然后将所有者与当前用户进行比较。如果用户不是所有者,它可以抛出异常,例如 403 Forbidden。你不是所有者
    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2021-07-19
    • 2011-07-28
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多