Skip to content

Added revokeAccessTokenBySecret #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ Typically, the plain text token is retrieved from the request's headers as part
process. If you need to revoke the token for another user as an admin, and don't have access to the
token, you would need to get the user's access tokens and delete them manually.

If you dont 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.

```php
$user->revokeAccessTokenBySecret($secret);
```

You can revoke all access tokens with the `revokeAllAccessTokens()` method.

```php
Expand Down
3 changes: 2 additions & 1 deletion docs/guides/api_tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ if ($user->tokenCan('users-read')) {

### Revoking Tokens

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

```php
$user->revokeAccessToken($rawToken);
$user->revokeAccessTokenBySecret($secret);
$user->revokeAllAccessTokens();
```

Expand Down
11 changes: 11 additions & 0 deletions src/Authentication/Traits/HasAccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public function revokeAccessToken(string $rawToken): void
$identityModel->revokeAccessToken($this, $rawToken);
}

/**
* Delete any access tokens for the given secret token.
*/
public function revokeAccessTokenBySecret(string $secretToken): void
{
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);

$identityModel->revokeAccessTokenBySecret($this, $secretToken);
}

/**
* Revokes all access tokens for this user.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Models/UserIdentityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ public function revokeAccessToken(User $user, string $rawToken): void
$this->checkQueryReturn($return);
}

/**
* Delete any access tokens for the given secret token.
*/
public function revokeAccessTokenBySecret(User $user, string $secretToken): void
{
$this->checkUserId($user);

$return = $this->where('user_id', $user->id)
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
->where('secret', $secretToken)
->delete();

$this->checkQueryReturn($return);
}

/**
* Revokes all access tokens for this user.
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/Authentication/HasAccessTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ public function testRevokeAccessToken(): void
$this->assertCount(0, $this->user->accessTokens());
}

public function testRevokeAccessTokenBySecret(): void
{
$token = $this->user->generateAccessToken('foo');

$this->assertCount(1, $this->user->accessTokens());

$this->user->revokeAccessTokenBySecret($token->secret);

$this->assertCount(0, $this->user->accessTokens());
}

public function testRevokeAllAccessTokens(): void
{
$this->user->generateAccessToken('foo');
Expand Down