Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[DX] New service to simplify password encoding#3995
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1459,30 +1459,57 @@ is available by calling the PHP function :phpfunction:`hash_algos`. | ||
| Determining the Hashed Password | ||
| ............................... | ||
| .. versionadded:: 2.6 | ||
| The ``security.password_encoder`` service was introduced in Symfony 2.6. | ||
| If you're storing users in the database and you have some sort of registration | ||
| form for users, you'll need to be able to determine the hashed password so | ||
| that you can set it on your user before inserting it. No matter what algorithm | ||
| you configure for your user object, the hashed password can always be determined | ||
| in the following way from a controller:: | ||
| $user = new Acme\UserBundle\Entity\User(); | ||
| $plainPassword = 'ryanpass'; | ||
| $encoded = $this->container->get('security.password_encoder') | ||
| ->encodePassword($user, $plainPassword); | ||
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.
| ||
| $user->setPassword($encoded); | ||
| In order for this to work, just make sure that you have the encoder for your | ||
| user class (e.g. ``Acme\UserBundle\Entity\User``) configured under the ``encoders`` | ||
| key in ``app/config/security.yml``. | ||
| .. sidebar:: Get the User Encoder | ||
| In some cases, you need a specific encoder for a given user (e.g. ``Acme\UserBundle\Entity\User``). | ||
| You can use the ``EncoderFactory`` to get this encoder:: | ||
| $factory = $this->get('security.encoder_factory'); | ||
| $user = new Acme\UserBundle\Entity\User(); | ||
| $encoder = $factory->getEncoder($user); | ||
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. I'm mixed on this. Will anyone ever really need to get the So, my question to everyone is: is this worth even mentioning? Certainly, in super-advanced cases, someone really smart could find this service if they need it. 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. I tend to agree with you. I can't imagine a use case where this is needed. It might add more confusion than clarifying anything if we keep it here. | ||
| .. caution:: | ||
| When you allow a user to submit a plaintext password (e.g. registration | ||
| form, change password form), you *must* have validation that guarantees | ||
| that the password is 4096 characters or less. Read more details in | ||
| :ref:`How to implement a simple Registration Form <cookbook-registration-password-max>`. | ||
| Validating a Plaintext Password | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Sometimes you want to check if a plain password is valid for a given user:: | ||
| // a user instance of some class which implements Symfony\Component\Security\Core\User\UserInterface | ||
| $user = ...; | ||
| // the password that should be checked | ||
| $plainPassword = ...; | ||
| $isValidPassword = $this->container->get('security.password_encoder') | ||
| ->isPasswordValid($user, $plainPassword); | ||
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. Hmm, now that I think about it, I also think that this is an "edge case". But, one use might be if you want the user to type in their old password to change to a new one or something similar. So let's keep this here. But I think we need to revisit these chapters later and maybe move some stuff around. | ||
| Retrieving the User Object | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||