Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit cfaaf5c

Browse files
committed
Merge branch 'hotfix/5'
Close #5
2 parents 6f25663 + 17ea1fe commit cfaaf5c

File tree

5 files changed

+200
-6
lines changed

5 files changed

+200
-6
lines changed

docs/book/intro.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/book/v1/auth-adapter.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Authentication adapters
2+
3+
The authentication adapters for `zend-expressive-authentication` implement the
4+
interface `Zend\Expressive\Authentication\AuthenticationInterface`:
5+
6+
```php
7+
namespace Zend\Expressive\Authentication;
8+
9+
use Psr\Http\Message\ServerRequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
interface AuthenticationInterface
13+
{
14+
/**
15+
* Authenticate the PSR-7 request and return a valid user,
16+
* or null if not authenticated
17+
*
18+
* @param ServerRequestInterface $request
19+
* @return UserInterface|null
20+
*/
21+
public function authenticate(ServerRequestInterface $request): ?UserInterface;
22+
23+
/**
24+
* Generate the unauthorized response
25+
*
26+
* @param ServerRequestInterface $request
27+
* @return ResponseInterface
28+
*/
29+
public function unauthorizedResponse(ServerRequestInterface $request): ResponseInterface;
30+
}
31+
```
32+
33+
This interface contains two method: `authenticate()` to check if a PSR-7
34+
request contains a valid credential, and `unauthorizedResponse()` to generate
35+
and return an unauthorized response.
36+
37+
We provide 4 authentication adapters:
38+
39+
- [zend-expressive-authentication-basic](https://github.com/zendframework/zend-expressive-authentication-basic),
40+
for [Basic Access Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication),
41+
supporting only `bcrypt` as the password hashing algorithm to ensure best
42+
security.
43+
- [zend-expressive-authentication-session](https://github.com/zendframework/zend-expressive-authentication-session),
44+
for authenticating username/password credential pairs and persisting them
45+
between requests via PHP sessions.
46+
- [zend-expressive-authentication-zendauthentication](https://github.com/zendframework/zend-expressive-authentication-zendauthentication),
47+
supporting the [zend-authentication](https://github.com/zendframework/zend-authentication)
48+
component.
49+
- [zend-expressive-authentication-oauth2](https://github.com/zendframework/zend-expressive-authentication-oauth2),
50+
supporting the [OAuth2](https://oauth.net/2/) authentication framework via the
51+
[league/oauth2-server](https://oauth2.thephpleague.com/) package.

docs/book/v1/intro.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
```

docs/book/v1/user-repository.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# User Repository
2+
3+
An authentication adapter can pull user information from a variety
4+
of repositories:
5+
6+
- an [htpasswd](https://httpd.apache.org/docs/current/programs/htpasswd.html) file
7+
- a database
8+
- a cache
9+
10+
zend-expressive-authentication provides an interface,
11+
`Zend\Expressive\Authentication\UserRepositoryInterface`, to access this user
12+
storage:
13+
14+
```php
15+
namespace Zend\Expressive\Authentication;
16+
17+
interface UserRepositoryInterface
18+
{
19+
/**
20+
* Authenticate the credential (username) using a password
21+
* or using only the credential string (e.g. token based credential)
22+
* It returns the authenticated user or null.
23+
*
24+
* @param string $credential can be also a token
25+
*/
26+
public function authenticate(string $credential, string $password = null) : ?UserInterface;
27+
28+
/**
29+
* Get the user roles if present.
30+
*
31+
* @param string $username
32+
* @return string[]
33+
*/
34+
public function getRolesFromUser(string $username) : array;
35+
}
36+
```
37+
38+
It contains two functions: `authenticate()` and `getRolesFromUser()`. The first
39+
is used to authenticate using the user's credential. If authenticated, the
40+
result will be a `UserInterface` instance, otherwise a null value is returned.
41+
42+
The second function is `getRolesFromUser()` and it specifies how to retrieve
43+
the roles for a user. If a user does not have roles, this function will return
44+
an empty array.

mkdocs.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ docs_dir: docs/book
22
site_dir: docs/html
33
pages:
44
- index.md
5-
- Introduction: intro.md
5+
- Introduction: v1/intro.md
6+
- Reference:
7+
- 'Authentication adapter': v1/auth-adapter.md
8+
- 'User repository': v1/user-repository.md
69
site_name: Authentication middleware
710
site_description: 'Authentication middleware for Expressive and PSR-7 applications'
811
repo_url: 'https://github.com/zendframework/zend-expressive-authentication'
9-
copyright: 'Copyright (c) 2017 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'
12+
copyright: 'Copyright (c) 2018 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'

0 commit comments

Comments
 (0)