forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Add purging of invalid refresh tokens to command (laravel#1396)
* Delete invalid refresh tokens Delete refresh tokens referring to non-existing access tokens. * Use whereDoesntHave() instead * Add test for purge command * Update description and output of command * Update PurgeCommand.php Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
c2b93a7
commit 985577c
Showing
2 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Laravel\Passport\Tests\Feature\Console; | ||
|
||
use Laravel\Passport\AuthCode; | ||
use Laravel\Passport\RefreshToken; | ||
use Laravel\Passport\Tests\Feature\PassportTestCase; | ||
use Laravel\Passport\Token; | ||
|
||
class PurgeCommand extends PassportTestCase | ||
{ | ||
public function test_purge() | ||
{ | ||
$expired = now()->subDays(8); | ||
$notExpired = now(); | ||
|
||
$accessTokenExpired = Token::create(['id' => 'a', 'user_id' => 1, 'client_id' => 1, 'revoked' => 0, 'expires_at' => $expired]); | ||
$accessTokenRevoked = Token::create(['id' => 'b', 'user_id' => 1, 'client_id' => 1, 'revoked' => 1, 'expires_at' => $notExpired]); | ||
$accessTokenOk = Token::create(['id' => 'c', 'user_id' => 1, 'client_id' => 1, 'revoked' => 0, 'expires_at' => $notExpired]); | ||
|
||
$authCodeExpired = AuthCode::create(['id' => 'a', 'user_id' => 1, 'client_id' => 1, 'revoked' => 0, 'expires_at' => $expired]); | ||
$authCodeRevoked = AuthCode::create(['id' => 'b', 'user_id' => 1, 'client_id' => 1, 'revoked' => 1, 'expires_at' => $notExpired]); | ||
$authCodeOk = AuthCode::create(['id' => 'c', 'user_id' => 1, 'client_id' => 1, 'revoked' => 0, 'expires_at' => $notExpired]); | ||
|
||
$refreshTokenExpired = RefreshToken::create(['id' => 'a', 'access_token_id' => $accessTokenExpired->id, 'revoked' => 0, 'expires_at' => $expired]); | ||
$refreshTokenRevoked = RefreshToken::create(['id' => 'b', 'access_token_id' => $accessTokenRevoked->id, 'revoked' => 1, 'expires_at' => $notExpired]); | ||
$refreshTokenInvalidAccessToken = RefreshToken::create(['id' => 'c', 'access_token_id' => 'xyz', 'revoked' => 0, 'expires_at' => $notExpired]); | ||
$refreshTokenOk = RefreshToken::create(['id' => 'd', 'access_token_id' => $accessTokenOk->id, 'revoked' => 0, 'expires_at' => $notExpired]); | ||
|
||
$this->artisan('passport:purge'); | ||
|
||
$this->assertFalse(Token::whereKey($accessTokenExpired->id)->exists()); | ||
$this->assertFalse(Token::whereKey($accessTokenRevoked->id)->exists()); | ||
$this->assertTrue(Token::whereKey($accessTokenOk->id)->exists()); | ||
|
||
$this->assertFalse(AuthCode::whereKey($authCodeExpired->id)->exists()); | ||
$this->assertFalse(AuthCode::whereKey($authCodeRevoked->id)->exists()); | ||
$this->assertTrue(AuthCode::whereKey($authCodeOk->id)->exists()); | ||
|
||
$this->assertFalse(RefreshToken::whereKey($refreshTokenExpired->id)->exists()); | ||
$this->assertFalse(RefreshToken::whereKey($refreshTokenRevoked->id)->exists()); | ||
$this->assertFalse(RefreshToken::whereKey($refreshTokenInvalidAccessToken->id)->exists()); | ||
$this->assertTrue(RefreshToken::whereKey($refreshTokenOk->id)->exists()); | ||
} | ||
} |