Skip to content

Commit

Permalink
[9.x] Implement passport:hash command (#1238)
Browse files Browse the repository at this point in the history
* Implement passport:hash command

* Skip records which are already hashed

* Fix client command when hashing secrets

* Use password_get_info

* Update HashCommand.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
driesvints and taylorotwell authored May 5, 2020
1 parent 69ccc34 commit 7a39717
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Console/ClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ protected function createAuthCodeClient(ClientRepository $clients)
protected function outputClientDetails(Client $client)
{
$this->line('<comment>Client ID:</comment> '.$client->id);
$this->line('<comment>Client secret:</comment> '.$client->secret);
$this->line('<comment>Client secret:</comment> '.$client->plainSecret);
}
}
55 changes: 55 additions & 0 deletions src/Console/HashCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Laravel\Passport\Console;

use Illuminate\Console\Command;
use Laravel\Passport\Passport;

class HashCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'passport:hash';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Hash all of the existing secrets in the clients table';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
if (! Passport::$hashesClientSecrets) {
$this->warn("Please enable client hashing yet in your AppServiceProvider before continuning.");

return;
}

if ($this->confirm('Are you sure you want to hash all client secrets? This cannot be undone.')) {
$model = Passport::clientModel();

foreach ((new $model)->whereNotNull('secret')->cursor() as $client) {
if (password_get_info($client->secret)['algo'] === PASSWORD_BCRYPT) {
continue;
}

$client->timestamps = false;

$client->forceFill([
'secret' => password_hash($client->secret, PASSWORD_BCRYPT),
])->save();
}

$this->info('All client secrets were successfully hashed.');
}
}
}
1 change: 1 addition & 0 deletions src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function boot()
$this->commands([
Console\InstallCommand::class,
Console\ClientCommand::class,
Console\HashCommand::class,
Console\KeysCommand::class,
Console\PurgeCommand::class,
]);
Expand Down

0 comments on commit 7a39717

Please sign in to comment.