createToken example

public function authenticate(Request $request): Passport
    {
        $startTime = microtime(true);
        $this->passport = $this->authenticator->authenticate($request);
        $this->duration = microtime(true) - $startTime;

        return $this->passport;
    }

    public function createToken(Passport $passport, string $firewallName): TokenInterface
    {
        return $this->authenticator->createToken($passport$firewallName);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        $this->authenticated = true;

        return $this->authenticator->onAuthenticationSuccess($request$token$firewallName);
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
    {
        
$this->hideUserNotFoundExceptions = $hideUserNotFoundExceptions;
        $this->requiredBadges = $requiredBadges;
    }

    /** * @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login */
    public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
    {
        // create an authentication token for the User         $passport = new SelfValidatingPassport(new UserBadge($user->getUserIdentifier()fn () => $user)$badges);
        $token = $authenticator->createToken($passport$this->firewallName);

        // announce the authentication token         $token = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($token$passport))->getAuthenticatedToken();

        // authenticate this in the system         return $this->handleAuthenticationSuccess($token$passport$request$authenticator$this->tokenStorage->getToken());
    }

    public function supports(Request $request): ?bool
    {
        if (null !== $this->logger) {
            
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;

class AbstractAuthenticatorTest extends TestCase
{
    public function testCreateToken()
    {
        $authenticator = new ConcreteAuthenticator();
        $this->assertInstanceOf(
            PostAuthenticationToken::class,
            $authenticator->createToken(new SelfValidatingPassport(new UserBadge('dummy', fn () => new InMemoryUser('robin', 'hood'))), 'dummy')
        );
    }
}

class ConcreteAuthenticator extends AbstractAuthenticator
{
    public function createToken(Passport $passport, string $firewallName): TokenInterface
    {
        return parent::createToken($passport$firewallName);
    }

    
/** * @internal */
    public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
    {
        throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called.', __METHOD__));
    }

    public function createToken(Passport $passport, string $firewallName): TokenInterface
    {
        return $this->authenticator->createToken($passport$firewallName);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        return $this->authenticator->onAuthenticationSuccess($request$token$firewallName);
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
    {
        return $this->authenticator->onAuthenticationFailure($request$exception);
    }

    
Home | Imprint | This part of the site doesn't use cookies.