PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS
PHP | 126 lines | 70 code | 14 blank | 42 comment | 6 complexity | f8bcc527b4c4af46329a904c3e63db9b MD5 | raw file
  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. use Symfony\Component\Security\Core\Util\StringUtils;
  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. class TokenBasedRememberMeServices extends AbstractRememberMeServices
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function processAutoLoginCookie(array $cookieParts, Request $request)
  30. {
  31. if (count($cookieParts) !== 4) {
  32. throw new AuthenticationException('The cookie is invalid.');
  33. }
  34. list($class, $username, $expires, $hash) = $cookieParts;
  35. if (false === $username = base64_decode($username, true)) {
  36. throw new AuthenticationException('$username contains a character from outside the base64 alphabet.');
  37. }
  38. try {
  39. $user = $this->getUserProvider($class)->loadUserByUsername($username);
  40. } catch (\Exception $e) {
  41. if (!$e instanceof AuthenticationException) {
  42. $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
  43. }
  44. throw $e;
  45. }
  46. if (!$user instanceof UserInterface) {
  47. throw new \RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface, but returned "%s".', get_class($user)));
  48. }
  49. if (true !== StringUtils::equals($this->generateCookieHash($class, $username, $expires, $user->getPassword()), $hash)) {
  50. throw new AuthenticationException('The cookie\'s hash is invalid.');
  51. }
  52. if ($expires < time()) {
  53. throw new AuthenticationException('The cookie has expired.');
  54. }
  55. return $user;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function onLoginSuccess(Request $request, Response $response, TokenInterface $token)
  61. {
  62. $user = $token->getUser();
  63. $expires = time() + $this->options['lifetime'];
  64. $value = $this->generateCookieValue(get_class($user), $user->getUsername(), $expires, $user->getPassword());
  65. $response->headers->setCookie(
  66. new Cookie(
  67. $this->options['name'],
  68. $value,
  69. $expires,
  70. $this->options['path'],
  71. $this->options['domain'],
  72. $this->options['secure'],
  73. $this->options['httponly']
  74. )
  75. );
  76. }
  77. /**
  78. * Generates the cookie value.
  79. *
  80. * @param string $class
  81. * @param string $username The username
  82. * @param int $expires The Unix timestamp when the cookie expires
  83. * @param string $password The encoded password
  84. *
  85. * @return string
  86. */
  87. protected function generateCookieValue($class, $username, $expires, $password)
  88. {
  89. // $username is encoded because it might contain COOKIE_DELIMITER,
  90. // we assume other values don't
  91. return $this->encodeCookie(array(
  92. $class,
  93. base64_encode($username),
  94. $expires,
  95. $this->generateCookieHash($class, $username, $expires, $password),
  96. ));
  97. }
  98. /**
  99. * Generates a hash for the cookie to ensure it is not being tempered with.
  100. *
  101. * @param string $class
  102. * @param string $username The username
  103. * @param int $expires The Unix timestamp when the cookie expires
  104. * @param string $password The encoded password
  105. *
  106. * @return string
  107. */
  108. protected function generateCookieHash($class, $username, $expires, $password)
  109. {
  110. return hash_hmac('sha256', $class.$username.$expires.$password, $this->getKey());
  111. }
  112. }