Created
February 21, 2014 03:15
-
-
Save luishdez/9128125 to your computer and use it in GitHub Desktop.
Manually remember me for Symfony2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserProvider | |
{ | |
/** ... */ | |
protected function enableRememberMe($username) | |
{ | |
$rememberMeValue = $this->generateRememberMeCookie($username); | |
$this->session->set('REMEMBER_ME', $rememberMeValue); | |
} | |
protected function generateRememberMeCookie($username) | |
{ | |
$key = 'ThisTokenIsNotSoSecretChangeIt'; | |
$class = 'MyCompany\Bundle\UserBundle\Entity\User'; | |
$password = 'none'; | |
$expires = time()+2592000; | |
$hash = hash('sha256', $class.$username.$expires.$password.$key); | |
return $this->encodeCookie( | |
array( | |
$class, | |
base64_encode($username), | |
$expires, | |
$hash | |
) | |
); | |
} | |
/** | |
* Encodes the cookie parts | |
* | |
* @param array $cookieParts | |
* | |
* @return string | |
*/ | |
protected function encodeCookie(array $cookieParts) | |
{ | |
return base64_encode(implode(':', $cookieParts)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment