Skip to content

Commit 11d8533

Browse files
committed
Merge branch 'billriess/8.x'
2 parents 4f52c4c + cdc37f0 commit 11d8533

14 files changed

+201
-37
lines changed

database/migrations/2016_06_01_000004_create_oauth_clients_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->unsignedBigInteger('user_id')->nullable()->index();
1919
$table->string('name');
2020
$table->string('secret', 100)->nullable();
21+
$table->string('provider')->nullable();
2122
$table->text('redirect');
2223
$table->boolean('personal_access_client');
2324
$table->boolean('password_client');

src/Bridge/Client.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@ class Client implements ClientEntityInterface
1616
*/
1717
protected $identifier;
1818

19+
/**
20+
* The client's provider.
21+
*
22+
* @var string
23+
*/
24+
public $provider;
25+
1926
/**
2027
* Create a new client instance.
2128
*
2229
* @param string $identifier
2330
* @param string $name
2431
* @param string $redirectUri
2532
* @param bool $isConfidential
33+
* @param string|null $provider
2634
* @return void
2735
*/
28-
public function __construct($identifier, $name, $redirectUri, $isConfidential = false)
36+
public function __construct($identifier, $name, $redirectUri, $isConfidential = false, $provider = null)
2937
{
3038
$this->setIdentifier((string) $identifier);
3139

3240
$this->name = $name;
3341
$this->isConfidential = $isConfidential;
3442
$this->redirectUri = explode(',', $redirectUri);
43+
$this->provider = $provider;
3544
}
3645

