簡體   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