繁体   English   中英

在不同的方法参数上使用身份验证(Restler 3)

[英]Using authentication on different method parameters (Restler 3)

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

Simple.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!";
        }
    }
}

使用此身份验证类:

BasicAuthentication.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(gateway):addAuthenticationClass('BasicAuthentication'); $ R-> addAPIClass( '简单'); $ R->手柄();

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

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

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

我发现了以下相关问题: Restler 3.0基本身份验证Luracast Restler身份验证

我正在使用Restler rc4。

你已经使你的混合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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM