|
1 |
| -# zendframework/zend-expressive-authentication |
| 1 | +# Zend Expressive Authentication |
2 | 2 |
|
3 |
| -This component provides authentication abstraction and authentication middleware |
4 |
| -for PSR-7 applications. |
| 3 | +This component provides authentication abstraction using a middleware approach |
| 4 | +for [PSR-7](http://www.php-fig.org/psr/psr-7/) applications. |
| 5 | + |
| 6 | +The authentication is provided using the [AuthenticationMiddleware](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/AuthenticationMiddleware.php) |
| 7 | +class. This middleware consumes an [AuthenticationInterface](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/AuthenticationInterface.php) |
| 8 | +adapter to check if a [PSR-7](http://www.php-fig.org/psr/psr-7/) request is |
| 9 | +authenticated or not. If authenticated, the middleware executes the next |
| 10 | +delegate in the pipeline, passing a [UserInterface](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/UserInterface.php) |
| 11 | +object as attribute in the request. If the request is not authenticated, the |
| 12 | +middleware returns a `401 Unauthorized` response. |
| 13 | + |
| 14 | +The `Zend\Expressive\Authentication\UserInterface` is defined as follows: |
| 15 | + |
| 16 | +```php |
| 17 | +namespace Zend\Expressive\Authentication; |
| 18 | + |
| 19 | +interface UserInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Get the username |
| 23 | + * |
| 24 | + * @return string |
| 25 | + */ |
| 26 | + public function getUsername(): string; |
| 27 | + |
| 28 | + /** |
| 29 | + * Get all user roles |
| 30 | + * |
| 31 | + * @return string[] |
| 32 | + */ |
| 33 | + public function getUserRoles() : array; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +The `UserInterface` attribute in the PSR-7 request can be used for checking |
| 38 | +if a user has been authenticated or not, e.g. it can be used to verify the |
| 39 | +authorization level of a user (for this scope, it is used by [zend-expressive-authotization](https://github.com/zendframework/zend-expressive-authorization)). |
| 40 | + |
| 41 | +## Usage in the route |
| 42 | + |
| 43 | +The `AuthenticationMiddleware` can be used to authenticate a route. You just |
| 44 | +need to add the class name of the middleware in the pipeline of a route. |
| 45 | +As an example: |
| 46 | + |
| 47 | +```php |
| 48 | +$app->get('/admin/dashboard', [ |
| 49 | + Zend\Expressive\Authentication\AuthenticationMiddleware::class, |
| 50 | + Admin\Action\Dashboard::class |
| 51 | +], 'admin.dashboard'); |
| 52 | +``` |
| 53 | + |
| 54 | +In this example, the `AuthenticationMiddleware` is executed as first middleware |
| 55 | +of the route `admin.dashboard`. If the user is authenticated, the application |
| 56 | +executes the `Dashboard` action, otherwise it returns a `401 Unauthorized` |
| 57 | +response. |
| 58 | + |
| 59 | +## Choosing an authentication adapter |
| 60 | + |
| 61 | +You can choose an authentication adapter and a user repository through the |
| 62 | +service container configuration. |
| 63 | +You need to specify the service for authentication using the name `Zend\Expressive\Authentication\AuthenticationInterface` and the user registry |
| 64 | +using the service name `Zend\Expressive\Authentication\UserRepositoryInterface::class`. |
| 65 | + |
| 66 | +For instance, using `zend-servicemanager` you can easily configure these two |
| 67 | +services using `aliases`. Below, it's reported an example of configuration using |
| 68 | +the *HTTP Basic Access Authentication* adapter and the *htpasswd* file as user |
| 69 | +repository. |
| 70 | + |
| 71 | +```php |
| 72 | +// ConfigProvider.php |
| 73 | + |
| 74 | +use Zend\Expressive\Authentication\AuthenticationInterface; |
| 75 | +use Zend\Expressive\Authentication\UserRepositoryInterface; |
| 76 | + |
| 77 | +class ConfigProvider |
| 78 | +{ |
| 79 | + // ... |
| 80 | + |
| 81 | + public function getDependencies() : array |
| 82 | + { |
| 83 | + return [ |
| 84 | + 'aliases' => [ |
| 85 | + AuthenticationInterface::class => Basic\BasicAccess::class, |
| 86 | + UserRepositoryInterface::class => UserRepository\Htpasswd::class |
| 87 | + ], |
| 88 | + // ... |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + // ... |
| 93 | +} |
| 94 | +``` |
0 commit comments