-
Notifications
You must be signed in to change notification settings - Fork 141
/
Purge.php
48 lines (40 loc) · 1.4 KB
/
Purge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Dusterio\LumenPassport\Console\Commands;
use Illuminate\Console\Command;
use Laravel\Passport\ClientRepository;
use Illuminate\Support\Facades\DB;
use DateTime;
use Laravel\Passport\Passport;
class Purge extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'passport:purge';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete expired refresh tokens and their associated tokens from the database';
/**
* Execute the console command.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
public function handle(ClientRepository $clients)
{
$count = DB::table('oauth_refresh_tokens')->where('expires_at', '<', new DateTime())->delete();
if (Passport::$refreshTokensExpireAt && Passport::$tokensExpireAt) {
$difference = Passport::$refreshTokensExpireAt->getTimestamp() - Passport::$tokensExpireAt->getTimestamp();
// We assume it's safe to delete tokens that cannot be refreshed anyway
$count += DB::table('oauth_access_tokens')
->where('expires_at', '<', (new DateTime())->setTimestamp(time() - $difference))
->delete();
}
$this->info('Successfully deleted expired tokens: ' . $count);
}
}