PlaintextPasswordEncoder.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Core\Encoder;
  11. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  12. /**
  13. * PlaintextPasswordEncoder does not do any encoding but is useful in testing environments.
  14. *
  15. * As this encoder is not cryptographically secure, usage of it in production environments is discouraged.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class PlaintextPasswordEncoder extends BasePasswordEncoder
  20. {
  21. private $ignorePasswordCase;
  22. /**
  23. * @param bool $ignorePasswordCase Compare password case-insensitive
  24. */
  25. public function __construct(bool $ignorePasswordCase = false)
  26. {
  27. $this->ignorePasswordCase = $ignorePasswordCase;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function encodePassword(string $raw, ?string $salt)
  33. {
  34. if ($this->isPasswordTooLong($raw)) {
  35. throw new BadCredentialsException('Invalid password.');
  36. }
  37. return $this->mergePasswordAndSalt($raw, $salt);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function isPasswordValid(string $encoded, string $raw, ?string $salt)
  43. {
  44. if ($this->isPasswordTooLong($raw)) {
  45. return false;
  46. }
  47. $pass2 = $this->mergePasswordAndSalt($raw, $salt);
  48. if (!$this->ignorePasswordCase) {
  49. return $this->comparePasswords($encoded, $pass2);
  50. }
  51. return $this->comparePasswords(strtolower($encoded), strtolower($pass2));
  52. }
  53. }