timtegeler/guardian

0.1.0 2017-02-17 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:22:24 UTC


README

Build Status Coverage Status GitHub license

Guardian 提供了一个身份验证后端与 PSR-15 中间件堆栈之间的适配器

  • 与 PSR-15 中间件接口兼容
  • 使用简单接口适配您的身份验证后端
<?php

// create a new authentication backend which implements the AuthenticationInterface
$authenticationBackend = new AuthenticationBackend();

// using e.g. mindplay-dk/middleman as a dispatcher for the middleware stack
$response = (new Dispatcher(
    [   
        // inject Guardian with the authentication backend instance
        new Guardian($authenticationBackend),
        // ... more middlwares e.g. a router
        new Router()
    ]
))->dispatch($request);

身份验证后端

Guardian 的重点是适配 PSR-15 中间件堆栈的身份验证后端。

这意味着 Guardian 本身无法提供身份验证,例如 基本访问身份验证。但 Guardian 提供了一个简单的接口,该接口可以由身份验证后端实现。接口由两个方法组成。

<?php

interface AuthenticationInterface
{

    /**
     * @param ServerRequestInterface $request
     * @return bool
     */
    public function authenticate(ServerRequestInterface $request);
    
    /**
    * @return ResponseInterface
    */
    public function getAuthenticationFailedResponse();

}

authenticate

authenticate 方法接收当前请求作为参数,必须返回一个 boolean(代表 access approvedaccess denied)。

getAuthenticationFailedResponse

getAuthenticationFailedResponse 方法必须返回一个 ResponseInterface 实例。当 Guardian 在 access denied 的情况下,会调用该方法以返回一个 ResponseInterface 实例到中间件管道。由于需要自定义属性,因此由身份验证后端负责提供合适的 ResponseInterface 实例。

例如,一个支持 基本访问身份验证 的身份验证后端,“应该返回一个包含 HTTP 401 未授权状态WWW-Authenticate 字段 的响应头。基本身份验证(最常用)的 WWW-Authenticate 字段构造如下: WWW-Authenticate: Basic realm="User Visible Realm"" [WIKI]