Skip to content

Commit 9fe71f7

Browse files
author
Luca Degasperi
committed
Working on deleting the expired sessions
1 parent be831e1 commit 9fe71f7

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/LucaDegasperi/OAuth2Server/Commands/ExpiredTokensCommand.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use Illuminate\Console\Command;
44
use Symfony\Component\Console\Input\InputOption;
55
use Symfony\Component\Console\Input\InputArgument;
6+
use LucaDegasperi\Repositories\SessionManagementInterface;
67

78
class ExpiredTokensCommand extends Command
89
{
9-
1010
/**
1111
* The console command name.
1212
*
@@ -21,14 +21,19 @@ class ExpiredTokensCommand extends Command
2121
*/
2222
protected $description = 'A command to delete the OAuth expired tokens';
2323

24+
25+
protected $sessions;
26+
2427
/**
2528
* Create a new command instance.
2629
*
2730
* @return void
2831
*/
29-
public function __construct()
32+
public function __construct(SessionManagementInterface $sessions)
3033
{
3134
parent::__construct();
35+
36+
$this->sessions = $sessions;
3237
}
3338

3439
/**
@@ -38,8 +43,14 @@ public function __construct()
3843
*/
3944
public function fire()
4045
{
41-
//
42-
var_dump('fails');
46+
$value = $this->option('delete');
47+
if ($value === true) {
48+
$result = $this->deleteExpiredTokens();
49+
$this->info('Expired OAuth tokens were deleted');
50+
}
51+
else {
52+
$this->info('use the --delete option to trigger the delete of the expired tokens');
53+
}
4354
}
4455

4556
/**
@@ -64,4 +75,8 @@ protected function getOptions()
6475
);
6576
}
6677

67-
}
78+
protected function deleteExpiredTokens()
79+
{
80+
return $this->sessions->deleteExpired();
81+
}
82+
}

src/LucaDegasperi/OAuth2Server/Repositories/FluentSession.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use DB;
55
use Carbon\Carbon;
66

7-
class FluentSession implements SessionInterface
7+
class FluentSession implements SessionInterface, SessionManagementInterface
88
{
99

1010
public function createSession($clientId, $ownerType, $ownerId)
@@ -175,4 +175,16 @@ public function removeRefreshToken($refreshToken)
175175
->where('refresh_token', '=', $refreshToken)
176176
->delete();
177177
}
178+
179+
public function deleteExpired()
180+
{
181+
$time = time();
182+
$expiredSessions = DB::table('oauth_sessions')
183+
->join('oauth_session_access_tokens', 'oauth_session_access_tokens.session_id', '=', 'oauth_sessions.id')
184+
->join('oauth_session_refresh_tokens', 'oauth_session_refresh_tokens.session_access_token_id', '=', 'oauth_session_access_tokens.id')
185+
->where('oauth_session_refresh_tokens.refresh_token_expires', '<', $time)
186+
->where('oauth_session_access_tokens.access_token_expires', '<', $time)
187+
->get();
188+
var_dump($expiredSessions);
189+
}
178190
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace LucaDegasperi\OAuth2Server\Repositories;
2+
3+
interface SessionManagementInterface
4+
{
5+
public function deleteExpired();
6+
}

0 commit comments

Comments
 (0)