|
| 1 | +# Protecting an API with Access Tokens |
| 2 | + |
| 3 | +Access Tokens can be used to authenticate users for your own site, or when allowing third-party developers to access your API. When making requests using access tokens, the token should be included in the `Authorization` header as a `Bearer` token. |
| 4 | + |
| 5 | +Tokens are issued with the `generateAccessToken()` method on the user. This returns a `CodeIgniter\Shield\Entities\AccessToken` instance. Tokens are hashed using a SHA-256 algorithm before being saved to the database. The access token returned when you generate it will include a `raw_token` field that contains the plain-text, un-hashed, token. You should display this to your user at once so they have a chance to copy it somewhere safe, as this is the only time this will be available. After this request, there is no way to get the raw token. |
| 6 | + |
| 7 | +The `generateAccessToken()` method requires a name for the token. These are free strings and are often used to identify the user/device the token was generated from, like 'Johns MacBook Air'. |
| 8 | + |
| 9 | +```php |
| 10 | +$routes->get('/access/token', static function() { |
| 11 | + $token = auth()->user()->generateAccessToken(service('request')->getVar('token_name)); |
| 12 | + |
| 13 | + return json_encode(['token' => $token->raw_token]); |
| 14 | +}); |
| 15 | +``` |
| 16 | + |
| 17 | +You can access all of the user's tokens with the `accessTokens()` method on that user. |
| 18 | + |
| 19 | +```php |
| 20 | +$tokens = $user->accessTokens(); |
| 21 | +foreach($tokens as $token) { |
| 22 | + // |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Token Permissions |
| 27 | + |
| 28 | +Access tokens can be given `scopes`, which are basically permission strings, for the token. This is generally not the same as the permission the user has, but is used to specify the permissions on the API itself. If not specified, the token is granted all access to all scopes. This might be enough for a smaller API. |
| 29 | + |
| 30 | +```php |
| 31 | +return $user->generateAccessToken('token-name', ['users-read'])->raw_token; |
| 32 | +``` |
| 33 | + |
| 34 | +> **Note** |
| 35 | +> At this time, scope names should avoid using a colon (`:`) as this causes issues with the route filters being correctly recognized. |
| 36 | +
|
| 37 | +When handling incoming requests you can check if the token has been granted access to the scope with the `tokenCan()` method. |
| 38 | + |
| 39 | +```php |
| 40 | +if ($user->tokenCan('users-read')) { |
| 41 | + // |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Revoking Tokens |
| 46 | + |
| 47 | +Tokens can be revoked by deleting them from the database with the `revokeAccessToken($rawToken)` or `revokeAllAccessTokens()` methods. |
| 48 | + |
| 49 | +```php |
| 50 | +$user->revokeAccessToken($rawToken); |
| 51 | +$user->revokeAllAccessTokens(); |
| 52 | +``` |
| 53 | + |
| 54 | +## Protecting Routes |
| 55 | + |
| 56 | +The first way to specify which routes are protected is to use the `tokens` controller filter. |
| 57 | + |
| 58 | +For example, to ensure it protects all routes under the `/api` route group, you would use the `$filters` setting on `app/Config/Filters.php`. |
| 59 | + |
| 60 | +```php |
| 61 | +public $filters = [ |
| 62 | + 'tokens' => ['before' => ['api/*']], |
| 63 | +]; |
| 64 | +``` |
| 65 | + |
| 66 | +You can also specify the filter should run on one or more routes within the routes file itself: |
| 67 | + |
| 68 | +```php |
| 69 | +$routes->group('api', ['filter' => 'tokens'], function($routes) { |
| 70 | + // |
| 71 | +}); |
| 72 | +$routes->get('users', 'UserController::list', ['filter' => 'tokens:users-read']); |
| 73 | +``` |
| 74 | + |
| 75 | +When the filter runs, it checks the `Authorization` header for a `Bearer` value that has the raw token. It then hashes the raw token and looks it up in the database. Once found, it can determine the correct user, which will then be available through an `auth()->user()` call. |
| 76 | + |
| 77 | +> **Note** |
| 78 | +> Currently only a single scope can be used on a route filter. If multiple scopes are passed in, only the first one is checked. |
0 commit comments