AuthenticationClass 需要使用 UrlVersioning 进行复制 - Luracast Restler

AuthenticationClass needs duplicating with UrlVersioning - Luracast Restler

我想进行身份验证 class 并在无需复制我的安全代码的情况下对我的 APIS 进行版本控制。

我设置了 restler 并将以下内容添加到 index.php;

Defaults::setProperty('useUrlBasedVersioning', true);
$r->addAuthenticationClass('MyOrg\Security\APIAuth');

然后我在 public 文件夹之外的另一个文件夹中设置了身份验证 class。它不能独立工作,但我发现由于使用基于 UrlBased 的版本控制,我不得不在不同的命名空间中重复 class。

例如

MyOrd ---> Security ---> v1 ---> APIAuth.php

MyOrd ---> Security ---> v2 ---> APIAuth.php

我不想做上面的事情,但更简单的是

MyOrd ---> Security ---> APIAuth.php

我正在使用 Restler RC5,任何指导将不胜感激,或者这是 Restler 的错误。

也记录为 restler 项目的问题 https://github.com/Luracast/Restler/issues/433

只需实施 iProvideMultiVersionApi 和 return 授权 class 支持的最大版本,在您的情况下为 2。请参见下面的示例

namespace MyOrg\Security;

use Luracast\Restler\iAuthenticate;
use Luracast\Restler\iProvideMultiVersionApi;

class Auth implements iAuthenticate, iProvideMultiVersionApi{

    public function __isAllowed(){
        return isset($_GET['api_key']) && $_GET['api_key'] =='allow';
    }

    public function __getWWWAuthenticateString(){
        return 'Query';
    }

    /**
     * Maximum api version supported by the api class
     * @return int
     */
    public static function __getMaximumSupportedVersion()
    {
        return 2;
    }
}