CheckPassportEvent example

$this->csrfTokenManager->expects($this->any())
            ->method('isTokenValid')
            ->with(new CsrfToken('authenticator_token_id', 'abc123'))
            ->willReturn(false);

        $event = $this->createEvent($this->createPassport(new CsrfTokenBadge('authenticator_token_id', 'abc123')));
        $this->listener->checkPassport($event);
    }

    private function createEvent($passport)
    {
        return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport);
    }

    private function createPassport(?CsrfTokenBadge $badge)
    {
        $passport = new SelfValidatingPassport(new UserBadge('wouter', fn ($username) => new InMemoryUser($username, 'pass')));
        if ($badge) {
            $passport->addBadge($badge);
        }

        return $passport;
    }
}
$this->ldap = $this->createMock(LdapInterface::class);
    }

    /** * @dataProvider provideShouldNotCheckPassport */
    public function testShouldNotCheckPassport($authenticator$passport)
    {
        $this->ldap->expects($this->never())->method('bind');

        $listener = $this->createListener();
        $listener->onCheckPassport(new CheckPassportEvent($authenticator$passport));
    }

    public static function provideShouldNotCheckPassport()
    {
        // no LdapBadge         yield [new TestAuthenticator()new Passport(new UserBadge('test')new PasswordCredentials('s3cret'))];

        // ldap already resolved         $badge = new LdapBadge('app.ldap');
        $badge->markResolved();
        yield [new TestAuthenticator()new Passport(new UserBadge('test')new PasswordCredentials('s3cret')[$badge])];
    }

        return new SelfValidatingPassport(new UserBadge($username));
    }

    private function createLoginFailedEvent($passport)
    {
        return new LoginFailureEvent($this->createMock(AuthenticationException::class)$this->createMock(AuthenticatorInterface::class)$this->requestStack->getCurrentRequest(), null, 'main', $passport);
    }

    private function createCheckPassportEvent($passport)
    {
        return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport);
    }

    private function createRequest($ip = '192.168.1.0')
    {
        $request = new Request();
        $request->server->set('REMOTE_ADDR', $ip);

        return $request;
    }
}
public function testPostAuthValidCredentials()
    {
        $this->userChecker->expects($this->once())->method('checkPostAuth')->with($this->user);

        $this->listener->postCheckCredentials(new AuthenticationSuccessEvent(new PostAuthenticationToken($this->user, 'main', [])));
    }

    private function createCheckPassportEvent($passport = null)
    {
        $passport ??= new SelfValidatingPassport(new UserBadge('test', fn () => $this->user));

        return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport);
    }

    private function createAuthenticationSuccessEvent()
    {
        return new AuthenticationSuccessEvent(new PostAuthenticationToken($this->user, 'main', []));
    }
}
->onlyMethods(['addBadge'])
            ->setConstructorArgs([new UserBadge('wouter', fn () => $this->user)new PasswordCredentials('ThePa$$word')[new PasswordUpgradeBadge('ThePa$$word')]])
            ->getMock();

        $passport->expects($this->never())->method('addBadge')->with($this->isInstanceOf(PasswordUpgradeBadge::class));

        $this->listener->checkPassport($this->createEvent($passport));
    }

    private function createEvent($passport)
    {
        return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport);
    }
}
protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->listener = new UserProviderListener($this->userProvider);
    }

    public function testSetUserProvider()
    {
        $passport = new SelfValidatingPassport(new UserBadge('wouter'));

        $this->listener->checkPassport(new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport));

        $user = new InMemoryUser('wouter', null);
        $this->userProvider->createUser($user);
        $this->assertTrue($user->isEqualTo($passport->getUser()));
    }

    /** * @dataProvider provideCompletePassports */
    public function testNotOverrideUserLoader($passport)
    {
        
private function executeAuthenticator(AuthenticatorInterface $authenticator, Request $request): ?Response
    {
        $passport = null;
        $previousToken = $this->tokenStorage->getToken();

        try {
            // get the passport from the Authenticator             $passport = $authenticator->authenticate($request);

            // check the passport (e.g. password checking)             $event = new CheckPassportEvent($authenticator$passport);
            $this->eventDispatcher->dispatch($event);

            // check if all badges are resolved             $resolvedBadges = [];
            foreach ($passport->getBadges() as $badge) {
                if (!$badge->isResolved()) {
                    throw new BadCredentialsException(sprintf('Authentication failed: Security badge "%s" is not resolved, did you forget to register the correct listeners?', get_debug_type($badge)));
                }

                $resolvedBadges[] = $badge::class;
            }

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