@@ -770,12 +770,12 @@ Creating a custom Password Hasher
770770
771771If you need to create your own, it needs to follow these rules:
772772
773- #. The class must implement :class: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface `
774- (you can also extend :class: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasher ` );
773+ #. The class must implement :class: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface `
774+ (you can also implement :class: `Symfony\\ Component\\ PasswordHasher\\ LegacyPasswordHasherInterface ` if your hash algorithm uses a separate salt );
775775
776776#. The implementations of
777- :method: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface::hashPassword `
778- and :method: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface::isPasswordValid `
777+ :method: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface::hash `
778+ and :method: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface::verify `
779779 **must validate that the password length is no longer than 4096
780780 characters. ** This is for security reasons (see `CVE-2013-5750 `_).
781781
@@ -784,31 +784,31 @@ If you need to create your own, it needs to follow these rules:
784784
785785.. code-block :: php
786786
787- // src/Security/CustomVerySecureHasher.php
788- namespace App\Security;
787+ // src/Security/Hasher/ CustomVerySecureHasher.php
788+ namespace App\Security\Hasher ;
789789
790+ use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
790791 use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
791- use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
792- use Symfony\Component\Security\Core\Exception\BadCredentialsException;
792+ use Symfony\Component\PasswordHasher\PasswordHasherInterface;
793793
794- class CustomVerySecureHasher extends UserPasswordHasher
794+ class CustomVerySecureHasher implements PasswordHasherInterface
795795 {
796796 use CheckPasswordLengthTrait;
797797
798- public function hashPassword(UserInterface $user, string $plainPassword): string
798+ public function hash( string $plainPassword): string
799799 {
800- if ($this->isPasswordTooLong($user->getPassword() )) {
801- throw new BadCredentialsException('Invalid password.' );
800+ if ($this->isPasswordTooLong($plainPassword )) {
801+ throw new InvalidPasswordException( );
802802 }
803803
804804 // ... hash the plain password in a secure way
805805
806806 return $hashedPassword;
807807 }
808808
809- public function isPasswordValid(UserInterface $user , string $plainPassword): bool
809+ public function verify(string $hashedPassword , string $plainPassword): bool
810810 {
811- if ($ this->isPasswordTooLong($user->getPassword() )) {
811+ if ('' === $plainPassword || $ this->isPasswordTooLong($plainPassword )) {
812812 return false;
813813 }
814814
@@ -849,21 +849,21 @@ Now, define a password hasher using the ``id`` setting:
849849 <!-- ... -->
850850 <!-- id: the service ID of your custom hasher (the FQCN using the default services.yaml) -->
851851 <security : password_hasher class =" app_hasher"
852- id =" App\Security\Hasher\MyCustomPasswordHasher " />
852+ id =" App\Security\Hasher\CustomVerySecureHasher " />
853853 </config >
854854 </srv : container >
855855
856856 .. code-block :: php
857857
858858 // config/packages/security.php
859- use App\Security\Hasher\MyCustomPasswordHasher ;
859+ use App\Security\Hasher\CustomVerySecureHasher ;
860860 use Symfony\Config\SecurityConfig;
861861
862862 return static function (SecurityConfig $security) {
863863 // ...
864864 $security->passwordHasher('app_hasher')
865865 // the service ID of your custom hasher (the FQCN using the default services.yaml)
866- ->id(MyCustomPasswordHasher ::class)
866+ ->id(CustomVerySecureHasher ::class)
867867 ;
868868 };
869869
0 commit comments