Skip to content

Commit eccd7d7

Browse files
committed
Merge branch '12.x' into 13.x
2 parents f712492 + 3a47efb commit eccd7d7

File tree

3 files changed

+112
-26
lines changed

3 files changed

+112
-26
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Release Notes
22

3-
## [Unreleased](https://github.com/laravel/passport/compare/v12.2.0...12.x)
3+
## [Unreleased](https://github.com/laravel/passport/compare/v12.2.1...12.x)
4+
5+
## [v12.2.1](https://github.com/laravel/passport/compare/v12.2.0...v12.2.1) - 2024-07-10
6+
7+
* [12.x] Fix purge command by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/passport/pull/1772
48

59
## [v12.2.0](https://github.com/laravel/passport/compare/v12.1.0...v12.2.0) - 2024-04-17
610

src/Console/PurgeCommand.php

+16-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Passport\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Support\Carbon;
78
use Laravel\Passport\Passport;
89
use Symfony\Component\Console\Attribute\AsCommand;
@@ -18,7 +19,7 @@ class PurgeCommand extends Command
1819
protected $signature = 'passport:purge
1920
{--revoked : Only purge revoked tokens and authentication codes}
2021
{--expired : Only purge expired tokens and authentication codes}
21-
{--hours= : The number of hours to retain expired tokens}';
22+
{--hours=168 : The number of hours to retain expired tokens}';
2223

2324
/**
2425
* The console command description.
@@ -32,33 +33,23 @@ class PurgeCommand extends Command
3233
*/
3334
public function handle()
3435
{
35-
$expired = $this->option('hours')
36-
? Carbon::now()->subHours($this->option('hours'))
37-
: Carbon::now()->subDays(7);
36+
$revoked = $this->option('revoked') || ! $this->option('expired');
3837

39-
if (($this->option('revoked') && $this->option('expired')) ||
40-
(! $this->option('revoked') && ! $this->option('expired'))) {
41-
Passport::token()->where('revoked', 1)->orWhereDate('expires_at', '<', $expired)->delete();
42-
Passport::authCode()->where('revoked', 1)->orWhereDate('expires_at', '<', $expired)->delete();
43-
Passport::refreshToken()->where('revoked', 1)->orWhereDate('expires_at', '<', $expired)->delete();
38+
$expired = $this->option('expired') || ! $this->option('revoked')
39+
? Carbon::now()->subHours($this->option('hours'))
40+
: false;
4441

45-
$this->option('hours')
46-
? $this->components->info('Purged revoked items and items expired for more than '.$this->option('hours').' hours.')
47-
: $this->components->info('Purged revoked items and items expired for more than seven days.');
48-
} elseif ($this->option('revoked')) {
49-
Passport::token()->where('revoked', 1)->delete();
50-
Passport::authCode()->where('revoked', 1)->delete();
51-
Passport::refreshToken()->where('revoked', 1)->delete();
42+
$constraint = fn (Builder $query) => $query
43+
->when($revoked, fn () => $query->orWhere('revoked', true))
44+
->when($expired, fn () => $query->orWhere('expires_at', '<', $expired));
5245

53-
$this->components->info('Purged revoked items.');
54-
} elseif ($this->option('expired')) {
55-
Passport::token()->whereDate('expires_at', '<', $expired)->delete();
56-
Passport::authCode()->whereDate('expires_at', '<', $expired)->delete();
57-
Passport::refreshToken()->whereDate('expires_at', '<', $expired)->delete();
46+
Passport::token()->where($constraint)->delete();
47+
Passport::authCode()->where($constraint)->delete();
48+
Passport::refreshToken()->where($constraint)->delete();
5849

59-
$this->option('hours')
60-
? $this->components->info('Purged items expired for more than '.$this->option('hours').' hours.')
61-
: $this->components->info('Purged items expired for more than seven days.');
62-
}
50+
$this->components->info(sprintf('Purged %s.', implode(' and ', array_filter([
51+
$revoked ? 'revoked items' : null,
52+
$expired ? "items expired for more than {$expired->longAbsoluteDiffForHumans()}" : null,
53+
]))));
6354
}
6455
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Console;
4+
5+
use Illuminate\Support\Carbon;
6+
use Illuminate\Support\Facades\DB;
7+
use Orchestra\Testbench\Concerns\WithWorkbench;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class PurgeCommand extends TestCase
11+
{
12+
use WithWorkbench;
13+
14+
public function test_it_can_purge_tokens()
15+
{
16+
$this->travelTo(Carbon::create(2000, 1, 8));
17+
18+
$query = DB::pretend(function () {
19+
$this->artisan('passport:purge')
20+
->expectsOutputToContain('Purged revoked items and items expired for more than 1 week.');
21+
});
22+
23+
$this->assertSame([
24+
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
25+
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
26+
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
27+
], array_column($query, 'query'));
28+
}
29+
30+
public function test_it_can_purge_revoked_tokens()
31+
{
32+
$query = DB::pretend(function () {
33+
$this->artisan('passport:purge', ['--revoked' => true])
34+
->expectsOutputToContain('Purged revoked items.');
35+
});
36+
37+
$this->assertSame([
38+
'delete from "oauth_access_tokens" where ("revoked" = 1)',
39+
'delete from "oauth_auth_codes" where ("revoked" = 1)',
40+
'delete from "oauth_refresh_tokens" where ("revoked" = 1)',
41+
], array_column($query, 'query'));
42+
}
43+
44+
public function test_it_can_purge_expired_tokens()
45+
{
46+
$this->travelTo(Carbon::create(2000, 1, 8));
47+
48+
$query = DB::pretend(function () {
49+
$this->artisan('passport:purge', ['--expired' => true])
50+
->expectsOutputToContain('Purged items expired for more than 1 week.');
51+
});
52+
53+
$this->assertSame([
54+
'delete from "oauth_access_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')',
55+
'delete from "oauth_auth_codes" where ("expires_at" < \'2000-01-01 00:00:00\')',
56+
'delete from "oauth_refresh_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')',
57+
], array_column($query, 'query'));
58+
}
59+
60+
public function test_it_can_purge_revoked_and_expired_tokens()
61+
{
62+
$this->travelTo(Carbon::create(2000, 1, 8));
63+
64+
$query = DB::pretend(function () {
65+
$this->artisan('passport:purge', ['--revoked' => true, '--expired' => true])
66+
->expectsOutputToContain('Purged revoked items and items expired for more than 1 week.');
67+
});
68+
69+
$this->assertSame([
70+
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
71+
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
72+
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
73+
], array_column($query, 'query'));
74+
}
75+
76+
public function test_it_can_purge_tokens_by_hours()
77+
{
78+
$this->travelTo(Carbon::create(2000, 1, 1, 2));
79+
80+
$query = DB::pretend(function () {
81+
$this->artisan('passport:purge', ['--hours' => 2])
82+
->expectsOutputToContain('Purged revoked items and items expired for more than 2 hours.');
83+
});
84+
85+
$this->assertSame([
86+
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
87+
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
88+
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
89+
], array_column($query, 'query'));
90+
}
91+
}

0 commit comments

Comments
 (0)