|
| 1 | +# Authentication |
| 2 | + |
| 3 | +Authentication is the process by which an identity is presented to the application. |
| 4 | +It ensures that the entity making the request has the proper credentials to access the API. |
| 5 | + |
| 6 | +**Dotkernel API** identities are delivered to the application from the client through the `Authorization` request. |
| 7 | +If it is present, the application tries to find and assign the identity to the application. |
| 8 | +If it is not presented, Dotkernel API assigns a default `guest` identity, represented by an instance of the class `Mezzio\Authentication\UserInterface`. |
| 9 | + |
| 10 | +## Configuration |
| 11 | + |
| 12 | +Authentication in Dotkernel API is built around the `mezzio/mezzio-authentication-oauth2` component and is already configured out of the box. |
| 13 | +But if you want to dig more, the configuration is stored in `config/autoload/local.php` under the `authentication` key. |
| 14 | + |
| 15 | +> You can check the |
| 16 | +> [mezzio/mezzio-authentication-oauth2](https://docs.mezzio.dev/mezzio-authentication-oauth2/v1/intro/#configuration) |
| 17 | +> configuration part for more info. |
| 18 | +
|
| 19 | +## How it works |
| 20 | + |
| 21 | +Dotkernel API authentication system can be used for SPAs (single-page applications), mobile applications, and simple, token-based APIs. |
| 22 | +It allows each user of your application to generate API tokens for their accounts. |
| 23 | + |
| 24 | +The authentication happens through the middleware in the `Api\App\Middleware\AuthenticationMiddleware`. |
| 25 | + |
| 26 | +## Database |
| 27 | + |
| 28 | +When you install **Dotkernel API** for the first time, you need to run the migrations and seeders. |
| 29 | +All the tables required for authentication are automatically created and populated. |
| 30 | + |
| 31 | +In Dotkernel API, authenticated users come from either the `admin` or the `user` table. |
| 32 | +We choose to keep the admin table separated from the users to prevent users of the application from accessing sensitive data, which only the administrators of the application should access. |
| 33 | + |
| 34 | +The `oauth_clients` table is pre-populated with the default `admin` and `frontend` clients with the same password as their names (**we recommend you change the default passwords**). |
| 35 | + |
| 36 | +As you guessed each client serves to authenticate `admin` or `user`. |
| 37 | + |
| 38 | +Another table that is pre-populated is the `oauth_scopes` table, with the `api` scope. |
| 39 | + |
| 40 | +### Issuing API Tokens |
| 41 | + |
| 42 | +Token generation in Dotkernel API is done using the `password` `grant_type` scenario, which in this case allows authentication to an API using the user's credentials (generally a username and password). |
| 43 | + |
| 44 | +The client sends a POST request to the `/security/generate-token` with the following parameters: |
| 45 | + |
| 46 | +- `grant_type` = password. |
| 47 | +- `client_id` = column `name` from the `oauth_clients` table |
| 48 | +- `client_secret` = column `secret` from the `oauth_clients` table |
| 49 | +- `scope` = column `scope` from the `oauth_scopes` table |
| 50 | +- `username` = column `identity` from table `admin`/`user` |
| 51 | +- `password` = column `password` from table `admin`/`user` |
| 52 | + |
| 53 | +```shell |
| 54 | +POST /security/generate-token HTTP/1.1 |
| 55 | +Accept: application/json |
| 56 | +Content-Type: application/json |
| 57 | +{ |
| 58 | + "grant_type": "password", |
| 59 | + "client_id": "frontend", |
| 60 | + "client_secret": "frontend", |
| 61 | + "scope": "api", |
| 62 | + "username": "test@dotkernel.com", |
| 63 | + "password": "dotkernel" |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The server responds with a JSON as follows: |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "token_type": "Bearer", |
| 72 | + "expires_in": 86400, |
| 73 | + "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...", |
| 74 | + "refresh_token": "def5020087199939a49d0f2f818..." |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Next time when you make a request to the server to an authenticated endpoint, the client should use the `Authorization` header request. |
| 79 | + |
| 80 | +```shell |
| 81 | +GET /users/1 HTTP/1.1 |
| 82 | +Accept: application/json |
| 83 | +Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9... |
| 84 | +``` |
| 85 | + |
| 86 | +### Refreshing tokens |
| 87 | + |
| 88 | +Dotkernel API can refresh the access token, based on the expired access token's `refresh_token`. |
| 89 | + |
| 90 | +The clients need to send a `POST` request to the `/security/refresh-token` with the following request: |
| 91 | + |
| 92 | +```shell |
| 93 | +POST /security/refresh-token HTTP/1.1 |
| 94 | +Accept: application/json |
| 95 | +Content-Type: application/json |
| 96 | +{ |
| 97 | + "grant_type": "refresh_token", |
| 98 | + "client_id": "frontend", |
| 99 | + "client_secret": "frontend", |
| 100 | + "scope": "api", |
| 101 | + "refresh_token" : "def5020087199939a49d0f2f818..." |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +The server responds with a JSON as follows: |
| 106 | + |
| 107 | +```json |
| 108 | +{ |
| 109 | + "token_type": "Bearer", |
| 110 | + "expires_in": 86400, |
| 111 | + "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...", |
| 112 | + "refresh_token": "def5020087199939a49d0f2f818..." |
| 113 | +} |
| 114 | +``` |
0 commit comments