Add initial Auth class and update User

This commit is contained in:
Matthew Ross 2016-05-15 20:53:21 -04:00
parent 40bc11a45c
commit 6ae3d11a83
5 changed files with 70 additions and 5 deletions

View File

@ -0,0 +1,58 @@
<?php
use RedBeanPHP\R;
use Firebase\JWT;
class Auth extends BaseController {
public function authenticate($request, $response, $args) {
if (!$request->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));
}
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}
}