From b6537da8b670d14f2b9c559654ee09bd5ac51b81 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 9 Oct 2011 00:27:39 -0400 Subject: [PATCH 1/4] Added first draft of remember me to auth. --- laravel/security/auth.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 8e6f2967..6f2d7e5e 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -52,6 +52,15 @@ public static function user() $id = IoC::container()->core('session')->get(Auth::user_key); + if (is_null($id) AND ! is_null($cookie = strrev(Crypter::decrypt(\Cookie::get('remember'))))) + { + $cookie = explode('|', $cookie); + if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT'))) + { + $id = $cookie[0]; + } + } + return static::$user = call_user_func(Config::get('auth.user'), $id); } @@ -65,12 +74,14 @@ public static function user() * @param string $password * @return bool */ - public static function attempt($username, $password = null) + public static function attempt($username, $password = null, $remember = false) { if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password))) { static::login($user); + if ($remember) static::remember($user); + return true; } @@ -108,4 +119,15 @@ public static function logout() IoC::container()->core('session')->forget(Auth::user_key); } + /** + * Set a cookie so that users are remembered. + * + * @return bool + */ + public static function remember($user) + { + static::$user = $user; + $cookie = Crypter::encrypt(strrev($user->id.'|'.\Request::ip().'|'.md5(\Request::server('HTTP_USER_AGENT')).'|'.time())); + \Cookie::put('remember', $cookie, 60); + } } \ No newline at end of file From 3d2aa29d4432272ce7d1f3ab0dc2589e2a8f30cc Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 9 Oct 2011 11:57:00 -0400 Subject: [PATCH 2/4] Refactored auth remember me --- laravel/security/auth.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 6f2d7e5e..608c5b12 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -52,13 +52,19 @@ public static function user() $id = IoC::container()->core('session')->get(Auth::user_key); - if (is_null($id) AND ! is_null($cookie = strrev(Crypter::decrypt(\Cookie::get('remember'))))) + if (is_null($id) AND ! is_null($cookie = Crypter::decrypt(\Cookie::get('remember')))) { $cookie = explode('|', $cookie); if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT'))) { $id = $cookie[0]; } + + if ( ! is_null(static::$user = call_user_func(Config::get('auth.user'), $id))) + { + static::login($user); + return static::$user; + } } return static::$user = call_user_func(Config::get('auth.user'), $id); @@ -72,9 +78,11 @@ public static function user() * * @param string $username * @param string $password + * @param bool $remember + * @param int $ttl - Default is one week. * @return bool */ - public static function attempt($username, $password = null, $remember = false) + public static function attempt($username, $password = null, $remember = false, $ttl = 10080) { if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password))) { @@ -122,12 +130,14 @@ public static function logout() /** * Set a cookie so that users are remembered. * + * @param object $user + * @param int $ttl - Default is one week. * @return bool */ - public static function remember($user) + public static function remember($user, $ttl = 10080) { static::$user = $user; - $cookie = Crypter::encrypt(strrev($user->id.'|'.\Request::ip().'|'.md5(\Request::server('HTTP_USER_AGENT')).'|'.time())); - \Cookie::put('remember', $cookie, 60); + $cookie = Crypter::encrypt($user->id.'|'.\Request::ip().'|'.md5(\Request::server('HTTP_USER_AGENT')).'|'.time()); + \Cookie::put('remember', $cookie, $ttl); } } \ No newline at end of file From 560a4cc1c201a0f3db795caff5e7258ea9ad2cd2 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 9 Oct 2011 12:05:12 -0400 Subject: [PATCH 3/4] Added comments and changed the $cookie var to use implode. --- laravel/security/auth.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 608c5b12..98a12dda 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -60,7 +60,8 @@ public static function user() $id = $cookie[0]; } - if ( ! is_null(static::$user = call_user_func(Config::get('auth.user'), $id))) + // Attempt to find the user and set the login session. + if ( ! is_null($user = call_user_func(Config::get('auth.user'), $id))) { static::login($user); return static::$user; @@ -137,7 +138,7 @@ public static function logout() public static function remember($user, $ttl = 10080) { static::$user = $user; - $cookie = Crypter::encrypt($user->id.'|'.\Request::ip().'|'.md5(\Request::server('HTTP_USER_AGENT')).'|'.time()); + $cookie = Crypter::encrypt(implode('|', array($user->id, \Request::ip(), md5(\Request::server('HTTP_USER_AGENT')), time())); \Cookie::put('remember', $cookie, $ttl); } } \ No newline at end of file From 6e0d69e23a7e4ffba3fb868dcbb697d38399684f Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 9 Oct 2011 12:49:41 -0400 Subject: [PATCH 4/4] Refactered user and fixed parse error. --- laravel/security/auth.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 98a12dda..69122dab 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -52,23 +52,19 @@ public static function user() $id = IoC::container()->core('session')->get(Auth::user_key); - if (is_null($id) AND ! is_null($cookie = Crypter::decrypt(\Cookie::get('remember')))) + static::$user = call_user_func(Config::get('auth.user'), $id); + + if (is_null(static::$user) AND ! is_null($cookie = Crypter::decrypt(\Cookie::get('remember')))) { $cookie = explode('|', $cookie); - if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT'))) + if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT')) + AND ! is_null(static::$user = call_user_func(Config::get('auth.user'), $cookie[0]))) { - $id = $cookie[0]; - } - - // Attempt to find the user and set the login session. - if ( ! is_null($user = call_user_func(Config::get('auth.user'), $id))) - { - static::login($user); - return static::$user; + static::login(static::$user); } } - return static::$user = call_user_func(Config::get('auth.user'), $id); + return static::$user; } /** @@ -138,7 +134,7 @@ public static function logout() public static function remember($user, $ttl = 10080) { static::$user = $user; - $cookie = Crypter::encrypt(implode('|', array($user->id, \Request::ip(), md5(\Request::server('HTTP_USER_AGENT')), time())); + $cookie = Crypter::encrypt(implode('|', array($user->id, \Request::ip(), md5(\Request::server('HTTP_USER_AGENT')), time()))); \Cookie::put('remember', $cookie, $ttl); } } \ No newline at end of file