InMemoryUserProvider example

class AccessTokenAuthenticatorTest extends TestCase
{
    private AccessTokenHandlerInterface $accessTokenHandler;
    private AccessTokenExtractorInterface $accessTokenExtractor;
    private InMemoryUserProvider $userProvider;

    protected function setUp(): void
    {
        $this->accessTokenHandler = $this->createMock(AccessTokenHandlerInterface::class);
        $this->accessTokenExtractor = $this->createMock(AccessTokenExtractorInterface::class);
        $this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
    }

    public function testAuthenticateWithoutAccessToken()
    {
        $this->expectException(BadCredentialsException::class);
        $this->expectExceptionMessage('Invalid credentials.');

        $request = Request::create('/test');

        $this->accessTokenExtractor
            ->expects($this->once())
            
use Symfony\Component\Translation\Translator;

class JsonLoginAuthenticatorTest extends TestCase
{
    use ExpectDeprecationTrait;

    private InMemoryUserProvider $userProvider;
    private JsonLoginAuthenticator $authenticator;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
    }

    /** * @dataProvider provideSupportData */
    public function testSupport($request)
    {
        $this->setUpAuthenticator();

        $this->assertTrue($this->authenticator->supports($request));
    }

    
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Tests\Authenticator\Fixtures\PasswordUpgraderProvider;

class HttpBasicAuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private HttpBasicAuthenticator $authenticator;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();

        $hasherFactory = $this->createMock(PasswordHasherFactoryInterface::class);
        $hasher = $this->createMock(PasswordHasherInterface::class);
        $hasherFactory
            ->expects($this->any())
            ->method('getPasswordHasher')
            ->willReturn($hasher);

        $this->authenticator = new HttpBasicAuthenticator('test', $this->userProvider);
    }

    
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Tests\Authenticator\InMemoryAccessTokenHandler;

class FormEncodedBodyAccessTokenAuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private AccessTokenAuthenticator $authenticator;
    private AccessTokenHandlerInterface $accessTokenHandler;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->accessTokenHandler = new InMemoryAccessTokenHandler();
    }

    public function testSupport()
    {
        $this->setUpAuthenticator();
        $request = new Request([][][][][]['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
        $request->request->set('access_token', 'INVALID_ACCESS_TOKEN');
        $request->setMethod(Request::METHOD_POST);

        $this->assertNull($this->authenticator->supports($request));
    }
class SignatureRememberMeHandlerTest extends TestCase
{
    private SignatureHasher $signatureHasher;
    private InMemoryUserProvider $userProvider;
    private Request $request;
    private RequestStack $requestStack;
    private SignatureRememberMeHandler $handler;

    protected function setUp(): void
    {
        $this->signatureHasher = new SignatureHasher(PropertyAccess::createPropertyAccessor()[], 's3cret');
        $this->userProvider = new InMemoryUserProvider();
        $this->requestStack = new RequestStack();
        $this->request = Request::create('/login');
        $this->requestStack->push($this->request);
        $this->handler = new SignatureRememberMeHandler($this->signatureHasher, $this->userProvider, $this->requestStack, []);
    }

    /** * @group time-sensitive */
    public function testCreateRememberMeCookie()
    {
        
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Tests\Authenticator\InMemoryAccessTokenHandler;

class QueryAccessTokenAuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private AccessTokenAuthenticator $authenticator;
    private AccessTokenHandlerInterface $accessTokenHandler;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->accessTokenHandler = new InMemoryAccessTokenHandler();
    }

    public function testSupport()
    {
        $this->setUpAuthenticator();
        $request = new Request();
        $request->query->set('access_token', 'INVALID_ACCESS_TOKEN');

        $this->assertNull($this->authenticator->supports($request));
    }

    
use Symfony\Component\Security\Http\Tests\Authenticator\Fixtures\PasswordUpgraderProvider;

class FormLoginAuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private MockObject&AuthenticationSuccessHandlerInterface $successHandler;
    private MockObject&AuthenticationFailureHandlerInterface $failureHandler;
    private FormLoginAuthenticator $authenticator;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
        $this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
        $this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
    }

    /** * @dataProvider provideUsernamesForLength */
    public function testHandleWhenUsernameLength($username$ok)
    {
        if ($ok) {
            $this->expectNotToPerformAssertions();
        }
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;

class UserAuthenticatorTest extends TestCase
{
    public function testThrowsLogicExceptionIfCurrentRequestIsNull()
    {
        $container = new Container();
        $firewallMap = new FirewallMap($container[]);
        $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 Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Tests\Authenticator\InMemoryAccessTokenHandler;

class ChainedAccessTokenExtractorsTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private AccessTokenAuthenticator $authenticator;
    private AccessTokenHandlerInterface $accessTokenHandler;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->accessTokenHandler = new InMemoryAccessTokenHandler();
    }

    /** * @dataProvider provideSupportData */
    public function testSupport($request)
    {
        $this->setUpAuthenticator();

        $this->assertNull($this->authenticator->supports($request));
    }
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Http\Authenticator\X509Authenticator;

class X509AuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private X509Authenticator $authenticator;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main');
    }

    /** * @dataProvider provideServerVars */
    public function testAuthentication($username$credentials)
    {
        $serverVars = [];
        if ('' !== $username) {
            $serverVars['SSL_CLIENT_S_DN_Email'] = $username;
        }

    private TokenStorage $tokenStorage;
    private InMemoryUserProvider $userProvider;
    private MockObject&UserCheckerInterface $userChecker;
    private MockObject&AccessDecisionManagerInterface $accessDecisionManager;
    private Request $request;
    private RequestEvent $event;

    protected function setUp(): void
    {
        $this->tokenStorage = new TokenStorage();
        $this->userProvider = new InMemoryUserProvider(['kuba' => []]);
        $this->userChecker = $this->createMock(UserCheckerInterface::class);
        $this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class);
        $this->request = new Request();
        $this->event = new RequestEvent($this->createMock(HttpKernelInterface::class)$this->request, HttpKernelInterface::MAIN_REQUEST);
    }

    public function testFirewallNameIsRequired()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('$firewallName must not be empty');
        new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, '', $this->accessDecisionManager);
    }
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Tests\Authenticator\InMemoryAccessTokenHandler;

class HeaderAccessTokenAuthenticatorTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private AccessTokenAuthenticator $authenticator;
    private AccessTokenHandlerInterface $accessTokenHandler;

    protected function setUp(): void
    {
        $this->userProvider = new InMemoryUserProvider();
        $this->accessTokenHandler = new InMemoryAccessTokenHandler();
    }

    /** * @dataProvider provideSupportData */
    public function testSupport($request)
    {
        $this->setUpAuthenticator();

        $this->assertNull($this->authenticator->supports($request));
    }
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
use Symfony\Component\Security\Http\EventListener\UserProviderListener;

class UserProviderListenerTest extends TestCase
{
    private InMemoryUserProvider $userProvider;
    private UserProviderListener $listener;

    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);
        

    public function testSupport(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator$parameterName)
    {
        $request = $this->createRequest([$parameterName => 'TheUsername']);

        $this->assertTrue($authenticator->supports($request));
    }

    public function testSupportNoUser()
    {
        $authenticator = new RemoteUserAuthenticator(new InMemoryUserProvider()new TokenStorage(), 'main');

        $this->assertFalse($authenticator->supports($this->createRequest([])));
    }

    public function testSupportTokenStorageWithToken()
    {
        $tokenStorage = new TokenStorage();
        $tokenStorage->setToken(new PreAuthenticatedToken(new InMemoryUser('username', null), 'main'));

        $authenticator = new RemoteUserAuthenticator(new InMemoryUserProvider()$tokenStorage, 'main');

        
$provider = $this->createProvider();

        $refreshedUser = $provider->refreshUser($user);
        $this->assertEquals('foo', $refreshedUser->getPassword());
        $this->assertEquals(['ROLE_USER']$refreshedUser->getRoles());
        $this->assertFalse($refreshedUser->isEnabled());
    }

    protected function createProvider(): InMemoryUserProvider
    {
        return new InMemoryUserProvider([
            'fabien' => [
                'password' => 'foo',
                'enabled' => false,
                'roles' => ['ROLE_USER'],
            ],
        ]);
    }

    public function testCreateUser()
    {
        $provider = new InMemoryUserProvider();
        
Home | Imprint | This part of the site doesn't use cookies.