Skip to content

Instantly share code, notes, and snippets.

@luishdez
Created February 21, 2014 03:15

Revisions

  1. luishdez created this gist Feb 21, 2014.
    41 changes: 41 additions & 0 deletions UserProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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));
    }
    }