vendor/symfony/security-http/RememberMe/TokenBasedRememberMeServices.php line 21

Open in your IDE?
  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\Http\RememberMe;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. trigger_deprecation('symfony/security-http', '5.4', 'The "%s" class is deprecated, use "%s" instead.', TokenBasedRememberMeServices::class, SignatureRememberMeHandler::class);
  18. /**
  19. * Concrete implementation of the RememberMeServicesInterface providing
  20. * remember-me capabilities without requiring a TokenProvider.
  21. *
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. *
  24. * @deprecated since Symfony 5.4, use {@see SignatureRememberMeHandler} instead
  25. */
  26. class TokenBasedRememberMeServices extends AbstractRememberMeServices
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function processAutoLoginCookie(array $cookieParts, Request $request)
  32. {
  33. if (4 !== \count($cookieParts)) {
  34. throw new AuthenticationException('The cookie is invalid.');
  35. }
  36. [$class, $userIdentifier, $expires, $hash] = $cookieParts;
  37. if (false === $userIdentifier = base64_decode($userIdentifier, true)) {
  38. throw new AuthenticationException('$userIdentifier contains a character from outside the base64 alphabet.');
  39. }
  40. try {
  41. $userProvider = $this->getUserProvider($class);
  42. // @deprecated since Symfony 5.3, change to $userProvider->loadUserByIdentifier() in 6.0
  43. if (method_exists($userProvider, 'loadUserByIdentifier')) {
  44. $user = $userProvider->loadUserByIdentifier($userIdentifier);
  45. } else {
  46. trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "loadUserByIdentifier()" in user provider "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.', get_debug_type($userProvider));
  47. $user = $userProvider->loadUserByUsername($userIdentifier);
  48. }
  49. } catch (\Exception $e) {
  50. if (!$e instanceof AuthenticationException) {
  51. $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
  52. }
  53. throw $e;
  54. }
  55. if (!$user instanceof UserInterface) {
  56. throw new \RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface, but returned "%s".', get_debug_type($user)));
  57. }
  58. if (true !== hash_equals($this->generateCookieHash($class, $userIdentifier, $expires, $user->getPassword()), $hash)) {
  59. throw new AuthenticationException('The cookie\'s hash is invalid.');
  60. }
  61. if ($expires < time()) {
  62. throw new AuthenticationException('The cookie has expired.');
  63. }
  64. return $user;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function onLoginSuccess(Request $request, Response $response, TokenInterface $token)
  70. {
  71. $user = $token->getUser();
  72. $expires = time() + $this->options['lifetime'];
  73. // @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
  74. $value = $this->generateCookieValue(\get_class($user), method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), $expires, $user->getPassword());
  75. $response->headers->setCookie(
  76. new Cookie(
  77. $this->options['name'],
  78. $value,
  79. $expires,
  80. $this->options['path'],
  81. $this->options['domain'],
  82. $this->options['secure'] ?? $request->isSecure(),
  83. $this->options['httponly'],
  84. false,
  85. $this->options['samesite']
  86. )
  87. );
  88. }
  89. /**
  90. * Generates the cookie value.
  91. *
  92. * @param int $expires The Unix timestamp when the cookie expires
  93. * @param string|null $password The encoded password
  94. *
  95. * @return string
  96. */
  97. protected function generateCookieValue(string $class, string $userIdentifier, int $expires, ?string $password)
  98. {
  99. // $userIdentifier is encoded because it might contain COOKIE_DELIMITER,
  100. // we assume other values don't
  101. return $this->encodeCookie([
  102. $class,
  103. base64_encode($userIdentifier),
  104. $expires,
  105. $this->generateCookieHash($class, $userIdentifier, $expires, $password),
  106. ]);
  107. }
  108. /**
  109. * Generates a hash for the cookie to ensure it is not being tampered with.
  110. *
  111. * @param int $expires The Unix timestamp when the cookie expires
  112. * @param string|null $password The encoded password
  113. *
  114. * @return string
  115. */
  116. protected function generateCookieHash(string $class, string $userIdentifier, int $expires, ?string $password)
  117. {
  118. return hash_hmac('sha256', $class.self::COOKIE_DELIMITER.$userIdentifier.self::COOKIE_DELIMITER.$expires.self::COOKIE_DELIMITER.$password, $this->getSecret());
  119. }
  120. }