@@ -781,12 +781,12 @@ Creating a custom Password Hasher
781
781
782
782
If you need to create your own, it needs to follow these rules:
783
783
784
- #. The class must implement :class: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface `
785
- (you can also extend :class: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasher ` );
784
+ #. The class must implement :class: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface `
785
+ (you can also implement :class: `Symfony\\ Component\\ PasswordHasher\\ LegacyPasswordHasherInterface ` if your hash algorithm uses a separate salt );
786
786
787
787
#. The implementations of
788
- :method: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface::hashPassword `
789
- and :method: `Symfony\\ Component\\ PasswordHasher\\ Hasher \\ UserPasswordHasherInterface::isPasswordValid `
788
+ :method: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface::hash `
789
+ and :method: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface::verify `
790
790
**must validate that the password length is no longer than 4096
791
791
characters. ** This is for security reasons (see `CVE-2013-5750 `_).
792
792
@@ -795,31 +795,31 @@ If you need to create your own, it needs to follow these rules:
795
795
796
796
.. code-block :: php
797
797
798
- // src/Security/CustomVerySecureHasher.php
799
- namespace App\Security;
798
+ // src/Security/Hasher/ CustomVerySecureHasher.php
799
+ namespace App\Security\Hasher ;
800
800
801
+ use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
801
802
use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
802
- use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
803
- use Symfony\Component\Security\Core\Exception\BadCredentialsException;
803
+ use Symfony\Component\PasswordHasher\PasswordHasherInterface;
804
804
805
- class CustomVerySecureHasher extends UserPasswordHasher
805
+ class CustomVerySecureHasher implements PasswordHasherInterface
806
806
{
807
807
use CheckPasswordLengthTrait;
808
808
809
- public function hashPassword(UserInterface $user, string $plainPassword): string
809
+ public function hash( string $plainPassword): string
810
810
{
811
- if ($this->isPasswordTooLong($user->getPassword() )) {
812
- throw new BadCredentialsException('Invalid password.' );
811
+ if ($this->isPasswordTooLong($plainPassword )) {
812
+ throw new InvalidPasswordException( );
813
813
}
814
814
815
815
// ... hash the plain password in a secure way
816
816
817
817
return $hashedPassword;
818
818
}
819
819
820
- public function isPasswordValid(UserInterface $user , string $plainPassword): bool
820
+ public function verify(string $hashedPassword , string $plainPassword): bool
821
821
{
822
- if ($ this->isPasswordTooLong($user->getPassword() )) {
822
+ if ('' === $plainPassword || $ this->isPasswordTooLong($plainPassword )) {
823
823
return false;
824
824
}
825
825
@@ -860,21 +860,21 @@ Now, define a password hasher using the ``id`` setting:
860
860
<!-- ... -->
861
861
<!-- id: the service ID of your custom hasher (the FQCN using the default services.yaml) -->
862
862
<security : password_hasher class =" app_hasher"
863
- id =" App\Security\Hasher\MyCustomPasswordHasher " />
863
+ id =" App\Security\Hasher\CustomVerySecureHasher " />
864
864
</config >
865
865
</srv : container >
866
866
867
867
.. code-block :: php
868
868
869
869
// config/packages/security.php
870
- use App\Security\Hasher\MyCustomPasswordHasher ;
870
+ use App\Security\Hasher\CustomVerySecureHasher ;
871
871
use Symfony\Config\SecurityConfig;
872
872
873
873
return static function (SecurityConfig $security) {
874
874
// ...
875
875
$security->passwordHasher('app_hasher')
876
876
// the service ID of your custom hasher (the FQCN using the default services.yaml)
877
- ->id(MyCustomPasswordHasher ::class)
877
+ ->id(CustomVerySecureHasher ::class)
878
878
;
879
879
};
880
880
0 commit comments