From 533e34c3955a35abdafce5de5c7ff6c97a6cf082 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Wed, 13 Jul 2016 20:20:22 -0300 Subject: [PATCH 1/7] (Guillermo) Add-email-sender-class --- server/controllers/user/login.php | 28 ++++++++++++++++++++++++++-- server/models/SessionCookie.php | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 server/models/SessionCookie.php diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 54357d80..90bf90a1 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -5,6 +5,7 @@ class LoginController extends Controller { private $userInstance; private $session; + private $remembertoken; public function validations() { return [ @@ -19,8 +20,9 @@ class LoginController extends Controller { return; } - if ($this->areCredentialsValid()) { + if ($this->areCredentialsValid() || $this->isTokenValid()) { $this->createUserSession(); + $this->createSessionCookie(); Response::respondSuccess($this->getUserData()); } else { @@ -46,7 +48,8 @@ class LoginController extends Controller { return array( 'userId' => $userInstance->id, 'userEmail' => $userInstance->email, - 'token' => $this->getSession()->getToken() + 'token' => $this->getSession()->getToken(), + 'rememberToken' => $this->remembertoken ); } @@ -68,4 +71,25 @@ class LoginController extends Controller { return $this->session; } + private function isTokenValid(){ + $sessioncookie = SessionCookie::getDataStore(Controller::request('rememberToken'),'token'); + if($sessioncookie !== null){ + $this->userInstance = $sessioncookie->user; + return true; + } + } + private function createSessionCookie(){ + $remember = Controller::request('remember'); + if($remember){ + $this->remembertoken = md5(uniqid(rand())); + + $sessioncookie = new SessionCookie(); + + $sessioncookie->setProperties(array( + 'user' => $this->userInstance->getBeanInstance(), + 'token' => $this->remembertoken + )); + $sessioncookie->store(); + } + } } diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php new file mode 100644 index 00000000..5817f659 --- /dev/null +++ b/server/models/SessionCookie.php @@ -0,0 +1,20 @@ + Date: Thu, 14 Jul 2016 03:34:13 -0300 Subject: [PATCH 2/7] (Guillermo) Add-email-sender-class --- server/controllers/user/login.php | 33 +++++++++++++++++-------------- server/libs/Hashing.php | 3 +++ server/models/Session.php | 2 +- server/models/SessionCookie.php | 4 +--- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 90bf90a1..30ae03f6 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -5,7 +5,7 @@ class LoginController extends Controller { private $userInstance; private $session; - private $remembertoken; + private $rememberToken; public function validations() { return [ @@ -38,6 +38,15 @@ class LoginController extends Controller { return ($this->getUserByInputCredentials() !== null); } + private function isTokenValid() { + $sessionCookie = SessionCookie::getDataStore(Controller::request('rememberToken'),'token'); + $userid = Controller::request('userId'); + if ($sessionCookie !== null && $userid === $sessionCookie->user->id) { + $this->userInstance = $sessionCookie->user; + return true; + } + } + private function createUserSession() { $this->getSession()->createSession($this->userInstance->id); } @@ -49,7 +58,7 @@ class LoginController extends Controller { 'userId' => $userInstance->id, 'userEmail' => $userInstance->email, 'token' => $this->getSession()->getToken(), - 'rememberToken' => $this->remembertoken + 'rememberToken' => $this->rememberToken ); } @@ -71,25 +80,19 @@ class LoginController extends Controller { return $this->session; } - private function isTokenValid(){ - $sessioncookie = SessionCookie::getDataStore(Controller::request('rememberToken'),'token'); - if($sessioncookie !== null){ - $this->userInstance = $sessioncookie->user; - return true; - } - } private function createSessionCookie(){ $remember = Controller::request('remember'); if($remember){ - $this->remembertoken = md5(uniqid(rand())); + $this->rememberToken = Hashing::generateRandomToken(); + $sessionCookie = new SessionCookie(); - $sessioncookie = new SessionCookie(); - - $sessioncookie->setProperties(array( + $sessionCookie->setProperties(array( 'user' => $this->userInstance->getBeanInstance(), - 'token' => $this->remembertoken + 'token' => $this->rememberToken, + 'ip' => $_SERVER[‘REMOTE_ADDR’], + 'creationDate' => date("d-m-Y (H:i:s)" )); - $sessioncookie->store(); + $sessionCookie->store(); } } } diff --git a/server/libs/Hashing.php b/server/libs/Hashing.php index 01ef7ff4..7b296357 100644 --- a/server/libs/Hashing.php +++ b/server/libs/Hashing.php @@ -7,4 +7,7 @@ class Hashing { public static function verifyPassword($password, $hash) { return password_verify($password, $hash); } + public static function generateRandomToken() { + return md5(uniqid(rand())); + } } \ No newline at end of file diff --git a/server/models/Session.php b/server/models/Session.php index 3c9d4f5e..3d543607 100644 --- a/server/models/Session.php +++ b/server/models/Session.php @@ -65,6 +65,6 @@ class Session { } private function generateToken() { - return md5(uniqid(rand())); + return Hashing::generateRandomToken();; } } \ No newline at end of file diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php index 5817f659..496027bf 100644 --- a/server/models/SessionCookie.php +++ b/server/models/SessionCookie.php @@ -1,6 +1,6 @@ Date: Fri, 15 Jul 2016 17:12:06 -0300 Subject: [PATCH 3/7] (Guillermo) code review changes --- server/controllers/user/login.php | 4 ++-- server/models/SessionCookie.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 30ae03f6..e69292de 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -89,8 +89,8 @@ class LoginController extends Controller { $sessionCookie->setProperties(array( 'user' => $this->userInstance->getBeanInstance(), 'token' => $this->rememberToken, - 'ip' => $_SERVER[‘REMOTE_ADDR’], - 'creationDate' => date("d-m-Y (H:i:s)" + 'ip' => $_SERVER['REMOTE_ADDR'], + 'creationDate' => date('d-m-Y (H:i:s)') )); $sessionCookie->store(); } diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php index 496027bf..bd33c8e4 100644 --- a/server/models/SessionCookie.php +++ b/server/models/SessionCookie.php @@ -1,6 +1,6 @@ Date: Fri, 15 Jul 2016 22:16:22 -0300 Subject: [PATCH 4/7] [AntonyAntonio] - Add remember me test [skip ci] --- server/controllers/user/login.php | 17 ++++++++----- server/models/SessionCookie.php | 5 ++-- tests/init.rb | 3 ++- tests/user/login.rb | 41 ++++++++++++++++++++++++------- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index e69292de..87800419 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -6,7 +6,7 @@ class LoginController extends Controller { private $userInstance; private $session; private $rememberToken; - + public function validations() { return [ 'permission' => 'any', @@ -39,11 +39,16 @@ class LoginController extends Controller { } private function isTokenValid() { - $sessionCookie = SessionCookie::getDataStore(Controller::request('rememberToken'),'token'); - $userid = Controller::request('userId'); - if ($sessionCookie !== null && $userid === $sessionCookie->user->id) { - $this->userInstance = $sessionCookie->user; - return true; + $rememberToken = Controller::request('rememberToken'); + + if ($rememberToken) { + $sessionCookie = SessionCookie::getDataStore($rememberToken, 'token'); + $userid = Controller::request('userId'); + + if ($sessionCookie !== null && $userid === $sessionCookie->user->id) { + $this->userInstance = $sessionCookie->user; + return true; + } } } diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php index bd33c8e4..e66adc5e 100644 --- a/server/models/SessionCookie.php +++ b/server/models/SessionCookie.php @@ -1,7 +1,7 @@ Date: Sat, 16 Jul 2016 03:54:29 -0300 Subject: [PATCH 5/7] [AntonyAntonio] - Add remember me test [skip ci] --- server/controllers/user/login.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 87800419..67bf5dd2 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -20,7 +20,7 @@ class LoginController extends Controller { return; } - if ($this->areCredentialsValid() || $this->isTokenValid()) { + if ($this->areCredentialsValid() || $this->isRememberTokenValid()) { $this->createUserSession(); $this->createSessionCookie(); @@ -38,7 +38,7 @@ class LoginController extends Controller { return ($this->getUserByInputCredentials() !== null); } - private function isTokenValid() { + private function isRememberTokenValid() { $rememberToken = Controller::request('rememberToken'); if ($rememberToken) { From 35a968652a3fe496b0e5a3ee930c2a7488b7ec55 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Sat, 16 Jul 2016 03:57:42 -0300 Subject: [PATCH 6/7] [AntonyAntonio] - Add remember me test [skip ci] --- server/controllers/user/login.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/user/login.php b/server/controllers/user/login.php index 67bf5dd2..5e1fb8a7 100644 --- a/server/controllers/user/login.php +++ b/server/controllers/user/login.php @@ -87,10 +87,10 @@ class LoginController extends Controller { } private function createSessionCookie(){ $remember = Controller::request('remember'); - if($remember){ + if ($remember) { $this->rememberToken = Hashing::generateRandomToken(); - $sessionCookie = new SessionCookie(); + $sessionCookie = new SessionCookie(); $sessionCookie->setProperties(array( 'user' => $this->userInstance->getBeanInstance(), 'token' => $this->rememberToken, From c613d886b3595950c49473c7ee5623876b067897 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 19 Jul 2016 10:51:43 -0300 Subject: [PATCH 7/7] Ivan - Fix master login test --- server/tests/__mocks__/HashingMock.php | 29 +++++++++++++++++++++ server/tests/controllers/user/loginTest.php | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 server/tests/__mocks__/HashingMock.php diff --git a/server/tests/__mocks__/HashingMock.php b/server/tests/__mocks__/HashingMock.php new file mode 100644 index 00000000..ef61e299 --- /dev/null +++ b/server/tests/__mocks__/HashingMock.php @@ -0,0 +1,29 @@ + parent::stub()->returns('HASHED_PASSword'), + 'verifyPassword' => parent::stub()->returns(true), + 'generateRandomToken' => parent::stub()->returns('TEST_TOKEN') + )); + } + + public static function mockInstanceFunction($functionName, $functionMock) { + self::getInstance()->{$functionName} = $functionMock; + } + + private static function getInstanceMock() { + return new \Mock(array( + 'initSession' => parent::stub(), + 'closeSession' => parent::stub(), + 'createSession' => parent::stub(), + 'getToken' => parent::stub()->returns('TEST_TOKEN'), + 'sessionExists' => parent::stub()->returns(false), + 'checkAuthentication' => parent::stub()->returns(true), + 'isLoggedWithId' => parent::stub()->returns(true), + )); + } +} \ No newline at end of file diff --git a/server/tests/controllers/user/loginTest.php b/server/tests/controllers/user/loginTest.php index 018efd72..7b80f38b 100644 --- a/server/tests/controllers/user/loginTest.php +++ b/server/tests/controllers/user/loginTest.php @@ -38,7 +38,8 @@ class LoginControllerTest extends PHPUnit_Framework_TestCase { $this->assertTrue(Response::get('respondSuccess')->hasBeenCalledWithArgs(array( 'userId' => 'MOCK_ID', 'userEmail' => 'MOCK_EMAIL', - 'token' => 'TEST_TOKEN' + 'token' => 'TEST_TOKEN', + 'rememberToken' => null ))); }