Skip to content

[11.x] Add Provider Guard to ClientRepository for Personal Access Clients #1655

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

Merged
merged 3 commits into from
Apr 24, 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
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