How to Migrate a Password Hash

How to Migrate a Password Hash

New in version 4.4: Password migration was introduced in Symfony 4.4.

In order to protect passwords, it is recommended to store them using the latest hash algorithms. This means that if a better hash algorithm is supported on your system, the user’s password should be rehashed using the newer algorithm and stored. That’s possible with the migrate_from option:

  1. Configure a new Encoder Using “migrate_from”
  2. Upgrade the Password
  3. Optionally, Trigger Password Migration From a Custom Encoder

Configure a new Encoder Using “migrate_from”

When a better hashing algorithm becomes available, you should keep the existing encoder(s), rename it, and then define the new one. Set the migrate_from option on the new encoder to point to the old, legacy encoder(s):

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. encoders:
    5. # an encoder used in the past for some users
    6. legacy:
    7. algorithm: sha256
    8. encode_as_base64: false
    9. iterations: 1
    10. App\Entity\User:
    11. # the new encoder, along with its options
    12. algorithm: sodium
    13. migrate_from:
    14. - bcrypt # uses the "bcrypt" encoder with the default options
    15. - legacy # uses the "legacy" encoder configured above
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:security="http://symfony.com/schema/dic/security"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/security
    7. https://symfony.com/schema/dic/security/security-1.0.xsd">
    8. <security:config>
    9. <!-- ... -->
    10. <security:encoder class="legacy"
    11. algorithm="sha256"
    12. encode-as-base64="false"
    13. iterations="1"
    14. />
    15. <!-- algorithm: the new encoder, along with its options -->
    16. <security:encoder class="App\Entity\User"
    17. algorithm="sodium"
    18. >
    19. <!-- uses the bcrypt encoder with the default options -->
    20. <security:migrate-from>bcrypt</security:migrate-from>
    21. <!-- uses the legacy encoder configured above -->
    22. <security:migrate-from>legacy</security:migrate-from>
    23. </security:encoder>
    24. </security:config>
    25. </container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'encoders' => [
    5. 'legacy' => [
    6. 'algorithm' => 'sha256',
    7. 'encode_as_base64' => false,
    8. 'iterations' => 1,
    9. ],
    10. 'App\Entity\User' => [
    11. // the new encoder, along with its options
    12. 'algorithm' => 'sodium',
    13. 'migrate_from' => [
    14. 'bcrypt', // uses the "bcrypt" encoder with the default options
    15. 'legacy', // uses the "legacy" encoder configured above
    16. ],
    17. ],
    18. ],
    19. ]);

With this setup:

  • New users will be encoded with the new algorithm;
  • Whenever a user logs in whose password is still stored using the old algorithm, Symfony will verify the password with the old algorithm and then rehash and update the password using the new algorithm.

Tip

The auto, native, bcrypt and argon encoders automatically enable password migration using the following list of migrate_from algorithms:

  1. PBKDF2 (which uses [hash_pbkdf2](https://www.php.net/manual/en/function.hash-pbkdf2.php "hash_pbkdf2"));
  2. Message digest (which uses [hash](https://www.php.net/manual/en/function.hash.php "hash"))

Both use the hash_algorithm setting as the algorithm. It is recommended to use migrate_from instead of hash_algorithm, unless the auto encoder is used.

Upgrade the Password

Upon successful login, the Security system checks whether a better algorithm is available to hash the user’s password. If it is, it’ll hash the correct password using the new hash. If you use a Guard authenticator, you first need to provide the original password to the Security system.

You can enable the upgrade behavior by implementing how this newly hashed password should be stored:

After this, you’re done and passwords are always hashed as secure as possible!

Provide the Password when using Guard

When you’re using a custom guard authenticator, you need to implement Symfony\Component\Security\Guard\PasswordAuthenticatedInterface. This interface defines a getPassword() method that returns the password for this login request. This password is used in the migration process:

  1. // src/Security/CustomAuthenticator.php
  2. namespace App\Security;
  3. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  4. // ...
  5. class CustomAuthenticator extends AbstractGuardAuthenticator implements PasswordAuthenticatedInterface
  6. {
  7. // ...
  8. public function getPassword($credentials): ?string
  9. {
  10. return $credentials['password'];
  11. }
  12. }

Upgrade the Password when using Doctrine

When using the entity user provider, implement Symfony\Component\Security\Core\User\PasswordUpgraderInterface in the UserRepository (see the Doctrine docs for information on how to create this class if it’s not already created). This interface implements storing the newly created password hash:

  1. // src/Repository/UserRepository.php
  2. namespace App\Repository;
  3. // ...
  4. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  5. class UserRepository extends EntityRepository implements PasswordUpgraderInterface
  6. {
  7. // ...
  8. public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
  9. {
  10. // set the new encoded password on the User object
  11. $user->setPassword($newEncodedPassword);
  12. // execute the queries on the database
  13. $this->getEntityManager()->flush();
  14. }
  15. }

Upgrade the Password when using a Custom User Provider

If you’re using a custom user provider, implement the Symfony\Component\Security\Core\User\PasswordUpgraderInterface in the user provider:

  1. // src/Security/UserProvider.php
  2. namespace App\Security;
  3. // ...
  4. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  5. class UserProvider implements UserProviderInterface, PasswordUpgraderInterface
  6. {
  7. // ...
  8. public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
  9. {
  10. // set the new encoded password on the User object
  11. $user->setPassword($newEncodedPassword);
  12. // ... store the new password
  13. }
  14. }

Trigger Password Migration From a Custom Encoder

If you’re using a custom password encoder, you can trigger the password migration by returning true in the needsRehash() method:

  1. // src/Security/CustomPasswordEncoder.php
  2. namespace App\Security;
  3. // ...
  4. use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
  5. class CustomPasswordEncoder implements PasswordEncoderInterface
  6. {
  7. // ...
  8. public function needsRehash(string $encoded): bool
  9. {
  10. // check whether the current password is hash using an outdated encoder
  11. $hashIsOutdated = ...;
  12. return $hashIsOutdated;
  13. }
  14. }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.