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

Commit ffc6038

Browse files
committed
Merge pull request #5 from ezimuel/feature/doc
Added intro, authentication adapter and user repository docs
2 parents 6f25663 + 6106434 commit ffc6038

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed

docs/book/auth-adapter.md

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

docs/book/intro.md

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,94 @@
1-
# zendframework/zend-expressive-authentication
1+
# Zend Expressive Authentication
22

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+
```

docs/book/user-repository.md

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

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ site_dir: docs/html
33
pages:
44
- index.md
55
- Introduction: intro.md
6+
- 'Authentication adapter': auth-adapter.md
7+
- 'User repository': user-repository.md
68
site_name: Authentication middleware
79
site_description: 'Authentication middleware for Expressive and PSR-7 applications'
810
repo_url: 'https://github.com/zendframework/zend-expressive-authentication'

0 commit comments

Comments
 (0)