tracesoftwareinternational / elephant-guard
符合PSR-7和PSR-15 HTTP规范的中间件
Requires
- php: ^7.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- tuupola/callable-handler: ^1.0
- tuupola/http-factory: ^1.0
- webmozart/glob: ^4.1
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.26.0
- equip/dispatch: ^2.0
- overtrue/phplint: ^1.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.2
- zendframework/zend-diactoros: ^2.0
This package is not auto-updated.
Last update: 2024-09-29 05:08:17 UTC
README
该中间件旨在对一组特定的路由应用一个给定的AuthenticatorInterface
。
它可以与所有使用PSR-7或PSR-15风格中间件的框架一起使用。它已经与Slim Framework进行了测试。
致谢
非常感谢Mika Tuupola,他的结构化PSR-15兼容中间件给我们带来了灵感(想到slim-jwt-auth和slim-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)。有关更多信息,请参阅许可文件。