@@ -770,12 +770,12 @@ Creating a custom Password Hasher
770
770
771
771
If you need to create your own, it needs to follow these rules:
772
772
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 );
775
775
776
776
#. 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 `
779
779
**must validate that the password length is no longer than 4096
780
780
characters. ** This is for security reasons (see `CVE-2013-5750 `_).
781
781
@@ -784,31 +784,31 @@ If you need to create your own, it needs to follow these rules:
784
784
785
785
.. code-block :: php
786
786
787
- // src/Security/CustomVerySecureHasher.php
788
- namespace App\Security;
787
+ // src/Security/Hasher/ CustomVerySecureHasher.php
788
+ namespace App\Security\Hasher ;
789
789
790
+ use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
790
791
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;
793
793
794
- class CustomVerySecureHasher extends UserPasswordHasher
794
+ class CustomVerySecureHasher implements PasswordHasherInterface
795
795
{
796
796
use CheckPasswordLengthTrait;
797
797
798
- public function hashPassword(UserInterface $user, string $plainPassword): string
798
+ public function hash( string $plainPassword): string
799
799
{
800
- if ($this->isPasswordTooLong($user->getPassword() )) {
801
- throw new BadCredentialsException('Invalid password.' );
800
+ if ($this->isPasswordTooLong($plainPassword )) {
801
+ throw new InvalidPasswordException( );
802
802
}
803
803
804
804
// ... hash the plain password in a secure way
805
805
806
806
return $hashedPassword;
807
807
}
808
808
809
- public function isPasswordValid(UserInterface $user , string $plainPassword): bool
809
+ public function verify(string $hashedPassword , string $plainPassword): bool
810
810
{
811
- if ($ this->isPasswordTooLong($user->getPassword() )) {
811
+ if ('' === $plainPassword || $ this->isPasswordTooLong($plainPassword )) {
812
812
return false;
813
813
}
814
814
@@ -849,21 +849,21 @@ Now, define a password hasher using the ``id`` setting:
849
849
<!-- ... -->
850
850
<!-- id: the service ID of your custom hasher (the FQCN using the default services.yaml) -->
851
851
<security : password_hasher class =" app_hasher"
852
- id =" App\Security\Hasher\MyCustomPasswordHasher " />
852
+ id =" App\Security\Hasher\CustomVerySecureHasher " />
853
853
</config >
854
854
</srv : container >
855
855
856
856
.. code-block :: php
857
857
858
858
// config/packages/security.php
859
- use App\Security\Hasher\MyCustomPasswordHasher ;
859
+ use App\Security\Hasher\CustomVerySecureHasher ;
860
860
use Symfony\Config\SecurityConfig;
861
861
862
862
return static function (SecurityConfig $security) {
863
863
// ...
864
864
$security->passwordHasher('app_hasher')
865
865
// the service ID of your custom hasher (the FQCN using the default services.yaml)
866
- ->id(MyCustomPasswordHasher ::class)
866
+ ->id(CustomVerySecureHasher ::class)
867
867
;
868
868
};
869
869
0 commit comments