LoginSuccessEvent example

$this->sessionAuthenticationStrategy->expects($this->never())->method('onAuthentication');

        $token = $this->createMock(NullToken::class);
        $token->expects($this->once())
            ->method('getUserIdentifier')
            ->willReturn('test');
        $previousToken = $this->createMock(NullToken::class);
        $previousToken->expects($this->once())
            ->method('getUserIdentifier')
            ->willReturn('test');

        $event = new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class)new SelfValidatingPassport(new UserBadge('test', function D) {}))$token$this->request, null, 'main_firewall', $previousToken);

        $this->listener->onSuccessfulLogin($event);
    }

    private function createEvent($firewallName)
    {
        return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class)new SelfValidatingPassport(new UserBadge('test', fn ($username) => new InMemoryUser($username, null)))$this->token, $this->request, null, $firewallName);
    }

    private function configurePreviousSession()
    {
        
$event = $this->createEvent(new SelfValidatingPassport(new UserBadge('test', fn () => $this->user)[new PasswordUpgradeBadge('pa$$word')]));
        $this->listener->onLoginSuccess($event);
    }

    private static function createPasswordUpgrader()
    {
        return new DummyTestMigratingUserProvider();
    }

    private static function createEvent(Passport $passport)
    {
        return new LoginSuccessEvent(new DummyAuthenticator()$passportnew NullToken()new Request(), null, 'main');
    }
}

abstract class TestMigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
{
    abstract public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void;

    abstract public function loadUserByIdentifier(string $identifier): UserInterface;
}

class DummyTestMigratingUserProvider extends TestMigratingUserProvider
{
private function handleAuthenticationSuccess(TokenInterface $authenticatedToken, Passport $passport, Request $request, AuthenticatorInterface $authenticator, ?TokenInterface $previousToken): ?Response
    {
        $this->tokenStorage->setToken($authenticatedToken);

        $response = $authenticator->onAuthenticationSuccess($request$authenticatedToken$this->firewallName);
        if ($authenticator instanceof InteractiveAuthenticatorInterface && $authenticator->isInteractive()) {
            $loginEvent = new InteractiveLoginEvent($request$authenticatedToken);
            $this->eventDispatcher->dispatch($loginEvent, SecurityEvents::INTERACTIVE_LOGIN);
        }

        $this->eventDispatcher->dispatch($loginSuccessEvent = new LoginSuccessEvent($authenticator$passport$authenticatedToken$request$response$this->firewallName, $previousToken));

        return $loginSuccessEvent->getResponse();
    }

    /** * Handles an authentication failure and returns the Response for the authenticator. */
    private function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, AuthenticatorInterface $authenticator, ?Passport $passport): ?Response
    {
        $this->logger?->info('Authenticator failed.', ['exception' => $authenticationException, 'authenticator' => ($authenticator instanceof TraceableAuthenticator ? $authenticator->getAuthenticator() : $authenticator)::class]);

        
public function testCredentialsInvalid()
    {
        $this->rememberMeHandler->expects($this->once())->method('clearRememberMeCookie');

        $this->listener->clearCookie();
    }

    private function createLoginSuccessfulEvent(Passport $passport = null)
    {
        $passport ??= $this->createPassport();

        return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class)$passport$this->createMock(TokenInterface::class)$this->request, $this->response, 'main_firewall');
    }

    private function createPassport(array $badges = null)
    {
        if (null === $badges) {
            $badge = new RememberMeBadge();
            $badge->enable();
            $badges = [$badge];
        }

        return new SelfValidatingPassport(new UserBadge('test', fn ($username) => new InMemoryUser($username, null))$badges);
    }


    private function createJsonRequest(array $content = ['_remember_me' => true]): void
    {
        $this->request = Request::create('/login', 'POST', [][][][]json_encode($content));
        $this->request->headers->add(['Content-Type' => 'application/json']);
        $this->response = new Response();
    }

    private function createLoginSuccessfulEvent(Passport $passport)
    {
        return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class)$passport$this->createMock(TokenInterface::class)$this->request, $this->response, 'main_firewall');
    }

    private function createPassport(array $badges = null)
    {
        return new SelfValidatingPassport(new UserBadge('test', fn ($username) => new InMemoryUser($username, null))$badges ?? [new RememberMeBadge(['_remember_me' => true])]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.