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