Middleware to implement RFC 2617 Http Authentication. Contains the following components:
- PHP >= 7.0
- A PSR-7 http library
- A PSR-15 middleware dispatcher
This package is installable and autoloadable via Composer as middlewares/http-authentication.
composer require middlewares/http-authentication
The Basic access authentication is the simplest technique.
Array
or ArrayAccess
with the usernames and passwords of all available users. The keys are the usernames and the values the passwords.
The realm value. By default is "Login".
The attribute name used to save the username of the user. If it's not defined, it wont be saved. Example:
$dispatcher = new Dispatcher([
(new Middlewares\BasicAuthentication([
'username1' => 'password1',
'username2' => 'password2'
]))->attribute('username'),
function ($request) {
$username = $request->getAttribute('username');
return new Response('Hello '.$username);
}
]);
$response = $dispatcher->dispatch(new ServerRequest());
A PSR-17 factory to create the 401
responses.
The Digest access authentication is more secure than basic.
Array
or ArrayAccess
with the usernames and passwords of all available users. The keys are the usernames and the values the passwords.
The realm value. By default is "Login".
The attribute name used to save the username of the user. If it's not defined, it wont be saved. Example:
To configure the nonce value. If its not defined, it's generated with uniqid
$dispatcher = new Dispatcher([
(new Middlewares\DigestAuthentication([
'username1' => 'password1',
'username2' => 'password2'
]))->attribute('username'),
function ($request) {
$username = $request->getAttribute('username');
return new Response('Hello '.$username);
}
]);
$response = $dispatcher->dispatch(new ServerRequest());
A PSR-17 factory to create the 401
responses.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.