@@ -121,12 +121,12 @@ the given password is valid.
121
121
122
122
This functionality is offered by the :class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Provider\\ DaoAuthenticationProvider `.
123
123
It fetches the user's data from a :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserProviderInterface `,
124
- uses a :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface `
124
+ uses a :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface `
125
125
to create a hash of the password and returns an authenticated token if the
126
126
password was valid::
127
127
128
+ use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
128
129
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
129
- use Symfony\Component\Security\Core\Encoder\EncoderFactory;
130
130
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
131
131
use Symfony\Component\Security\Core\User\UserChecker;
132
132
@@ -145,14 +145,14 @@ password was valid::
145
145
// for some extra checks: is account enabled, locked, expired, etc.
146
146
$userChecker = new UserChecker();
147
147
148
- // an array of password encoders (see below)
149
- $encoderFactory = new EncoderFactory (...);
148
+ // an array of password hashers (see below)
149
+ $hasherFactory = new PasswordHasherFactoryInterface (...);
150
150
151
151
$daoProvider = new DaoAuthenticationProvider(
152
152
$userProvider,
153
153
$userChecker,
154
154
'secured_area',
155
- $encoderFactory
155
+ $hasherFactory
156
156
);
157
157
158
158
$daoProvider->authenticate($unauthenticatedToken);
@@ -165,96 +165,105 @@ password was valid::
165
165
It is also possible to let multiple user providers try to find the user's
166
166
data, using the :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ ChainUserProvider `.
167
167
168
- The Password Encoder Factory
169
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168
+ .. _the-password-encoder-factory :
169
+
170
+ The Password Hasher Factory
171
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
170
172
171
173
The :class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Provider\\ DaoAuthenticationProvider `
172
- uses an encoder factory to create a password encoder for a given type of
173
- user. This allows you to use different encoding strategies for different
174
- types of users. The default :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ EncoderFactory `
175
- receives an array of encoders ::
174
+ uses a factory to create a password hasher for a given type of user. This allows
175
+ you to use different hashing strategies for different types of users.
176
+ The default :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ PasswordHasherFactory `
177
+ receives an array of hashers ::
176
178
177
179
use Acme\Entity\LegacyUser;
178
- use Symfony\Component\Security\Core\Encoder\EncoderFactory ;
179
- use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder ;
180
+ use Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher ;
181
+ use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory ;
180
182
use Symfony\Component\Security\Core\User\InMemoryUser;
181
183
182
- $defaultEncoder = new MessageDigestPasswordEncoder ('sha512', true, 5000);
183
- $weakEncoder = new MessageDigestPasswordEncoder ('md5', true, 1);
184
+ $defaultHasher = new MessageDigestPasswordHasher ('sha512', true, 5000);
185
+ $weakHasher = new MessageDigestPasswordHasher ('md5', true, 1);
184
186
185
- $encoders = [
186
- InMemoryUser::class => $defaultEncoder ,
187
- LegacyUser::class => $weakEncoder ,
187
+ $hashers = [
188
+ InMemoryUser::class => $defaultHasher ,
189
+ LegacyUser::class => $weakHasher ,
188
190
// ...
189
191
];
190
- $encoderFactory = new EncoderFactory($encoders );
192
+ $hasherFactory = new PasswordHasherFactory($hashers );
191
193
192
- Each encoder should implement :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface `
194
+ Each hasher should implement :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface `
193
195
or be an array with a ``class `` and an ``arguments `` key, which allows the
194
- encoder factory to construct the encoder only when it is needed.
196
+ hasher factory to construct the hasher only when it is needed.
197
+
198
+ .. _creating-a-custom-password-encoder :
195
199
196
- Creating a custom Password Encoder
197
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
+ Creating a custom Password Hasher
201
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198
202
199
- There are many built-in password encoders . But if you need to create your
203
+ There are many built-in password hasher . But if you need to create your
200
204
own, it needs to follow these rules:
201
205
202
- #. The class must implement :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface `
203
- (you can also extend :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ BasePasswordEncoder `);
206
+ #. The class must implement :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface `
207
+ (you can also extend :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasher `);
204
208
205
209
#. The implementations of
206
- :method: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface::encodePassword `
210
+ :method: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface::hashPassword `
207
211
and
208
- :method: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface ::isPasswordValid `
212
+ :method: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface ::isPasswordValid `
209
213
must first of all make sure the password is not too long, i.e. the password length is no longer
210
214
than 4096 characters. This is for security reasons (see `CVE-2013-5750 `_), and you can use the
211
- :method: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ BasePasswordEncoder ::isPasswordTooLong `
215
+ :method: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ CheckPasswordLengthTrait ::isPasswordTooLong `
212
216
method for this check::
213
217
214
- use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
218
+ use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
219
+ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
215
220
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
216
221
217
- class FoobarEncoder extends BasePasswordEncoder
222
+ class FoobarHasher extends UserPasswordHasher
218
223
{
219
- public function encodePassword($raw, $salt)
224
+ use CheckPasswordLengthTrait;
225
+
226
+ public function hashPassword(UserInterface $user, string $plainPassword): string
220
227
{
221
- if ($this->isPasswordTooLong($raw )) {
228
+ if ($this->isPasswordTooLong($user->getPassword() )) {
222
229
throw new BadCredentialsException('Invalid password.');
223
230
}
224
231
225
232
// ...
226
233
}
227
234
228
- public function isPasswordValid($encoded, $raw, $salt )
235
+ public function isPasswordValid(UserInterface $user, string $plainPassword )
229
236
{
230
- if ($this->isPasswordTooLong($raw )) {
237
+ if ($this->isPasswordTooLong($user->getPassword() )) {
231
238
return false;
232
239
}
233
240
234
241
// ...
235
242
}
236
243
}
237
244
238
- Using Password Encoders
239
- ~~~~~~~~~~~~~~~~~~~~~~~
245
+ .. _using-password-encoders :
240
246
241
- When the :method: `Symfony\\ Component\\ Security\\ Core\\ Encoder\\ EncoderFactory::getEncoder `
242
- method of the password encoder factory is called with the user object as
243
- its first argument, it will return an encoder of type :class: `Symfony\\ Component\\ Security\\ Core\\ Encoder\\ PasswordEncoderInterface `
244
- which should be used to encode this user's password::
247
+ Using Password Hashers
248
+ ~~~~~~~~~~~~~~~~~~~~~~
249
+
250
+ When the :method: `Symfony\\ Component\\ PasswordHasher\\ Hasher\\ PasswordHasherFactory::getPasswordHasher `
251
+ method of the password hasher factory is called with the user object as
252
+ its first argument, it will return a hasher of type :class: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface `
253
+ which should be used to hash this user's password::
245
254
246
255
// a Acme\Entity\LegacyUser instance
247
256
$user = ...;
248
257
249
258
// the password that was submitted, e.g. when registering
250
259
$plainPassword = ...;
251
260
252
- $encoder = $encoderFactory->getEncoder ($user);
261
+ $hasher = $hasherFactory->getPasswordHasher ($user);
253
262
254
- // returns $weakEncoder (see above)
255
- $encodedPassword = $encoder->encodePassword($plainPassword , $user->getSalt() );
263
+ // returns $weakHasher (see above)
264
+ $hashedPassword = $hasher->hashPassword($user , $plainPassword );
256
265
257
- $user->setPassword($encodedPassword );
266
+ $user->setPassword($hashedPassword );
258
267
259
268
// ... save the user
260
269
@@ -267,11 +276,7 @@ in) is correct, you can use::
267
276
// the submitted password, e.g. from the login form
268
277
$plainPassword = ...;
269
278
270
- $validPassword = $encoder->isPasswordValid(
271
- $user->getPassword(), // the encoded password
272
- $plainPassword, // the submitted password
273
- $user->getSalt()
274
- );
279
+ $validPassword = $hasher->isPasswordValid($user, $plainPassword);
275
280
276
281
Authentication Events
277
282
---------------------
0 commit comments