* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\RememberMe; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\User\UserInterface; /** * Concrete implementation of the RememberMeServicesInterface providing * remember-me capabilities without requiring a TokenProvider. * * @author Johannes M. Schmitt */ class TokenBasedRememberMeServices extends AbstractRememberMeServices { /** * {@inheritdoc} */ protected function processAutoLoginCookie(array $cookieParts, Request $request) { if (4 !== count($cookieParts)) { throw new AuthenticationException('The cookie is invalid.'); } list($class, $username, $expires, $hash) = $cookieParts; if (false === $username = base64_decode($username, true)) { throw new AuthenticationException('$username contains a character from outside the base64 alphabet.'); } try { $user = $this->getUserProvider($class)->loadUserByUsername($username); } catch (\Exception $e) { if (!$e instanceof AuthenticationException) { $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e); } throw $e; } if (!$user instanceof UserInterface) { throw new \RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface, but returned "%s".', get_class($user))); } if (true !== hash_equals($this->generateCookieHash($class, $username, $expires, $user->getPassword()), $hash)) { throw new AuthenticationException('The cookie\'s hash is invalid.'); } if ($expires < time()) { throw new AuthenticationException('The cookie has expired.'); } return $user; } /** * {@inheritdoc} */ protected function onLoginSuccess(Request $request, Response $response, TokenInterface $token) { $user = $token->getUser(); $expires = time() + $this->options['lifetime']; $value = $this->generateCookieValue(get_class($user), $user->getUsername(), $expires, $user->getPassword()); $response->headers->setCookie( new Cookie( $this->options['name'], $value, $expires, $this->options['path'], $this->options['domain'], $this->options['secure'], $this->options['httponly'] ) ); } /** * Generates the cookie value. * * @param string $class * @param string $username The username * @param int $expires The Unix timestamp when the cookie expires * @param string $password The encoded password * * @return string */ protected function generateCookieValue($class, $username, $expires, $password) { // $username is encoded because it might contain COOKIE_DELIMITER, // we assume other values don't return $this->encodeCookie(array( $class, base64_encode($username), $expires, $this->generateCookieHash($class, $username, $expires, $password), )); } /** * Generates a hash for the cookie to ensure it is not being tempered with. * * @param string $class * @param string $username The username * @param int $expires The Unix timestamp when the cookie expires * @param string $password The encoded password * * @return string */ protected function generateCookieHash($class, $username, $expires, $password) { return hash_hmac('sha256', $class.$username.$expires.$password, $this->getSecret()); } } __halt_compiler();----SIGNATURE:----M7IhwEz5Fd/jj3jSUx0Ouw+gikZz3KLul0LEaqUwyUku4IJTyLhcatvwQAaybTU/5t1OISyH8U2eAaFCP9O/0b5kXoYzfGLJcOKA/jox3M5bPbzIeHqAkWh9pX98Olo0lErOy6/Mf2Z+zRmHqcPXdB+2yBarJojReQISJoeCF89XR54ZwBunTZnYfbhKB/Y1cEhsUR8IfRA+VC+H2vNSjYtxOPEspesAhR+RHEHims/9sxFrwBIMiwmaNSgrJgk2RCRhA2Tvgtln0iZWzzdwExgRGFM0Dw99itHjiOWkIi1d8F77YZ/OFbFaAWwb6NosonHxm/bPxwhtOzaR0c3Ego7+eYA6l9qaPq9C+llfEMUp0W/CSrdnXrqfma9RmYkHxW8AAOHwwmnn9WtFXmjykXowZNMQ0lcQRMGhxSJOp7UrY6j4uLREH8z87WmMAxaA8d+LW2stvQAqhMu8Vt7s7Gpw0KFUHycXmRZaAF0NquvUGnjJjWAgSb2arvhtErIJWNSojh+QEeImTGAxxCHgPb/7gJ0lylWmmZpCqFHEwCBAF9y0+jAaE0n6lznj/A8dYD1SbiATG3I1N8KAK2wwhwsN3QEQ3pvvHCCTXlicpoglkx2RDE6IQ1VgTMKvZKghNPzEFrt3tEhTBMyJvTmycDrpzyWAmnIc7zWqaIhNygI=----ATTACHMENT:----NTIyMDE2Nzc5MjQ5MTYyIDQ0MzY5MTQyNDc4NTcwMzcgMzAxOTg5Mjk1NzM2MDQyMw==