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