Skip to content

Commit 187580a

Browse files
authored
[11.x] Allow authenticated client to be retrieved from the guard (#1508)
* Allow authenticated client to be retrieved from the guard * Make token guard macroable * Use previously resolved client when available * Add tests ensuring client and user are only pulled from the database once
1 parent fa30cd6 commit 187580a

File tree

3 files changed

+190
-72
lines changed

3 files changed

+190
-72
lines changed

src/Guards/TokenGuard.php

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Exception;
66
use Firebase\JWT\JWT;
7+
use Illuminate\Auth\GuardHelpers;
78
use Illuminate\Container\Container;
9+
use Illuminate\Contracts\Auth\Guard;
810
use Illuminate\Contracts\Debug\ExceptionHandler;
911
use Illuminate\Contracts\Encryption\Encrypter;
1012
use Illuminate\Cookie\CookieValuePrefix;
1113
use Illuminate\Cookie\Middleware\EncryptCookies;
1214
use Illuminate\Http\Request;
15+
use Illuminate\Support\Traits\Macroable;
1316
use Laravel\Passport\ClientRepository;
1417
use Laravel\Passport\Passport;
1518
use Laravel\Passport\PassportUserProvider;
@@ -20,8 +23,10 @@
2023
use Nyholm\Psr7\Factory\Psr17Factory;
2124
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
2225

23-
class TokenGuard
26+
class TokenGuard implements Guard
2427
{
28+
use GuardHelpers, Macroable;
29+
2530
/**
2631
* The resource server instance.
2732
*
@@ -57,6 +62,20 @@ class TokenGuard
5762
*/
5863
protected $encrypter;
5964

65+
/**
66+
* The request instance.
67+
*
68+
* @var \Illuminate\Http\Request
69+
*/
70+
protected $request;
71+
72+
/**
73+
* The currently authenticated client.
74+
*
75+
* @var \Laravel\Passport\Client|null
76+
*/
77+
protected $client;
78+
6079
/**
6180
* Create a new token guard instance.
6281
*
@@ -65,56 +84,83 @@ class TokenGuard
6584
* @param \Laravel\Passport\TokenRepository $tokens
6685
* @param \Laravel\Passport\ClientRepository $clients
6786
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
87+
* @param \Illuminate\Http\Request $request
6888
* @return void
6989
*/
7090
public function __construct(
7191
ResourceServer $server,
7292
PassportUserProvider $provider,
7393
TokenRepository $tokens,
7494
ClientRepository $clients,
75-
Encrypter $encrypter
95+
Encrypter $encrypter,
96+
Request $request
7697
) {
7798
$this->server = $server;
7899
$this->tokens = $tokens;
79100
$this->clients = $clients;
80101
$this->provider = $provider;
81102
$this->encrypter = $encrypter;
103+
$this->request = $request;
82104
}
83105

84106
/**
85107
* Get the user for the incoming request.
86108
*
87-
* @param \Illuminate\Http\Request $request
88109
* @return mixed
89110
*/
90-
public function user(Request $request)
111+
public function user()
91112
{
92-
if ($request->bearerToken()) {
93-
return $this->authenticateViaBearerToken($request);
94-
} elseif ($request->cookie(Passport::cookie())) {
95-
return $this->authenticateViaCookie($request);
113+
if (! is_null($this->user)) {
114+
return $this->user;
115+
}
116+
117+
if ($this->request->bearerToken()) {
118+
return $this->user = $this->authenticateViaBearerToken($this->request);
119+
} elseif ($this->request->cookie(Passport::cookie())) {
120+
return $this->user = $this->authenticateViaCookie($this->request);
96121
}
97122
}
98123

124+
/**
125+
* Validate a user's credentials.
126+
*
127+
* @param array $credentials
128+
* @return bool
129+
*/
130+
public function validate(array $credentials = [])
131+
{
132+
return ! is_null((new static(
133+
$this->server,
134+
$this->provider,
135+
$this->tokens,
136+
$this->clients,
137+
$this->encrypter,
138+
$credentials['request'],
139+
))->user());
140+
}
141+
99142
/**
100143
* Get the client for the incoming request.
101144
*
102-
* @param \Illuminate\Http\Request $request
103145
* @return mixed
104146
*/
105-
public function client(Request $request)
147+
public function client()
106148
{
107-
if ($request->bearerToken()) {
108-
if (! $psr = $this->getPsrRequestViaBearerToken($request)) {
149+
if (! is_null($this->client)) {
150+
return $this->client;
151+
}
152+
153+
if ($this->request->bearerToken()) {
154+
if (! $psr = $this->getPsrRequestViaBearerToken($this->request)) {
109155
return;
110156
}
111157

112-
return $this->clients->findActive(
158+
return $this->client = $this->clients->findActive(
113159
$psr->getAttribute('oauth_client_id')
114160
);
115-
} elseif ($request->cookie(Passport::cookie())) {
116-
if ($token = $this->getTokenViaCookie($request)) {
117-
return $this->clients->findActive($token['aud']);
161+
} elseif ($this->request->cookie(Passport::cookie())) {
162+
if ($token = $this->getTokenViaCookie($this->request)) {
163+
return $this->client = $this->clients->findActive($token['aud']);
118164
}
119165
}
120166
}
@@ -285,6 +331,19 @@ protected function getTokenFromRequest($request)
285331
return $token;
286332
}
287333

334+
/**
335+
* Set the current request instance.
336+
*
337+
* @param \Illuminate\Http\Request $request
338+
* @return $this
339+
*/
340+
public function setRequest(Request $request)
341+
{
342+
$this->request = $request;
343+
344+
return $this;
345+
}
346+
288347
/**
289348
* Determine if the cookie contents should be serialized.
290349
*

src/PassportServiceProvider.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use DateInterval;
66
use Illuminate\Auth\Events\Logout;
7-
use Illuminate\Auth\RequestGuard;
87
use Illuminate\Config\Repository as Config;
98
use Illuminate\Support\Facades\Auth;
109
use Illuminate\Support\Facades\Cookie;
@@ -341,19 +340,18 @@ protected function registerGuard()
341340
* Make an instance of the token guard.
342341
*
343342
* @param array $config
344-
* @return \Illuminate\Auth\RequestGuard
343+
* @return \Laravel\Passport\Guards\TokenGuard
345344
*/
346345
protected function makeGuard(array $config)
347346
{
348-
return new RequestGuard(function ($request) use ($config) {
349-
return (new TokenGuard(
350-
$this->app->make(ResourceServer::class),
351-
new PassportUserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
352-
$this->app->make(TokenRepository::class),
353-
$this->app->make(ClientRepository::class),
354-
$this->app->make('encrypter')
355-
))->user($request);
356-
}, $this->app['request']);
347+
return new TokenGuard(
348+
$this->app->make(ResourceServer::class),
349+
new PassportUserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
350+
$this->app->make(TokenRepository::class),
351+
$this->app->make(ClientRepository::class),
352+
$this->app->make('encrypter'),
353+
$this->app->make('request')
354+
);
357355
}
358356

359357
/**

0 commit comments

Comments
 (0)