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

Simplify JWTTokenAuthenticator::loadUser #1102

Merged
merged 1 commit into from
Jan 16, 2023
Merged
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
46 changes: 18 additions & 28 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,42 +295,32 @@ protected function getPreAuthenticationTokenStorage()
*/
protected function loadUser(UserProviderInterface $userProvider, array $payload, $identity)
{
if ($userProvider instanceof PayloadAwareUserProviderInterface) {
return $userProvider->loadUserByUsernameAndPayload($identity, $payload);
}

if ($userProvider instanceof ChainUserProvider) {
foreach ($userProvider->getProviders() as $provider) {
try {
if ($provider instanceof PayloadAwareUserProviderInterface) {
return $provider->loadUserByUsernameAndPayload($identity, $payload);
}
$providers = $userProvider instanceof ChainUserProvider ? $userProvider->getProviders() : [$userProvider];

if (method_exists($provider, 'loadUserByIdentifier')) {
return $provider->loadUserByIdentifier($identity);
}
foreach ($providers as $provider) {
try {
if ($provider instanceof PayloadAwareUserProviderInterface) {
return $provider->loadUserByUsernameAndPayload($identity, $payload);
}

return $provider->loadUserByUsername($identity);
} catch (SecurityUserNotFoundException | UsernameNotFoundException $e) {
// try next one
if (method_exists($provider, 'loadUserByIdentifier')) {
return $provider->loadUserByIdentifier($identity);
}
}

if (class_exists(SecurityUserNotFoundException::class)) {
$ex = new SecurityUserNotFoundException(sprintf('There is no user with name "%s".', $identity));
$ex->setUserIdentifier($identity);
} else {
$ex = new UsernameNotFoundException(sprintf('There is no user with name "%s".', $identity));
$ex->setUsername($identity);
return $provider->loadUserByUsername($identity);
} catch (SecurityUserNotFoundException | UsernameNotFoundException $e) {
// try next one
}

throw $ex;
}

if (method_exists($userProvider, 'loadUserByIdentifier')) {
return $userProvider->loadUserByIdentifier($identity);
if (class_exists(SecurityUserNotFoundException::class)) {
$ex = new SecurityUserNotFoundException(sprintf('There is no user with name "%s".', $identity));
$ex->setUserIdentifier($identity);
} else {
$ex = new UsernameNotFoundException(sprintf('There is no user with name "%s".', $identity));
$ex->setUsername($identity);
}

return $userProvider->loadUserByUsername($identity);
throw $ex;
}
}