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