From f426cbaec13e36d67a0dea080ef04861a8fab202 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Sun, 19 Jul 2020 01:56:54 +0200 Subject: [PATCH] Add client command --- src/Console/ClientCommand.php | 118 +++++++++++++++++++++++++ src/MongodbPassportServiceProvider.php | 8 +- 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/Console/ClientCommand.php diff --git a/src/Console/ClientCommand.php b/src/Console/ClientCommand.php new file mode 100644 index 0000000..697f838 --- /dev/null +++ b/src/Console/ClientCommand.php @@ -0,0 +1,118 @@ +option('name') ?: $this->ask( + 'What should we name the personal access client?', + config('app.name').' Personal Access Client' + ); + + $client = $clients->createPersonalAccessClient( + null, $name, 'http://localhost' + ); + + $this->info('Personal access client created successfully.'); + + $this->patchedOutputClientDetails($client); + } + + /** + * Create a new password grant client. + * + * @param \Laravel\Passport\ClientRepository $clients + * @return void + */ + protected function createPasswordClient(ClientRepository $clients) + { + $name = $this->option('name') ?: $this->ask( + 'What should we name the password grant client?', + config('app.name').' Password Grant Client' + ); + + $client = $clients->createPasswordGrantClient( + null, $name, 'http://localhost' + ); + + $this->info('Password grant client created successfully.'); + + $this->patchedOutputClientDetails($client); + } + + /** + * Create a client credentials grant client. + * + * @param \Laravel\Passport\ClientRepository $clients + * @return void + */ + protected function createClientCredentialsClient(ClientRepository $clients) + { + $name = $this->option('name') ?: $this->ask( + 'What should we name the client?', + config('app.name').' ClientCredentials Grant Client' + ); + + $client = $clients->create( + null, $name, '' + ); + + $this->info('New client created successfully.'); + + $this->patchedOutputClientDetails($client); + } + + /** + * Create a authorization code client. + * + * @param \Laravel\Passport\ClientRepository $clients + * @return void + */ + protected function createAuthCodeClient(ClientRepository $clients) + { + $userId = $this->option('user_id') ?: $this->ask( + 'Which user ID should the client be assigned to?' + ); + + $name = $this->option('name') ?: $this->ask( + 'What should we name the client?' + ); + + $redirect = $this->option('redirect_uri') ?: $this->ask( + 'Where should we redirect the request after authorization?', + url('/auth/callback') + ); + + $client = $clients->create( + $userId, $name, $redirect, false, false, ! $this->option('public') + ); + + $this->info('New client created successfully.'); + + $this->patchedOutputClientDetails($client); + } + + /** + * Output the client's ID and secret key. + * + * @param \Laravel\Passport\Client $client + * @return void + */ + protected function patchedOutputClientDetails(Client $client) + { + $this->line('Client ID: '.$client->id); + $this->line('Client secret: '.$client->secret); + } +} diff --git a/src/MongodbPassportServiceProvider.php b/src/MongodbPassportServiceProvider.php index 37723a3..e88122f 100644 --- a/src/MongodbPassportServiceProvider.php +++ b/src/MongodbPassportServiceProvider.php @@ -2,14 +2,16 @@ namespace DesignMyNight\Mongodb; +use DesignMyNight\Mongodb\Console\ClientCommand; +use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; use DesignMyNight\Mongodb\Passport\AuthCode; use DesignMyNight\Mongodb\Passport\Bridge\RefreshTokenRepository; use DesignMyNight\Mongodb\Passport\Client; use DesignMyNight\Mongodb\Passport\PersonalAccessClient; -use DesignMyNight\Mongodb\Passport\RefreshToken; use DesignMyNight\Mongodb\Passport\Token; use Laravel\Passport\Bridge\RefreshTokenRepository as PassportRefreshTokenRepository; +use Laravel\Passport\Console\ClientCommand as OriginalClientCommand; use Laravel\Passport\Passport; class MongodbPassportServiceProvider extends ServiceProvider @@ -27,5 +29,9 @@ public function register() $this->app->bind(PassportRefreshTokenRepository::class, function () { return $this->app->make(RefreshTokenRepository::class); }); + + $this->app->extend(OriginalClientCommand::class, function (Application $app) { + return new ClientCommand(); + }); } }