Skip to content

Commit

Permalink
[11.x] Add Provider Guard to ClientRepository for Personal Access Cli…
Browse files Browse the repository at this point in the history
…ents (#1655)

* Added provider guard to Password Grant Client.

* Add argument provider in `InstallCommand`

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
michaelnabil230 and taylorotwell authored Apr 24, 2023
1 parent bf5e6f4 commit a75f0a9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ public function create($userId, $name, $redirect, $provider = null, $personalAcc
* @param int|null $userId
* @param string $name
* @param string $redirect
* @param string|null $provider
* @return \Laravel\Passport\Client
*/
public function createPersonalAccessClient($userId, $name, $redirect)
public function createPersonalAccessClient($userId, $name, $redirect, $provider = null)
{
return tap($this->create($userId, $name, $redirect, null, true), function ($client) {
return tap($this->create($userId, $name, $redirect, $provider, true), function ($client) {
$accessClient = Passport::personalAccessClient();
$accessClient->client_id = $client->getKey();
$accessClient->save();
Expand Down
28 changes: 20 additions & 8 deletions src/Console/ClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ protected function createPersonalClient(ClientRepository $clients)
config('app.name').' Personal Access Client'
);

$provider = $this->promptForProvider();

$client = $clients->createPersonalAccessClient(
null, $name, 'http://localhost'
null, $name, 'http://localhost', $provider
);

$this->info('Personal access client created successfully.');
Expand All @@ -85,13 +87,7 @@ protected function createPasswordClient(ClientRepository $clients)
config('app.name').' Password Grant Client'
);

$providers = array_keys(config('auth.providers'));

$provider = $this->option('provider') ?: $this->choice(
'Which user provider should this client use to retrieve users?',
$providers,
in_array('users', $providers) ? 'users' : null
);
$provider = $this->promptForProvider();

$client = $clients->createPasswordGrantClient(
null, $name, 'http://localhost', $provider
Expand Down Expand Up @@ -154,6 +150,22 @@ protected function createAuthCodeClient(ClientRepository $clients)
$this->outputClientDetails($client);
}

/**
* Ask the user what user provider should be used.
*
* @return string
*/
protected function promptForProvider()
{
$providers = array_keys(config('auth.providers'));

return $this->option('provider') ?: $this->choice(
'Which user provider should this client use to retrieve users?',
$providers,
in_array('users', $providers) ? 'users' : null
);
}

/**
* Output the client's ID and secret key.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle()
$this->configureUuids();
}

$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client', '--provider' => $provider]);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => $provider]);
}

Expand Down

0 comments on commit a75f0a9

Please sign in to comment.