Skip to content

Commit 9a8a50c

Browse files
committed
docs(SecurityBundle): register alias for argument for password hasher
1 parent c7ce246 commit 9a8a50c

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

security.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ You can also manually hash a password by running:
461461
462462
$ php bin/console security:hash-password
463463
464-
Read more about all available hashers and password migration in
465-
:doc:`security/passwords`.
464+
Read more about all available hashers (including specific hashers) and password
465+
migration in :doc:`security/passwords`.
466466

467467
.. _firewalls-authentication:
468468
.. _a-authentication-firewalls:
@@ -2714,7 +2714,7 @@ anonymous users access by checking if there is no user set on the token::
27142714
}
27152715

27162716
.. versionadded:: 7.3
2717-
2717+
27182718
The ``$vote`` argument of the ``voteOnAttribute()`` method was introduced
27192719
in Symfony 7.3.
27202720

security/passwords.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,60 @@ After configuring the correct algorithm, you can use the
226226
throw new \Exception('Bad credentials, cannot delete this user.');
227227
}
228228
229+
Injecting a Specific Password Hasher
230+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231+
232+
In some cases, you might define a password hasher in your configuration that is
233+
not linked to a user entity but is instead identified by a unique key.
234+
For example, you might have a separate hasher for things like password recovery
235+
codes.
236+
237+
With the following configuration:
238+
239+
.. code-block:: yaml
240+
241+
# config/packages/security.yaml
242+
security:
243+
password_hashers:
244+
recovery_code: 'auto'
245+
246+
firewalls:
247+
# ...
248+
249+
It is possible to inject the recovery_code password hasher into any service.
250+
To do this, you can't rely on standard autowiring, as Symfony wouldn't know
251+
which specific hasher to provide.
252+
253+
Instead, you can use the ``#[Target]`` attribute to request the hasher by its
254+
configuration key::
255+
256+
// src/Controller/HomepageController.php
257+
namespace App\Controller;
258+
259+
use Symfony\Component\DependencyInjection\Attribute\Target;
260+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
261+
262+
class HomepageController extends AbstractController
263+
{
264+
public function __construct(
265+
#[Target('recovery_code')]
266+
private readonly PasswordHasherInterface $passwordHasher,
267+
) {
268+
}
269+
270+
#[Route('/')]
271+
public function index(): Response
272+
{
273+
$plaintextToken = 'some-secret-token';
274+
275+
// Note: use hash(), not hashPassword(), as we are not using a UserInterface object
276+
$hashedToken = $this->passwordHasher->hash($plaintextToken);
277+
}
278+
}
279+
280+
When injecting a specific hasher by its name, you should type-hint the generic
281+
:class:`Symfony\\Component\\PasswordHasher\\PasswordHasherInterface`.
282+
229283
Reset Password
230284
--------------
231285

0 commit comments

Comments
 (0)