|
| 1 | +# zend-expressive-authentication-basic |
| 2 | + |
| 3 | +This component provides an HTTP Basic Authentication adapter for |
| 4 | +[zend-expressive-authentication](https://docs.zendframework.com/zend-expressive-authentication). |
| 5 | + |
| 6 | +HTTP Basic authentication utilizes the user-info section of the URL authority in |
| 7 | +order to provide credentials. While the HTTP specifications allow a single value |
| 8 | +for the user-info, most implementations require a `:`-separated credential, with |
| 9 | +the username first, and the password second; in fact, this is how browsers |
| 10 | +always send HTTP Basic credentials, as their prompts are always for the two |
| 11 | +values. As such, **this implementation expects both a username and password in |
| 12 | +the supplied credentials**. |
| 13 | + |
| 14 | +> ### Only use in trusted networks |
| 15 | +> |
| 16 | +> Since HTTP Basic transmits the credentials via the URL, it should only be used |
| 17 | +> within trusted networks, and never in public-facing sites, as the URL can be |
| 18 | +> sniffed by MITM proxies. |
| 19 | +
|
| 20 | +## Configuration |
| 21 | + |
| 22 | +To use the adapter, you will need to provide the following configuration: |
| 23 | + |
| 24 | +- A valid zend-expressive-authentication `UserRepositoryInterface` service in |
| 25 | + your DI container. This service will perform the actual work of validating the |
| 26 | + supplied credentials. |
| 27 | + |
| 28 | +- An HTTP Basic **realm**. This may be an arbitrary value, but is [required by |
| 29 | + the specification](https://tools.ietf.org/html/rfc7617#section-2). |
| 30 | + |
| 31 | +- A response factory. If you are using Expressive, this is already configured |
| 32 | + for you. |
| 33 | + |
| 34 | +As an example of configuration: |
| 35 | + |
| 36 | +```php |
| 37 | +// config/autoload/authentication.global.php |
| 38 | + |
| 39 | +use Zend\Expressive\Authentication\AdapterInterface; |
| 40 | +use Zend\Expressive\Authentication\Basic\BasicAccess; |
| 41 | +use Zend\Expressive\Authentication\UserRepositoryInterface; |
| 42 | +use Zend\Expressive\Authentication\UserRepository\PdoDatabase; |
| 43 | + |
| 44 | +return [ |
| 45 | + 'dependencies' => [ |
| 46 | + 'aliases' => [ |
| 47 | + // Use the default PdoDatabase user repository. This assumes |
| 48 | + // you have configured that service correctly. |
| 49 | + UserRepositoryInterface::class => PdoDatabase::class, |
| 50 | + |
| 51 | + // Tell zend-expressive-authentication to use the BasicAccess |
| 52 | + // adapter: |
| 53 | + AdapterInterface::class => BasicAccess::class, |
| 54 | + ], |
| 55 | + ], |
| 56 | + 'authentication' => [ |
| 57 | + 'realm' => 'api', |
| 58 | + ], |
| 59 | +]; |
| 60 | +``` |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +Whenever you need an authenticated user, you can place the |
| 65 | +zend-expressive-authentication `AuthenticationMiddleware` in your pipeline. |
| 66 | + |
| 67 | +### Globally |
| 68 | + |
| 69 | +If you need all routes to use authentication, add it globally. |
| 70 | + |
| 71 | +```php |
| 72 | +// In config/pipeline.php, within the callback: |
| 73 | + |
| 74 | +$app->pipe(Zend\Expressive\Authentication\AuthenticationMiddleware::class); |
| 75 | +``` |
| 76 | + |
| 77 | +### For an entire sub-path |
| 78 | + |
| 79 | +If you need all routes that begin with a particular sub-path to require |
| 80 | +authentication, use [path-segregation](https://docs.zendframework.com/zend-stratigility/v3/api/#path): |
| 81 | + |
| 82 | +```php |
| 83 | +// In config/pipeline.php. |
| 84 | +// In the import statements: |
| 85 | +use Zend\Expressive\Authentication\AuthenticationMiddleware; |
| 86 | + |
| 87 | +// In the callback: |
| 88 | +$app->pipe('/api', $factory->path( |
| 89 | + $factory->prepare(AuthenticationMiddleware::class) |
| 90 | +)); |
| 91 | +``` |
| 92 | + |
| 93 | +### For a specific route |
| 94 | + |
| 95 | +If you want to restrict access for a specific route, create a [route-specific |
| 96 | +middleware pipeline](https://docs.zendframework.com/zend-expressive/v3/cookbook/route-specific-pipeline/): |
| 97 | + |
| 98 | +```php |
| 99 | +// In config/routes.php, in the callback: |
| 100 | + |
| 101 | +$app->get( |
| 102 | + '/path/requiring/authentication', |
| 103 | + [ |
| 104 | + Zend\Expressive\Authentication\AuthenticationMiddleware::class, |
| 105 | + HandlerRequiringAuthentication::class, |
| 106 | + ] |
| 107 | +); |
| 108 | +``` |
0 commit comments