Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona o comando PurgeCommand e resolve modelo RefreshToken #6

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Sysvale\Mongodb\Console;

use Illuminate\Support\Carbon;
use Laravel\Passport\Console\PurgeCommand as PassportPurgeCommand;
use Laravel\Passport\Passport;

class PurgeCommand extends PassportPurgeCommand
{
public function handle()
{
$expired = Carbon::now()->subDays(7);

if (($this->option('revoked') && $this->option('expired')) ||
(!$this->option('revoked') && !$this->option('expired'))
) {
Passport::token()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();
Passport::authCode()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();
Passport::refreshToken()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();

$this->info('Purged revoked items and items expired for more than seven days.');
} elseif ($this->option('revoked')) {
Passport::token()->where('revoked', true)->delete();
Passport::authCode()->where('revoked', true)->delete();
Passport::refreshToken()->where('revoked', true)->delete();

$this->info('Purged revoked items.');
} elseif ($this->option('expired')) {
Passport::token()->where('expires_at', '<', $expired)->delete();
Passport::authCode()->where('expires_at', '<', $expired)->delete();
Passport::refreshToken()->where('expires_at', '<', $expired)->delete();

$this->info('Purged items expired for more than seven days.');
}
}
}
8 changes: 8 additions & 0 deletions src/MongodbPassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Sysvale\Mongodb\Passport\Token;
use Laravel\Passport\Bridge\RefreshTokenRepository as PassportRefreshTokenRepository;
use Laravel\Passport\Console\ClientCommand as PassportClientCommand;
use Laravel\Passport\Console\PurgeCommand as PassportPurgeCommand;
use Laravel\Passport\Passport;
use Laravel\Passport\TokenRepository as PassportTokenRepository;
use Sysvale\Mongodb\Console\PurgeCommand;
use Sysvale\Mongodb\Passport\RefreshToken;
use Sysvale\Mongodb\Passport\TokenRepository;

class MongodbPassportServiceProvider extends ServiceProvider
Expand All @@ -26,6 +29,7 @@ public function register()
Passport::useClientModel(Client::class);
Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
Passport::useTokenModel(Token::class);
Passport::useRefreshTokenModel(RefreshToken::class);

$this->app->bind(PassportRefreshTokenRepository::class, function () {
return $this->app->make(RefreshTokenRepository::class);
Expand All @@ -35,6 +39,10 @@ public function register()
return new ClientCommand();
});

$this->app->extend(PassportPurgeCommand::class, function () {
return new PurgeCommand();
});

$this->app->bind(PassportTokenRepository::class, function () {
return $this->app->make(TokenRepository::class);
});
Expand Down
3 changes: 2 additions & 1 deletion src/Passport/Bridge/RefreshTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\Passport\Bridge\RefreshTokenRepository as BaseRefreshTokenRepository;
use Laravel\Passport\Events\RefreshTokenCreated;
use Laravel\Passport\Passport;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;

/**
Expand All @@ -17,7 +18,7 @@ class RefreshTokenRepository extends BaseRefreshTokenRepository
*/
public function getNewRefreshToken()
{
return new RefreshToken();
return Passport::refreshToken();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Passport/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Sysvale\Mongodb\Passport;

use Jenssegers\Mongodb\Eloquent\Model;
use Sysvale\Mongodb\Passport\Bridge\RefreshToken as BridgeRefreshToken;

class RefreshToken extends Model
class RefreshToken extends BridgeRefreshToken
{
/**
* The database table used by the model.
Expand Down