PasswordHasherFactory example

use Symfony\Component\PasswordHasher\Hasher\PasswordHasherAwareInterface;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

class PasswordHasherFactoryTest extends TestCase
{
    public function testGetHasherWithMessageDigestHasher()
    {
        $factory = new PasswordHasherFactory([PasswordAuthenticatedUserInterface::class => [
            'class' => MessageDigestPasswordHasher::class,
            'arguments' => ['sha512', true, 5],
        ]]);

        $hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
        $expectedHasher = new MessageDigestPasswordHasher('sha512', true, 5);

        $this->assertEquals($expectedHasher->hash('foo', 'moo')$hasher->hash('foo', 'moo'));
    }

    public function testGetHasherWithService()
    {
yield 'user_class_given' => [
            ['p@ssw0rd', 'App'],
            ['App\Entity\User'],
        ];
    }

    protected function setUp(): void
    {
        $this->colSize = getenv('COLUMNS');
        putenv('COLUMNS='.(119 + \strlen(\PHP_EOL)));

        $hasherFactory = new PasswordHasherFactory([
            InMemoryUser::class => ['algorithm' => 'plaintext'],
            'Custom\Class\Native\User' => ['algorithm' => 'native', 'cost' => 10],
            'Custom\Class\Pbkdf2\User' => ['algorithm' => 'pbkdf2', 'hash_algorithm' => 'sha512', 'iterations' => 1000, 'encode_as_base64' => true],
            'Custom\Class\Test\User' => ['algorithm' => 'test'],
        ]);

        $this->passwordHasherCommandTester = new CommandTester(new UserPasswordHashCommand(
            $hasherFactory,
            [InMemoryUser::class, 'Custom\Class\Native\User', 'Custom\Class\Pbkdf2\User', 'Custom\Class\Test\User']
        ));
    }

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