3746
/**

src/Bridge/ClientRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public function getClientEntity($clientIdentifier)
3838
}
3939

4040
return new Client(
41-
$clientIdentifier, $record->name, $record->redirect, $record->confidential()
41+
$clientIdentifier,
42+
$record->name,
43+
$record->redirect,
44+
$record->confidential(),
45+
$record->provider
4246
);
4347
}
4448

src/Bridge/UserRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(Hasher $hasher)
3232
*/
3333
public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
3434
{
35-
$provider = config('auth.guards.api.provider');
35+
$provider = $clientEntity->provider ?: config('auth.guards.api.provider');
3636

3737
if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
3838
throw new RuntimeException('Unable to determine authentication model from configuration.');

src/Client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ class Client extends Model
5555
*/
5656
public function user()
5757
{
58+
$provider = $this->provider ?: config('auth.guards.api.provider');
59+
5860
return $this->belongsTo(
59-
config('auth.providers.'.config('auth.guards.api.provider').'.model')
61+
config("auth.providers.{$provider}.model")
6062
);
6163
}
6264

src/ClientRepository.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,19 @@ public function personalAccessClient()
104104
* @param int $userId
105105
* @param string $name
106106
* @param string $redirect
107+
* @param string|null $provider
107108
* @param bool $personalAccess
108109
* @param bool $password
109110
* @param bool $confidential
110111
* @return \Laravel\Passport\Client
111112
*/
112-
public function create($userId, $name, $redirect, $personalAccess = false, $password = false, $confidential = true)
113+
public function create($userId, $name, $redirect, $provider = null, $personalAccess = false, $password = false, $confidential = true)
113114
{
114115
$client = Passport::client()->forceFill([
115116
'user_id' => $userId,
116117
'name' => $name,
117118
'secret' => ($confidential || $personalAccess) ? Str::random(40) : null,
119+
'provider' => $provider,
118120
'redirect' => $redirect,
119121
'personal_access_client' => $personalAccess,
120122
'password_client' => $password,
@@ -136,7 +138,7 @@ public function create($userId, $name, $redirect, $personalAccess = false, $pass
136138
*/
137139
public function createPersonalAccessClient($userId, $name, $redirect)
138140
{
139-
return tap($this->create($userId, $name, $redirect, true), function ($client) {
141+
return tap($this->create($userId, $name, $redirect, null, true), function ($client) {
140142
$accessClient = Passport::personalAccessClient();
141143
$accessClient->client_id = $client->id;
142144
$accessClient->save();
@@ -149,11 +151,12 @@ public function createPersonalAccessClient($userId, $name, $redirect)
149151
* @param int $userId
150152
* @param string $name
151153
* @param string $redirect
154+
* @param string|null $provider
152155
* @return \Laravel\Passport\Client
153156
*/
154-
public function createPasswordGrantClient($userId, $name, $redirect)
157+
public function createPasswordGrantClient($userId, $name, $redirect, $provider = null)
155158
{
156-
return $this->create($userId, $name, $redirect, false, true);
159+
return $this->create($userId, $name, $redirect, $provider, false, true);
157160
}
158161

159162
/**

src/Console/ClientCommand.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ClientCommand extends Command
1818
{--password : Create a password grant client}
1919
{--client : Create a client credentials grant client}
2020
{--name= : The name of the client}
21+
{--provider= : The name of the user provider}
2122
{--redirect_uri= : The URI to redirect to after authorization }
2223
{--user_id= : The user ID the client should be assigned to }
2324
{--public : Create a public client (Auth code grant type only) }';
@@ -83,8 +84,16 @@ protected function createPasswordClient(ClientRepository $clients)
8384
config('app.name').' Password Grant Client'
8485
);
8586

87+
$providers = array_keys(config('auth.providers'));
88+
89+
$provider = $this->option('provider') ?: $this->choice(
90+
'Which user provider should this client use to retrieve users?',
91+
$providers,
92+
in_array('users', $providers) ? 'users' : null
93+
);
94+
8695
$client = $clients->createPasswordGrantClient(
87-
null, $name, 'http://localhost'
96+
null, $name, 'http://localhost', $provider
8897
);
8998

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

138147
$client = $clients->create(
139-
$userId, $name, $redirect, false, false, ! $this->option('public')
148+
$userId, $name, $redirect, null, false, false, ! $this->option('public')
140149
);
141150

142151
$this->info('New client created successfully.');

src/Console/InstallCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ class InstallCommand extends Command
2929
*/
3030
public function handle()
3131
{
32+
$provider = in_array('users', array_keys(config('auth.providers'))) ? 'users' : null;
33+
3234
$this->call('passport:keys', ['--force' => $this->option('force'), '--length' => $this->option('length')]);
3335
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
34-
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client']);
36+
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => $provider]);
3537
}
3638
}

src/Guards/TokenGuard.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Exception;
66
use Firebase\JWT\JWT;
77
use Illuminate\Container\Container;
8-
use Illuminate\Contracts\Auth\UserProvider;
98
use Illuminate\Contracts\Debug\ExceptionHandler;
109
use Illuminate\Contracts\Encryption\Encrypter;
1110
use Illuminate\Cookie\Middleware\EncryptCookies;
@@ -16,6 +15,7 @@
1615
use Laminas\Diactoros\UploadedFileFactory;
1716
use Laravel\Passport\ClientRepository;
1817
use Laravel\Passport\Passport;
18+
use Laravel\Passport\PassportUserProvider;
1919
use Laravel\Passport\TokenRepository;
2020
use Laravel\Passport\TransientToken;
2121
use League\OAuth2\Server\Exception\OAuthServerException;
@@ -34,7 +34,7 @@ class TokenGuard
3434
/**
3535
* The user provider implementation.
3636
*
37-
* @var \Illuminate\Contracts\Auth\UserProvider
37+
* @var \Laravel\Passport\PassportUserProvider
3838
*/
3939
protected $provider;
4040

@@ -63,25 +63,43 @@ class TokenGuard
6363
* Create a new token guard instance.
6464
*
6565
* @param \League\OAuth2\Server\ResourceServer $server
66-
* @param \Illuminate\Contracts\Auth\UserProvider $provider
66+
* @param \Laravel\Passport\PassportUserProvider $provider
6767
* @param \Laravel\Passport\TokenRepository $tokens
6868
* @param \Laravel\Passport\ClientRepository $clients
6969
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
7070
* @return void
7171
*/
72-
public function __construct(ResourceServer $server,
73-
UserProvider $provider,
74-
TokenRepository $tokens,
75-
ClientRepository $clients,
76-
Encrypter $encrypter)
77-
{
72+
public function __construct(
73+
ResourceServer $server,
74+
PassportUserProvider $provider,
75+
TokenRepository $tokens,
76+
ClientRepository $clients,
77+
Encrypter $encrypter
78+
) {
7879
$this->server = $server;
7980
$this->tokens = $tokens;
8081
$this->clients = $clients;
8182
$this->provider = $provider;
8283
$this->encrypter = $encrypter;
8384
}
8485

86+
/**
87+
* Determine if the requested provider matches the client's provider.
88+
*
89+
* @param \Illuminate\Http\Request $request
90+
* @return bool
91+
*/
92+
protected function hasValidProvider(Request $request)
93+
{
94+
$client = $this->client($request);
95+
96+
if ($client && ! $client->provider) {
97+
return true;
98+
}
99+
100+
return $client && $client->provider === $this->provider->getProviderName();
101+
}
102+
85103
/**
86104
* Get the user for the incoming request.
87105
*
@@ -90,6 +108,10 @@ public function __construct(ResourceServer $server,
90108
*/
91109
public function user(Request $request)
92110
{
111+
if (! $this->hasValidProvider($request)) {
112+
return;
113+
}
114+
93115
if ($request->bearerToken()) {
94116
return $this->authenticateViaBearerToken($request);
95117
} elseif ($request->cookie(Passport::cookie())) {

src/Http/Middleware/CheckCredentials.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function validate($psr, $scopes)
101101
abstract protected function validateCredentials($token);
102102

103103
/**
104-
* Validate token credentials.
104+
* Validate token scopes.
105105
*
106106
* @param \Laravel\Passport\Token $token
107107
* @param array $scopes

0 commit comments

Comments
 (0)