Skip to content

Commit 25f6155

Browse files
authored
Merge pull request #417 from codeigniter4/token-guides
2 parents 96c1d39 + 113312f commit 25f6155

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

docs/guides/api-tokens.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.

docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
* [Events](events.md)
1010
* [Testing](testing.md)
1111
* [Customization](customization.md)
12+
13+
Guides:
14+
* [Protecting an API with Access Tokens](guides/api-tokens.md)

src/Filters/TokenAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function before(RequestInterface $request, $arguments = null)
4848
'token' => $request->getHeaderLine(setting('Auth.authenticatorHeader')['tokens'] ?? 'Authorization'),
4949
]);
5050

51-
if (! $result->isOK()) {
51+
if (! $result->isOK() || (! empty($arguments) && $result->extraInfo()->tokenCant($arguments[0]))) {
5252
return redirect()->to('/login');
5353
}
5454

tests/Authentication/Filters/AbstractFilterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ static function ($routes): void {
6161
echo 'Open';
6262
});
6363
$routes->get('login', 'AuthController::login', ['as' => 'login']);
64+
$routes->get('protected-user-route', static function (): void {
65+
echo 'Protected';
66+
}, ['filter' => $this->alias . ':users-read']);
6467

6568
Services::injectMock('routes', $routes);
6669
}

tests/Authentication/Filters/TokenFilterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,27 @@ public function testRecordActiveDate(): void
6363
// Last Active should be greater than 'updated_at' column
6464
$this->assertGreaterThan(auth('tokens')->user()->updated_at, auth('tokens')->user()->last_active);
6565
}
66+
67+
public function testFiltersProtectsWithScopes(): void
68+
{
69+
/** @var User $user1 */
70+
$user1 = fake(UserModel::class);
71+
$token1 = $user1->generateAccessToken('foo', ['users-read']);
72+
/** @var User $user2 */
73+
$user2 = fake(UserModel::class);
74+
$token2 = $user2->generateAccessToken('foo', ['users-write']);
75+
76+
// User 1 should be able to access the route
77+
$this->withHeaders(['Authorization' => 'Bearer ' . $token1->raw_token])
78+
->get('protected-user-route');
79+
80+
// Last Active should be greater than 'updated_at' column
81+
$this->assertGreaterThan(auth('tokens')->user()->updated_at, auth('tokens')->user()->last_active);
82+
83+
// User 2 should NOT be able to access the route
84+
$result = $this->withHeaders(['Authorization' => 'Bearer ' . $token2->raw_token])
85+
->get('protected-user-route');
86+
87+
$result->assertRedirectTo('/login');
88+
}
6689
}

0 commit comments

Comments
 (0)