From 6ae3d11a83db57d01377b7e1f47936d2913fe993 Mon Sep 17 00:00:00 2001 From: Matthew Ross Date: Sun, 15 May 2016 20:53:21 -0400 Subject: [PATCH] Add initial Auth class and update User --- src/api/controllers/Auth.php | 58 ++++++++++++++++++++++++++++++++++++ src/api/index.php | 10 +++---- src/api/models/User.php | 3 ++ test/api/Mocks.php | 1 + test/api/models/UserTest.php | 3 ++ 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/api/controllers/Auth.php diff --git a/src/api/controllers/Auth.php b/src/api/controllers/Auth.php new file mode 100644 index 0000000..77cb8ed --- /dev/null +++ b/src/api/controllers/Auth.php @@ -0,0 +1,58 @@ +hasHeader('Authorization') { + $apiJson = new ApiJson(); + + return $response->withStatus(400); // Bad Request + } + + $jwt = $response->getHeader('Authorization'); + + // Validate token + // Issue new token with extended expiration + } + + public function login($request, $response, $args) { + $data = json_decode($request->getBody()); + $user = R::findOne('user', 'username = ?', [$data->username]); + + if ($user === null) { + $this->apiJson->addAlert('error', 'Invalid username or password.'); + + return $this->jsonResponse($response); + } + + if ($user->password !== $this->hashPassword($data->password, $user->salt) { + $this->apiJson->addAlert('error', 'Invalid username or password.'); + + return $this->jsonResponse($response); + } + + // Username and password verified + // Issue JWT + } + + public function logout($request, $response, $args) { + } + + private function getJwtKey() { + $key = R::load('jwt', 1); + + if ($key->id === 0) { + $key->token = password_hash(strval(time()), PASSWORD_BCRYPT); + R::store($key); + } + + return $key->token; + } + + private function hashPassword($password, $salt) { + return password_hash($data->password, PASSWORD_BCRYPT, + array('salt' => $salt)); + } +} + diff --git a/src/api/index.php b/src/api/index.php index 0a85cc6..44d1f71 100644 --- a/src/api/index.php +++ b/src/api/index.php @@ -44,11 +44,11 @@ $app->get ('/users/{id}', 'Users:getUser'); $app->post ('/users', 'Users:addUser'); $app->post ('/users/{id}', 'Users:updateUser'); $app->delete('/users/{id}', 'Users:removeUser'); -/* -$app->post('/authenticate', 'Users:authenticate'); -$app->post('/login', 'Users:login'); -$app->post('/logout', 'Users:logout'); -*/ + +$app->post('/authenticate', 'Auth:authenticate'); +$app->post('/login', 'Auth:login'); +$app->post('/logout', 'Auth:logout'); + $app->run(); R::close(); diff --git a/src/api/models/User.php b/src/api/models/User.php index f504391..1238771 100644 --- a/src/api/models/User.php +++ b/src/api/models/User.php @@ -16,6 +16,7 @@ class User extends BaseModel { public $email = ''; public $default_board_id = 0; public $user_option_id = 0; + public $last_login = 0; public function __construct($container, $id = 0) { parent::__construct('user', $id, $container); @@ -36,6 +37,7 @@ class User extends BaseModel { $bean->email = $this->email; $bean->default_board_id = $this->default_board_id; $bean->user_option_id = $this->user_option_id; + $bean->last_login = $this->last_login; } public function loadFromBean($bean) { @@ -76,6 +78,7 @@ class User extends BaseModel { $this->email = $obj->email; $this->default_board_id = (int) $obj->default_board_id; $this->user_option_id = (int) $obj->user_option_id; + $this->last_login = (int) $obj->last_login; } catch (Exception $ex) { $this->is_valid = false; } diff --git a/test/api/Mocks.php b/test/api/Mocks.php index ae01ef6..8a58e4e 100644 --- a/test/api/Mocks.php +++ b/test/api/Mocks.php @@ -63,6 +63,7 @@ class DataMock { $user->email = 'user@example.com'; $user->default_board_id = 1; $user->user_option_id = 1; + $user->last_login = 123456789; return $user; } diff --git a/test/api/models/UserTest.php b/test/api/models/UserTest.php index f178c76..af1555c 100644 --- a/test/api/models/UserTest.php +++ b/test/api/models/UserTest.php @@ -66,6 +66,7 @@ class UserTest extends PHPUnit_Framework_TestCase { $this->assertTrue($bean->password_hash === $user->password_hash); $this->assertTrue($bean->email === $user->email); $this->assertTrue($bean->default_board_id === $user->default_board_id); + $this->assertTrue($bean->last_login === $user->last_login); } private function assertDefaultProperties($user) { @@ -77,6 +78,7 @@ class UserTest extends PHPUnit_Framework_TestCase { $this->assertTrue($user->password_hash === ''); $this->assertTrue($user->email === ''); $this->assertTrue($user->default_board_id === 0); + $this->assertTrue($user->last_login === 0); } private function assertMockProperties($user) { @@ -88,6 +90,7 @@ class UserTest extends PHPUnit_Framework_TestCase { $this->assertTrue($user->password_hash === 'hashpass1234'); $this->assertTrue($user->email === 'user@example.com'); $this->assertTrue($user->default_board_id === 1); + $this->assertTrue($user->last_login === 123456789); } }