Skip to content

[Security] Update experimental_authenticators : add UserBadge #14672

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

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 20 additions & 16 deletions security/experimental_authenticators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ method that fits most use-cases::
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;

Expand Down Expand Up @@ -328,14 +328,7 @@ method that fits most use-cases::
throw new CustomUserMessageAuthenticationException('No API token provided');
}

$user = $this->entityManager->getRepository(User::class)
->findOneBy(['apiToken' => $apiToken])
;
if (null === $user) {
throw new UsernameNotFoundException();
}

return new SelfValidatingPassport($user);
return new SelfValidatingPassport(new UserBadge($apiToken));
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
Expand Down Expand Up @@ -472,12 +465,23 @@ are supported by default:
$apiToken
));

.. note::

If you don't need any credentials to be checked (e.g. a JWT token), you
can use the
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\SelfValidatingPassport`.
This class only requires a user and optionally `Passport Badges`_.
Self Validating Passport
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you don't need any credentials to be checked (e.g. a JWT token), you can use the
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\SelfValidatingPassport`.
This class only requires a ``UserBadge`` object and optionally `Passport Badges`_.

You can also pass a user loader to the ``UserBadge``. This callable receives the
``$userIdentifier`` as argument and must return a ``UserInterface`` object
(otherwise a ``UsernameNotFoundException`` is thrown). If this is not set,
the default user provider will be used with ``$userIdentifier`` as username::

// ...
return new SelfValidatingPassport(new UserBadge($email, function ($username) {
return $this->userRepository->findOneBy(['email' => $username]);
});


Passport Badges
~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -547,7 +551,7 @@ authenticator, you would initialize the passport like this::
``createAuthenticatedToken()``)::

// ...
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;

class LoginAuthenticator extends AbstractAuthenticator
{
Expand All @@ -557,7 +561,7 @@ authenticator, you would initialize the passport like this::
{
// ... process the request

$passport = new SelfValidatingPassport($username, []);
$passport = new SelfValidatingPassport(new UserBadge($username), []);

// set a custom attribute (e.g. scope)
$passport->setAttribute('scope', $oauthScope);
Expand Down