authenticateUser example

$request = $this->container->get('request_stack')->getCurrentRequest();
        $firewallName ??= $this->getFirewallConfig($request)?->getName();

        if (!$firewallName) {
            throw new LogicException('Unable to login as the current route is not covered by any firewall.');
        }

        $authenticator = $this->getAuthenticator($authenticatorName$firewallName);

        $this->container->get('security.user_checker')->checkPreAuth($user);

        return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user$authenticator$request$badges);
    }

    /** * Logout the current user by dispatching the LogoutEvent. * * @param bool $validateCsrfToken Whether to look for a valid CSRF token based on the `logout` listener configuration * * @return Response|null The LogoutEvent's Response if any * * @throws LogoutException When $validateCsrfToken is true and the CSRF token is not found or invalid */
    


    public function testAuthenticateUser()
    {
        $authenticator = $this->createAuthenticator();
        $authenticator->expects($this->any())->method('createToken')->willReturn($this->token);
        $authenticator->expects($this->any())->method('onAuthenticationSuccess')->willReturn($this->response);

        $this->tokenStorage->expects($this->once())->method('setToken')->with($this->token);

        $manager = $this->createManager([$authenticator]);
        $manager->authenticateUser($this->user, $authenticator$this->request);
    }

    public function testAuthenticateUserCanModifyTokenFromEvent()
    {
        $authenticator = $this->createAuthenticator();
        $authenticator->expects($this->any())->method('createToken')->willReturn($this->token);
        $authenticator->expects($this->any())->method('onAuthenticationSuccess')->willReturn($this->response);

        $modifiedToken = $this->createMock(TokenInterface::class);
        $modifiedToken->expects($this->any())->method('getUser')->willReturn($this->user);
        $listenerCalled = false;
        
$requestStack = new RequestStack();
        $user = new InMemoryUser('username', 'password');
        $userProvider = new InMemoryUserProvider();
        $authenticator = new HttpBasicAuthenticator('name', $userProvider);
        $request = new Request();

        $userAuthenticator = new UserAuthenticator($firewallMap$container$requestStack);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Cannot determine the correct Symfony\Bundle\SecurityBundle\Security\UserAuthenticator to use: there is no active Request and so, the firewall cannot be determined. Try using a specific Symfony\Bundle\SecurityBundle\Security\UserAuthenticator service.');

        $userAuthenticator->authenticateUser($user$authenticator$request);
    }
}
use FirewallAwareTrait;

    public function __construct(FirewallMap $firewallMap, ContainerInterface $userAuthenticators, RequestStack $requestStack)
    {
        $this->firewallMap = $firewallMap;
        $this->locator = $userAuthenticators;
        $this->requestStack = $requestStack;
    }

    public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
    {
        return $this->getForFirewall()->authenticateUser($user$authenticator$request$badges);
    }
}
Home | Imprint | This part of the site doesn't use cookies.