@@ -295,8 +295,8 @@ method that fits most use-cases::
295
295
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
296
296
use Symfony\Component\Security\Core\Exception\AuthenticationException;
297
297
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
298
- use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
299
298
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
299
+ use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
300
300
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
301
301
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
302
302
@@ -328,14 +328,7 @@ method that fits most use-cases::
328
328
throw new CustomUserMessageAuthenticationException('No API token provided');
329
329
}
330
330
331
- $user = $this->entityManager->getRepository(User::class)
332
- ->findOneBy(['apiToken' => $apiToken])
333
- ;
334
- if (null === $user) {
335
- throw new UsernameNotFoundException();
336
- }
337
-
338
- return new SelfValidatingPassport($user);
331
+ return new SelfValidatingPassport(new UserBadge($apiToken));
339
332
}
340
333
341
334
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
@@ -472,12 +465,23 @@ are supported by default:
472
465
$apiToken
473
466
));
474
467
475
- .. note ::
476
468
477
- If you don't need any credentials to be checked (e.g. a JWT token), you
478
- can use the
479
- :class: `Symfony\\ Component\\ Security\\ Http\\ Authenticator\\ Passport\\ SelfValidatingPassport `.
480
- This class only requires a user and optionally `Passport Badges `_.
469
+ Self Validating Passport
470
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
471
+ If you don't need any credentials to be checked (e.g. a JWT token), you can use the
472
+ :class: `Symfony\\ Component\\ Security\\ Http\\ Authenticator\\ Passport\\ SelfValidatingPassport `.
473
+ This class only requires a ``UserBadge `` object and optionally `Passport Badges `_.
474
+
475
+ You can also pass a user loader to the ``UserBadge ``. This callable receives the
476
+ ``$userIdentifier `` as argument and must return a ``UserInterface `` object
477
+ (otherwise a ``UsernameNotFoundException `` is thrown). If this is not set,
478
+ the default user provider will be used with ``$userIdentifier `` as username::
479
+
480
+ // ...
481
+ return new SelfValidatingPassport(new UserBadge($email, function ($username) {
482
+ return $this->userRepository->findOneBy(['email' => $username]);
483
+ });
484
+
481
485
482
486
Passport Badges
483
487
~~~~~~~~~~~~~~~
@@ -547,7 +551,7 @@ authenticator, you would initialize the passport like this::
547
551
``createAuthenticatedToken() ``)::
548
552
549
553
// ...
550
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface ;
554
+ use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge ;
551
555
552
556
class LoginAuthenticator extends AbstractAuthenticator
553
557
{
@@ -557,7 +561,7 @@ authenticator, you would initialize the passport like this::
557
561
{
558
562
// ... process the request
559
563
560
- $passport = new SelfValidatingPassport($username, []);
564
+ $passport = new SelfValidatingPassport(new UserBadge( $username) , []);
561
565
562
566
// set a custom attribute (e.g. scope)
563
567
$passport->setAttribute('scope', $oauthScope);
0 commit comments