Skip to content

Commit 7d6c685

Browse files
authored
Merge pull request #858 from WaaaghNL/develop
feat: added `revokeAccessTokenBySecret()`
2 parents 82f0ee5 + 18517d2 commit 7d6c685

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

docs/guides/api_tokens.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ if ($user->tokenCan('users-read')) {
4747

4848
### Revoking Tokens
4949

50-
Tokens can be revoked by deleting them from the database with the `revokeAccessToken($rawToken)` or `revokeAllAccessTokens()` methods.
50+
Tokens can be revoked by deleting them from the database with the `revokeAccessToken($rawToken)`, `revokeAccessTokenBySecret($secret)` or `revokeAllAccessTokens()` methods.
5151

5252
```php
5353
$user->revokeAccessToken($rawToken);
54+
$user->revokeAccessTokenBySecret($secret);
5455
$user->revokeAllAccessTokens();
5556
```
5657

docs/references/authentication/tokens.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Typically, the plain text token is retrieved from the request's headers as part
5656
process. If you need to revoke the token for another user as an admin, and don't have access to the
5757
token, you would need to get the user's access tokens and delete them manually.
5858

59+
If you don't have the raw token usable to remove the token there is the possibility to remove it using the tokens secret thats stored in the database. It's possible to get a list of all tokens with there secret using the `accessTokens()` function.
60+
61+
```php
62+
$user->revokeAccessTokenBySecret($secret);
63+
```
64+
5965
You can revoke all access tokens with the `revokeAllAccessTokens()` method.
6066

6167
```php

src/Authentication/Traits/HasAccessTokens.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public function revokeAccessToken(string $rawToken): void
4747
$identityModel->revokeAccessToken($this, $rawToken);
4848
}
4949

50+
/**
51+
* Delete any access tokens for the given secret token.
52+
*/
53+
public function revokeAccessTokenBySecret(string $secretToken): void
54+
{
55+
/** @var UserIdentityModel $identityModel */
56+
$identityModel = model(UserIdentityModel::class);
57+
58+
$identityModel->revokeAccessTokenBySecret($this, $secretToken);
59+
}
60+
5061
/**
5162
* Revokes all access tokens for this user.
5263
*/

src/Models/UserIdentityModel.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,21 @@ public function revokeAccessToken(User $user, string $rawToken): void
456456
$this->checkQueryReturn($return);
457457
}
458458

459+
/**
460+
* Delete any access tokens for the given secret token.
461+
*/
462+
public function revokeAccessTokenBySecret(User $user, string $secretToken): void
463+
{
464+
$this->checkUserId($user);
465+
466+
$return = $this->where('user_id', $user->id)
467+
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
468+
->where('secret', $secretToken)
469+
->delete();
470+
471+
$this->checkQueryReturn($return);
472+
}
473+
459474
/**
460475
* Revokes all access tokens for this user.
461476
*/

tests/Authentication/HasAccessTokensTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public function testRevokeAccessToken(): void
101101
$this->assertCount(0, $this->user->accessTokens());
102102
}
103103

104+
public function testRevokeAccessTokenBySecret(): void
105+
{
106+
$token = $this->user->generateAccessToken('foo');
107+
108+
$this->assertCount(1, $this->user->accessTokens());
109+
110+
$this->user->revokeAccessTokenBySecret($token->secret);
111+
112+
$this->assertCount(0, $this->user->accessTokens());
113+
}
114+
104115
public function testRevokeAllAccessTokens(): void
105116
{
106117
$this->user->generateAccessToken('foo');

0 commit comments

Comments
 (0)