middlewares/http-authentication

实现基本和摘要HTTP认证的中间件

v2.1.2 2024-01-12 17:44 UTC

This package is auto-updated.

Last update: 2024-09-12 19:09:26 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

实现RFC 2617 Http Authentication的中间件。包含以下组件

需求

安装

此包可通过Composer安装并自动加载,作为middlewares/http-authentication

composer require middlewares/http-authentication

BasicAuthentication

基本访问认证是最简单的技术。

您需要提供一个包含所有可用用户用户名和密码的ArrayArrayAccess。键是用户名,值是密码。

Dispatcher::run([
    new Middlewares\BasicAuthentication([
        'username1' => 'password1',
        'username2' => 'password2'
    ])
]);

可选地,您可以提供一个Psr\Http\Message\ResponseFactoryInterface作为第二个参数,该参数将用于创建错误响应(401)。如果没有定义,将使用Middleware\Utils\Factory自动检测。

$responseFactory = new MyOwnResponseFactory();

$route = new Middlewares\BasicAuthentication($users, $responseFactory);

realm

区域值。默认为"登录"。

attribute

用于保存用户用户名的属性名称。如果没有定义,则不会保存。例如

Dispatcher::run([
    (new Middlewares\BasicAuthentication([
        'username1' => 'password1',
        'username2' => 'password2'
    ]))->attribute('username'),

    function ($request) {
        $username = $request->getAttribute('username');

        return new Response('Hello '.$username);
    }
]);

verifyHash

此选项使用password_verify验证密码。如果您不想以纯文本形式提供密码,则很有用。

$users = [
    'username' => password_hash('secret-password', PASSWORD_DEFAULT);
]

Dispatcher::run([
    (new Middlewares\BasicAuthentication($users))
        ->attribute('username')
        ->verifyHash(),

    function ($request) {
        $username = $request->getAttribute('username');

        return new Response('Hello '.$username);
    }
]);

DigestAuthentication

摘要访问认证比基本认证更安全。

构造函数签名与BasicAuthentication相同

$users = [
    'username1' => 'password1',
    'username2' => 'password2'
];
$responseFactory = new MyOwnResponseFactory();

Dispatcher::run([
    new Middlewares\DigestAuthentication($users, $responseFactory)
]);

realm

区域值。默认为"登录"。

attribute

用于保存用户用户名的属性名称。如果没有定义,则不会保存。

nonce

配置nonce值。如果没有定义,它将使用uniqid生成

请参阅CHANGELOG以获取有关最近更改的更多信息,并参阅CONTRIBUTING以获取贡献细节。

MIT许可(MIT)。有关更多信息,请参阅LICENSE