-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Form] Add hash_mapping option to PasswordType #42883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Form\Extension\Core\EventListener; | ||
|
|
||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
| use Symfony\Component\Form\FormEvent; | ||
| use Symfony\Component\Form\FormEvents; | ||
| use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
| use Symfony\Component\PropertyAccess\PropertyAccess; | ||
| use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
| use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
|
|
||
| /** | ||
| * @author Sébastien Alfaiate <s.alfaiate@webarea.fr> | ||
| */ | ||
| class PasswordHasherListener implements EventSubscriberInterface | ||
| { | ||
| private $passwordHasher; | ||
| private $propertyAccessor; | ||
|
|
||
| public function __construct(UserPasswordHasherInterface $passwordHasher = null, PropertyAccessorInterface $propertyAccessor = null) | ||
| { | ||
| $this->passwordHasher = $passwordHasher; | ||
| $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); | ||
| } | ||
|
|
||
| public static function getSubscribedEvents() | ||
| { | ||
| return [ | ||
| FormEvents::SUBMIT => ['hashPassword', -256], | ||
| ]; | ||
| } | ||
|
|
||
| public function hashPassword(FormEvent $event) | ||
| { | ||
| $form = $event->getForm(); | ||
| $parentForm = $form->getParent(); | ||
|
|
||
| if ($parentForm && $parentForm->getConfig()->getType()->getInnerType() instanceof RepeatedType) { | ||
| $parentForm = $parentForm->getParent(); | ||
| } | ||
|
|
||
| if ($parentForm && ($user = $parentForm->getData()) && ($user instanceof PasswordAuthenticatedUserInterface)) { | ||
| $this->propertyAccessor->setValue( | ||
| $user, | ||
| $form->getConfig()->getOption('hash_mapping'), | ||
| $this->passwordHasher->hashPassword($user, $event->getData()) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,41 @@ | |
| namespace Symfony\Component\Form\Extension\Core\Type; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Exception\InvalidConfigurationException; | ||
| use Symfony\Component\Form\Extension\Core\EventListener\PasswordHasherListener; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\Form\FormInterface; | ||
| use Symfony\Component\Form\FormView; | ||
| use Symfony\Component\OptionsResolver\Options; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
| use Symfony\Component\PropertyAccess\PropertyAccess; | ||
| use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
|
||
| class PasswordType extends AbstractType | ||
| { | ||
| private $passwordHasher; | ||
| private $propertyAccessor; | ||
|
|
||
| public function __construct(UserPasswordHasherInterface $passwordHasher = null, PropertyAccessorInterface $propertyAccessor = null) | ||
| { | ||
| $this->passwordHasher = $passwordHasher; | ||
| $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function buildForm(FormBuilderInterface $builder, array $options) | ||
| { | ||
| if ($options['hash_mapping']) { | ||
| if ($options['hash_mapping'] == $builder->getName()) { | ||
| throw new InvalidConfigurationException('The hash_mapping property must be a different.'); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I had to use |
||
| } | ||
| $builder->addEventSubscriber(new PasswordHasherListener($this->passwordHasher, $this->propertyAccessor)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
|
|
@@ -36,6 +64,7 @@ public function configureOptions(OptionsResolver $resolver) | |
| { | ||
| $resolver->setDefaults([ | ||
| 'always_empty' => true, | ||
| 'hash_mapping' => null, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this new approach
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you are right, and this is also the reason we cannot use the previous PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| 'trim' => false, | ||
| 'invalid_message' => function (Options $options, $previousValue) { | ||
| return ($options['legacy_error_messages'] ?? true) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
hash_mappingis referring to the same property path as the mapped field then we are hashing the plain password before validating it, which looks wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if something goes wrong during plain password validation you can end up with a wrong hashed password (from #42807 (review)
in the DB if your object is a Doctrine entity and you flush changes, but also potentially when serializing the user in the session if the edited user is the currently authenticated user)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this PR is to store the password hash in a different property.
I did the change so
hash_mappingcannot refer to itself.And the
PasswordTypefield should be unmapped. Maybe I should force it ifhash_mappingis used?Also, if some validation constraints are put on the entity password property (the hashed one), for example a
NotBlank, the hash needs to be populated before the validation to work.And regarding your second comment, if something goes wrong during the validation, "the issue" you described is not really specific to this feature.
All others fields of the form are also already populate in the entity before validation. The same behavior already happens even if we don't use this new feature.