符合PSR-7和PSR-15 HTTP规范的中间件

1.1.0 2019-07-15 13:29 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:08:17 UTC


README

Software License Build Status Latest Version

该中间件旨在对一组特定的路由应用一个给定的AuthenticatorInterface

它可以与所有使用PSR-7或PSR-15风格中间件的框架一起使用。它已经与Slim Framework进行了测试。

致谢

非常感谢Mika Tuupola,他的结构化PSR-15兼容中间件给我们带来了灵感(想到slim-jwt-authslim-basic-auth),Elephant Guard包含了他的部分工作,非常感谢Mika!

安装

使用composer安装最新版本。

$ composer require tracesoftwareinternational/elephgant-guard

用法

配置选项作为数组传递。唯一必需的参数是authenticator

有关更多信息,请参阅参数

参数

认证器

该库的主要目的是测试传入请求是否符合实现AuthenticatorInterface的类

例如,您可以使用基于随机数的认证

use TraceSoftware\Middleware\ElephantGuard\AuthenticatorInterface;
use TraceSoftware\Middleware\ElephantGuard

class RandomAuthenticator implements AuthenticatorInterface {
    public function __invoke(array $arguments) {
        return (bool)rand(0,1);
    }
    
    public function getLastError(): string
    {
        return "";
    }
}

$app = new Slim\App;

$app->add(new ElephantGuard([
    "path" => "/",
    "authenticator" => new RandomAuthenticator
]);

同样也可以使用匿名类声明来完成。

use TraceSoftware\Middleware\ElephantGuard\AuthenticatorInterface;
use TraceSoftware\Middleware\ElephantGuard

$app = new Slim\App;

$app->add(new ElephantGuard([
    "path" => "/",
    "authenticator" => new class implements AuthenticatorInterface {
    
        public function __invoke(\Psr\Http\Message\ServerRequestInterface $request): bool
        {
            return (bool) rand(0,1);
        }
        
        public function getLastError(): string
        {
            return "";
        }
    }
]);

路径

可选的path参数允许您指定网站的受保护部分。它可以是字符串或数组。您不需要指定每个URL。相反,将path设置视为文件夹。在下面的示例中,所有以/api开始的URL都将进行认证。

$app = new Slim\App;

$app->add(new \TraceSoftware\Middleware\ElephantGuard([
    "path" => "/api", /* or ["/admin", "/api"] */
    "authenticator" => new \MyCustomAuthenticator()
]));

忽略

使用可选的ignore参数,您可以对path参数进行例外处理。在下面的示例中,所有以/api/admin开始的URL都将进行认证,但/api/token/admin/ping除外,这些不会进行认证。

$app = new Slim\App;

$app->add(new \TraceSoftware\Middleware\ElephantGuard([
    "path" => ["/api", "/admin"],
    "ignore" => [
        "/api/token", 
        "/admin/ping"
    ],
    "authenticator" => new \MyCustomAuthenticator()
]));

您可以使用webmozart/glob支持的Glob语法来忽略带有URL参数的路由(如/products/123456

在认证成功之前调用

只有当认证成功并且调用下一个传入中间件之前时才会调用Before函数。您可以使用它来在传递给下一个传入中间件之前更改请求。如果返回值不是\Psr\Http\Message\RequestInterface,则返回值将被忽略。

$app = new Slim\App;

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "path" => "/admin",
    "authenticator" => new \MyCustomAuthenticator()
    "before" => function ($request, $arguments) {
        return $request->withAttribute("user", $arguments["user"]);
    }
]));

在认证成功后调用

只有当认证成功并且已经调用传入中间件堆栈后才会调用After函数。您可以使用它来在传递给堆栈中的下一个传出中间件之前更改响应。如果返回值不是\Psr\Http\Message\ResponseInterface,则返回值将被忽略。

$app = new Slim\App;

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "path" => "/admin",
    "authenticator" => new \MyCustomAuthenticator()
    "after" => function ($request, $response, $arguments) {
        return $response->withHeader("X-Brawndo", "plants crave");
    }
]));

当认证失败时设置响应体

默认情况下,Elephant Guard返回一个空的响应体和401响应,以及一个包含“参数”的数组。

参数将是一个包含两个索引的数组

  • message:失败的原因
  • authenticatorError:从提供的认证器检索到的最后一个错误

您可以通过提供错误处理程序来返回自定义体。这在需要更多关于认证失败原因的信息时非常有用。

$app = new Slim\App;

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "path" => "/api",
    "authenticator" => new \MyCustomAuthenticator()
    "error" => function ($response, array $arguments) {
        $data = [];
        $data["status"] = "error";
        $data["message"] = $arguments["message"];
        $data["additionalInformation"] = $arguments['authenticatorError'];
        return $response->write(json_encode($data, JSON_UNESCAPED_SLASHES));
    }
]));

测试

您可以使用 composer 手动运行测试。

$ composer test

许可协议

GNU通用公共许可证v3.0(GPL v3.0)。有关更多信息,请参阅许可文件