-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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 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 |
---|---|---|
|
@@ -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:: | ||
|
||
$factory = $this->get('security.encoder_factory'); | ||
$user = new Acme\UserBundle\Entity\User(); | ||
$plainPassword = 'ryanpass'; | ||
$encoded = $this->container->get('security.password_encoder') | ||
->encodePassword($user, $plainPassword); | ||
|
||
$encoder = $factory->getEncoder($user); | ||
$password = $encoder->encodePassword('ryanpass', $user->getSalt()); | ||
$user->setPassword($password); | ||
$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); | ||
|
||
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. 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); | ||
|
||
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 | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
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.
$planPassword
is not defined in this context. Why don't we reuse "ryanpass" as it was before?