Skip to content

Commit e513f84

Browse files
committed
add purge command
1 parent 27893bb commit e513f84

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"sort-packages": true
2525
},
2626
"extra": {
27+
"branch-alias": {
28+
"dev-master": "1.0-dev"
29+
},
2730
"hyperf": {
2831
"config": "OAuthServer\\ConfigProvider"
2932
}

src/Command/PurgeTokenCommand.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace OAuthServer\Command;
4+
5+
use Carbon\Carbon;
6+
use Hyperf\Command\Command;
7+
use Hyperf\DbConnection\Db;
8+
use Hyperf\Contract\ConfigInterface;
9+
use Psr\Container\ContainerInterface;
10+
11+
12+
class PurgeTokenCommand extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'oauth:purge {--force : purge all expires token}';
20+
21+
/**
22+
* @var ContainerInterface
23+
*/
24+
private $container;
25+
26+
/**
27+
* @var ConfigInterface
28+
*/
29+
private $config;
30+
31+
public function __construct(ContainerInterface $container, ConfigInterface $config)
32+
{
33+
parent::__construct();
34+
$this->container = $container;
35+
$this->config = $config;
36+
}
37+
38+
/**
39+
* Undocumented function
40+
*
41+
* @return void
42+
*/
43+
public function handle()
44+
{
45+
$force = $this->input->getOption('force');
46+
47+
$this->clear();
48+
49+
$this->info('All token revoked');
50+
}
51+
52+
/**
53+
* Undocumented function
54+
*
55+
* @return void
56+
*/
57+
protected function configure()
58+
{
59+
$this->setDescription('Purge all token expires & revoked');
60+
}
61+
62+
/**
63+
* Undocumented function
64+
*
65+
* @return void
66+
*/
67+
private function clear()
68+
{
69+
$now = Carbon::now()->format('Y-m-d H:i:s');
70+
71+
$token = Db::connection(config('oauth.provider', 'default'))
72+
->table('oauth_access_tokens')
73+
->where('expires_at', '<=', $now)
74+
->orWhere('revoked', 1)
75+
->delete();
76+
77+
$refresh = Db::connection(config('oauth.provider', 'default'))
78+
->table('oauth_refresh_tokens')
79+
->where('expires_at', '<=', $now)
80+
->orWhere('revoked', 1)
81+
->delete();
82+
}
83+
}

src/ConfigProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use OAuthServer\AuthorizationServerFactory;
88
use League\OAuth2\Server\AuthorizationServer;
99
use OAuthServer\Repositories\TokenRepository;
10+
use OAuthServer\Command\PurgeTokenCommand;
1011

1112
class ConfigProvider
1213
{
@@ -30,7 +31,8 @@ public function __invoke(): array
3031
//
3132
],
3233
'commands' => [
33-
OAuthKeyCommand::class
34+
OAuthKeyCommand::class,
35+
PurgeTokenCommand::class
3436
],
3537
'annotations' => [
3638
'scan' => [

0 commit comments

Comments
 (0)