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

[9.x] Initial support for multiple providers #1220

Merged
merged 16 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
coverage: none

- name: Install dependencies
run: composer require "illuminate/contracts=${{ matrix.laravel }}" --prefer-dist --no-interaction --no-suggest
run: composer require "illuminate/contracts=${{ matrix.laravel }}" --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit --verbose
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('name');
$table->string('secret', 100)->nullable();
$table->string('provider')->nullable();
billriess marked this conversation as resolved.
Show resolved Hide resolved
$table->text('redirect');
$table->boolean('personal_access_client');
$table->boolean('password_client');
Expand Down
5 changes: 4 additions & 1 deletion src/Bridge/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Client implements ClientEntityInterface
*/
protected $identifier;

public $provider;
billriess marked this conversation as resolved.
Show resolved Hide resolved

/**
* Create a new client instance.
*
Expand All @@ -25,13 +27,14 @@ class Client implements ClientEntityInterface
* @param bool $isConfidential
* @return void
*/
public function __construct($identifier, $name, $redirectUri, $isConfidential = false)
public function __construct($identifier, $name, $redirectUri, $isConfidential = false, $provider = null)
billriess marked this conversation as resolved.
Show resolved Hide resolved
{
$this->setIdentifier((string) $identifier);

$this->name = $name;
$this->isConfidential = $isConfidential;
$this->redirectUri = explode(',', $redirectUri);
$this->provider = $provider;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getClientEntity($clientIdentifier)
}

return new Client(
$clientIdentifier, $record->name, $record->redirect, $record->confidential()
$clientIdentifier, $record->name, $record->redirect, $record->confidential(), $record->provider
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(HashManager $hasher)
*/
public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
{
$provider = config('auth.guards.api.provider');
$provider = $clientEntity->provider ?: config('auth.guards.api.provider');

if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
throw new RuntimeException('Unable to determine authentication model from configuration.');
Expand Down
13 changes: 12 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Passport;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Client extends Model
{
Expand Down Expand Up @@ -49,7 +50,7 @@ class Client extends Model
public function user()
{
return $this->belongsTo(
config('auth.providers.'.config('auth.guards.api.provider').'.model')
config('auth.providers.'.$this->provider ?: config('auth.guards.api.provider').'.model')
);
billriess marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -102,4 +103,14 @@ public function confidential()
{
return ! empty($this->secret);
}

/**
* Get the client's provider.
*
* @return mixed
billriess marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getProvider()
{
return $this->provider ? Auth::createUserProvider($this->provider) : null;
}
}
9 changes: 5 additions & 4 deletions src/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ public function personalAccessClient()
* @param bool $confidential
* @return \Laravel\Passport\Client
*/
public function create($userId, $name, $redirect, $personalAccess = false, $password = false, $confidential = true)
public function create($userId, $name, $redirect, $provider = null, $personalAccess = false, $password = false, $confidential = true)
billriess marked this conversation as resolved.
Show resolved Hide resolved
{
$client = Passport::client()->forceFill([
'user_id' => $userId,
'name' => $name,
'secret' => ($confidential || $personalAccess) ? Str::random(40) : null,
'redirect' => $redirect,
'provider' => $provider,
'personal_access_client' => $personalAccess,
'password_client' => $password,
'revoked' => false,
Expand All @@ -136,7 +137,7 @@ public function create($userId, $name, $redirect, $personalAccess = false, $pass
*/
public function createPersonalAccessClient($userId, $name, $redirect)
{
return tap($this->create($userId, $name, $redirect, true), function ($client) {
return tap($this->create($userId, $name, $redirect, null, true), function ($client) {
$accessClient = Passport::personalAccessClient();
$accessClient->client_id = $client->id;
$accessClient->save();
Expand All @@ -151,9 +152,9 @@ public function createPersonalAccessClient($userId, $name, $redirect)
* @param string $redirect
* @return \Laravel\Passport\Client
*/
public function createPasswordGrantClient($userId, $name, $redirect)
public function createPasswordGrantClient($userId, $name, $redirect, $provider = null)
billriess marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->create($userId, $name, $redirect, false, true);
return $this->create($userId, $name, $redirect, $provider, false, true);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Console/ClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ClientCommand extends Command
{--password : Create a password grant client}
{--client : Create a client credentials grant client}
{--name= : The name of the client}
{--provider= : The name of the provider}
{--redirect_uri= : The URI to redirect to after authorization }
{--user_id= : The user ID the client should be assigned to }
{--public : Create a public client (Auth code grant type only) }';
Expand Down Expand Up @@ -83,8 +84,16 @@ protected function createPasswordClient(ClientRepository $clients)
config('app.name').' Password Grant Client'
);

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

$provider = $this->option('provider') ?: $this->choice(
'What provider should be used?',
$providers,
$providers[0]
);

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

$this->info('Password grant client created successfully.');
Expand Down Expand Up @@ -136,7 +145,7 @@ protected function createAuthCodeClient(ClientRepository $clients)
);

$client = $clients->create(
$userId, $name, $redirect, false, false, ! $this->option('public')
$userId, $name, $redirect, null, false, false, ! $this->option('public')
);

$this->info('New client created successfully.');
Expand Down
2 changes: 1 addition & 1 deletion src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function handle()
{
$this->call('passport:keys', ['--force' => $this->option('force'), '--length' => $this->option('length')]);
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => array_keys(config('auth.providers'))[0]]);
billriess marked this conversation as resolved.
Show resolved Hide resolved
}
}
15 changes: 15 additions & 0 deletions src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public function __construct(ResourceServer $server,
$this->encrypter = $encrypter;
}

/**
* Determine if the requested provider matches the client's provider.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function validateProvider(Request $request)
billriess marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->provider == $this->client($request)->getProvider();
driesvints marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get the user for the incoming request.
*
Expand All @@ -90,6 +101,10 @@ public function __construct(ResourceServer $server,
*/
public function user(Request $request)
{
if (!$this->validateProvider($request)) {
billriess marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if ($request->bearerToken()) {
return $this->authenticateViaBearerToken($request);
} elseif ($request->cookie(Passport::cookie())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/CheckCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function validate($psr, $scopes)
abstract protected function validateCredentials($token);

/**
* Validate token credentials.
* Validate token scopes.
*
* @param \Laravel\Passport\Token $token
* @param array $scopes
Expand Down