Skip to content

Commit

Permalink
Fixed problem on HashManger and added passport:purge command (thanks …
Browse files Browse the repository at this point in the history
…dusterio)
  • Loading branch information
tonyputi committed Apr 4, 2019
1 parent 39f00b5 commit 0360529
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
31 changes: 19 additions & 12 deletions README.md
Expand Up @@ -97,19 +97,21 @@ This method will register the routes necessary to issue access tokens and revoke

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Carbon;

use Nomadnt\LumenPassport\Passport;

class AuthServiceProvider extends ServiceProvider{

class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register(){
public function register()
{

}

Expand All @@ -118,7 +120,8 @@ class AuthServiceProvider extends ServiceProvider{
*
* @return void
*/
public function boot(){
public function boot()
{
// register passport routes
Passport::routes();

Expand Down Expand Up @@ -191,14 +194,15 @@ namespace App\Listeners;
use Laravel\Passport\Events\AccessTokenCreated;
use Laravel\Passport\Token;

class RevokeOtherTokens{

class RevokeOtherTokens
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct(){
public function __construct()
{
//
}

Expand All @@ -208,7 +212,8 @@ class RevokeOtherTokens{
* @param \App\Events\OrderShipped $event
* @return void
*/
public function handle(AccessTokenCreated $event){
public function handle(AccessTokenCreated $event)
{
Token::where(function($query) use($event){
$query->where('user_id', $event->userId);
$query->where('id', '<>', $event->tokenId);
Expand All @@ -225,14 +230,15 @@ namespace App\Listeners;
use Laravel\Passport\Events\AccessTokenCreated;
use Laravel\Passport\Token;

class PruneRevokedTokens{

class PruneRevokedTokens
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct(){
public function __construct()
{
//
}

Expand All @@ -242,7 +248,8 @@ class PruneRevokedTokens{
* @param \App\Events\AccessTokenCreated $event
* @return void
*/
public function handle(AccessTokenCreated $event){
public function handle(AccessTokenCreated $event)
{
Token::where(function($query) use($event){
$query->where('user_id', $event->userId);
$query->where('id', '<>', $event->tokenId);
Expand Down
57 changes: 57 additions & 0 deletions src/Console/Commands/PurgeCommand.php
@@ -0,0 +1,57 @@
<?php

namespace Nomadnt\LumenPassport\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;

use Nomadnt\LumenPassport\Passport;

class PurgeCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'passport:purge';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete expired refresh tokens and their associated tokens from the database';

/**
* Create a new passport purge command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$date = Carbon::now();

$count = DB::table('oauth_refresh_tokens')->where('expires_at', '<', $date)->delete();

if(Passport::$refreshTokensExpireAt AND Passport::$tokensExpireAt){
$date->subDays(Passport::$refreshTokensExpireAt->diffinDays(Passport::$tokensExpireAt));
// We assume it's safe to delete tokens that cannot be refreshed anyway
$count += DB::table('oauth_access_tokens')->where('expires_at', '<', $date)->delete();
}

$this->info("Successfully deleted expired tokens: {$count}");
}
}
16 changes: 12 additions & 4 deletions src/PassportServiceProvider.php
Expand Up @@ -15,10 +15,18 @@ class PassportServiceProvider extends ServiceProvider
*/
public function boot()
{
parent::boot();

$this->app->singleton(Connection::class, function() {
$this->app->singleton(Connection::class, function(){
return $this->app['db.connection'];
});
});

if(preg_match('/5\.[678]\.\d+/', $this->app->version())){
$this->app->singleton(\Illuminate\Hashing\HashManager::class, function ($app) {
return new \Illuminate\Hashing\HashManager($app);
});
}

if($this->app->runningInConsole()){
$this->commands([Console\Commands\PurgeCommand::class]);
}
}
}

0 comments on commit 0360529

Please sign in to comment.