Skip to content

Commit 98173ac

Browse files
[13.x] Fix possible 500 error when retrieving user on token guard (#1871)
* catch exceptions when retrieving user from the provider * fix minor cs
1 parent ca3da23 commit 98173ac

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

src/Console/ClientCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function createDeviceCodeClient(ClientRepository $clients): Client
131131
{
132132
$confidential = $this->hasOption('public')
133133
? ! $this->option('public')
134-
: $this->confirm('Would you like to make this client confidential?', true);
134+
: $this->components->confirm('Would you like to make this client confidential?', true);
135135

136136
return $clients->createDeviceAuthorizationGrantClient($this->option('name'), $confidential);
137137
}
@@ -151,7 +151,7 @@ protected function createAuthCodeClient(ClientRepository $clients): Client
151151
: $this->components->confirm('Would you like to make this client confidential?', true);
152152

153153
$enableDeviceFlow = Passport::$deviceCodeGrantEnabled &&
154-
$this->confirm('Would you like to enable the device authorization flow for this client?');
154+
$this->components->confirm('Would you like to enable the device authorization flow for this client?');
155155

156156
return $clients->createAuthorizationCodeGrantClient(
157157
$this->option('name'), explode(',', $redirect), $confidential, null, $enableDeviceFlow

src/Guards/TokenGuard.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ protected function authenticateViaBearerToken(): ?Authenticatable
144144
// If the access token is valid we will retrieve the user according to the user ID
145145
// associated with the token. We will use the provider implementation which may
146146
// be used to retrieve users from Eloquent. Next, we'll be ready to continue.
147-
$user = $this->provider->retrieveById(
148-
$psr->getAttribute('oauth_user_id') ?: null
149-
);
150-
151-
if (! $user) {
147+
try {
148+
$user = $this->provider->retrieveById(
149+
$psr->getAttribute('oauth_user_id') ?: null
150+
);
151+
} catch (Exception) {
152152
return null;
153153
}
154154

155155
// Next, we will assign a token instance to this user which the developers may use
156156
// to determine if the token has a given scope, etc. This will be useful during
157157
// authorization such as within the developer's Laravel model policy classes.
158-
return $user->withAccessToken(AccessToken::fromPsrRequest($psr));
158+
return $user?->withAccessToken(AccessToken::fromPsrRequest($psr));
159159
}
160160

161161
/**
@@ -193,11 +193,13 @@ protected function authenticateViaCookie(): ?Authenticatable
193193
// If this user exists, we will return this user and attach a "transient" token to
194194
// the user model. The transient token assumes it has all scopes since the user
195195
// is physically logged into the application via the application's interface.
196-
if ($user = $this->provider->retrieveById($token['sub'])) {
197-
return $user->withAccessToken(new TransientToken);
196+
try {
197+
$user = $this->provider->retrieveById($token['sub']);
198+
} catch (Exception) {
199+
return null;
198200
}
199201

200-
return null;
202+
return $user?->withAccessToken(new TransientToken);
201203
}
202204

203205
/**

src/Http/Controllers/HandlesOAuthErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait HandlesOAuthErrors
1313
*
1414
* @template TResult
1515
*
16-
* @param \Closure(): TResult $callback
16+
* @param (\Closure(): TResult) $callback
1717
* @return TResult
1818
*
1919
* @throws \Laravel\Passport\Exceptions\OAuthServerException

src/Http/Middleware/CreateFreshApiToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function using(?string $guard = null): string
3838
/**
3939
* Handle an incoming request.
4040
*
41-
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
41+
* @param (\Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response)) $next
4242
*/
4343
public function handle(Request $request, Closure $next, ?string $guard = null): BaseResponse
4444
{

src/Http/Middleware/ValidateToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function using(array|string $param, string ...$params): string
3939
/**
4040
* Handle an incoming request.
4141
*
42-
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
42+
* @param (\Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response)) $next
4343
*/
4444
public function handle(Request $request, Closure $next, string ...$params): Response
4545
{

0 commit comments

Comments
 (0)