Skip to content

Commit bc5b997

Browse files
committed
conditionally use password hasher instead of encoder
1 parent 1d0cd3d commit bc5b997

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function generateClass(string $className, string $templateName, array $va
6969
$variables = array_merge($variables, [
7070
'class_name' => Str::getShortClassName($className),
7171
'namespace' => Str::getNamespace($className),
72+
'generator' => $this->templateComponentGenerator,
7273
]);
7374

7475
$this->addOperation($targetPath, $templateName, $variables);

src/Maker/MakeAuthenticator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Symfony\Component\HttpFoundation\Request;
4141
use Symfony\Component\HttpFoundation\Response;
4242
use Symfony\Component\HttpKernel\Kernel;
43+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
4344
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
4445
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
4546
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
@@ -337,6 +338,8 @@ private function generateAuthenticatorClass(array $securityData, string $authent
337338
$guardTemplateVariables = [
338339
'user_class_name' => $userClassNameDetails->getShortName(),
339340
'user_needs_encoder' => $hasEncoder,
341+
'password_class_details' => $generator->createClassNameDetails(UserPasswordEncoderInterface::class, '\\'),
342+
'password_variable_name' => 'passwordEncoder',
340343
'user_is_entity' => $isEntity,
341344
'provider_key_type_hint' => $this->providerKeyTypeHint(),
342345
'password_authenticated' => $guardPasswordAuthenticated,
@@ -353,7 +356,15 @@ private function generateAuthenticatorClass(array $securityData, string $authent
353356
}
354357

355358
if ($hasEncoder) {
356-
$useStatements[] = UserPasswordEncoderInterface::class;
359+
$encoder = UserPasswordEncoderInterface::class;
360+
361+
if (interface_exists(UserPasswordHasherInterface::class)) {
362+
$encoder = UserPasswordHasherInterface::class;
363+
$guardTemplateVariables['password_class_details'] = $generator->createClassNameDetails(UserPasswordHasherInterface::class, '\\');
364+
$guardTemplateVariables['password_variable_name'] = 'passwordHasher';
365+
}
366+
367+
$useStatements[] = $encoder;
357368
}
358369

359370
if ($guardPasswordAuthenticated) {

src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class <?= $class_name; ?> extends AbstractFormLoginAuthenticator<?= $password_au
1313
<?= $user_is_entity ? " private \$entityManager;\n" : null ?>
1414
private $urlGenerator;
1515
private $csrfTokenManager;
16-
<?= $user_needs_encoder ? " private \$passwordEncoder;\n" : null ?>
16+
<?= $user_needs_encoder ? sprintf(" private %s$%s;\n", $generator->getPropertyType($password_class_details), $password_variable_name) : null?>
1717

18-
public function __construct(<?= $user_is_entity ? 'EntityManagerInterface $entityManager, ' : null ?>UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager<?= $user_needs_encoder ? ', UserPasswordEncoderInterface $passwordEncoder' : null ?>)
18+
public function __construct(<?= $user_is_entity ? 'EntityManagerInterface $entityManager, ' : null ?>UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager<?= $user_needs_encoder ? sprintf(', %s $%s', $password_class_details->getShortName(), $password_variable_name) : null ?>)
1919
{
2020
<?= $user_is_entity ? " \$this->entityManager = \$entityManager;\n" : null ?>
2121
$this->urlGenerator = $urlGenerator;
2222
$this->csrfTokenManager = $csrfTokenManager;
23-
<?= $user_needs_encoder ? " \$this->passwordEncoder = \$passwordEncoder;\n" : null ?>
23+
<?= $user_needs_encoder ? sprintf(" \$this->%s = \$%s;\n", $password_variable_name, $password_variable_name) : null ?>
2424
}
2525

2626
public function supports(Request $request)
@@ -65,10 +65,13 @@ public function getUser($credentials, UserProviderInterface $userProvider)
6565

6666
public function checkCredentials($credentials, UserInterface $user)
6767
{
68-
<?= $user_needs_encoder ? "return \$this->passwordEncoder->isPasswordValid(\$user, \$credentials['password']);\n"
69-
: "// Check the user's password or other credentials and return true or false
68+
<?php if ($user_needs_encoder): ?>
69+
return $this-><?= $password_variable_name ?>->isPasswordValid($user, $credentials['password']);
70+
<?php else : ?>
71+
// Check the user's password or other credentials and return true or false
7072
// If there are no credentials to check, you can just return true
71-
throw new \Exception('TODO: check the credentials inside '.__FILE__);\n" ?>
73+
throw new \Exception('TODO: check the credentials inside '.__FILE__);
74+
<?php endif ?>
7275
}
7376

7477
<?php if ($password_authenticated): ?>

0 commit comments

Comments
 (0)