parent
8b9add004b
commit
3a5e329ba5
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
namespace Config;
|
||||||
|
use PDO;
|
||||||
|
use PDOStatement;
|
||||||
|
|
||||||
|
class Connection extends PDO{
|
||||||
|
private PDOStatement $stmt;
|
||||||
|
|
||||||
|
public function __construct(string $dsn, string $username, string $password)
|
||||||
|
{
|
||||||
|
parent::__construct($dsn, $username, $password);
|
||||||
|
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function executeQuery(string $query, array $parameters = []): bool
|
||||||
|
{
|
||||||
|
$this->stmt = parent::prepare($query);
|
||||||
|
foreach ($parameters as $name => $value) {
|
||||||
|
$this->stmt->bindValue($name, $value[0], $value[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->stmt->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getResults(): array
|
||||||
|
{
|
||||||
|
return $this->stmt->fetchAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
namespace Config;
|
||||||
|
use PDOException;
|
||||||
|
require_once __DIR__ ."/connection.php";
|
||||||
|
|
||||||
|
class DatabaseCon{
|
||||||
|
private string $dsn;
|
||||||
|
private string $login;
|
||||||
|
private string $password;
|
||||||
|
|
||||||
|
function __construct(){
|
||||||
|
if (getenv("SMDB_HOST") == null || getenv("SMDB_DATABASE") == null || getenv("SMDB_USER") == null || getenv("SMDB_PASSWORD") == null){
|
||||||
|
throw new PDOException("ENV variables not found");
|
||||||
|
}
|
||||||
|
$this->dsn = "mysql:host=".getenv("SMDB_HOST").";dbname=".getenv("SMDB_DATABASE").";charset=UTF8";
|
||||||
|
$this->login = getenv("SMDB_USER");
|
||||||
|
$this->password = getenv("SMDB_PASSWORD");
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect(): int|Connection {
|
||||||
|
try {
|
||||||
|
$connection = new Connection($this->dsn,$this->login,$this->password);
|
||||||
|
} catch (PDOException $e){
|
||||||
|
throw new PDOException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
namespace Gateway;
|
||||||
|
use Config\DatabaseCon;
|
||||||
|
use Config\Connection;
|
||||||
|
use PDOException;
|
||||||
|
use PDO;
|
||||||
|
use Config\Token;
|
||||||
|
|
||||||
|
class UserGateway {
|
||||||
|
private Connection $con;
|
||||||
|
private Token $token;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->token = new Token;
|
||||||
|
try {
|
||||||
|
$this->con = (new DatabaseCon)->connect();
|
||||||
|
} catch(PDOException $e) {
|
||||||
|
throw new PDOException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createUser(string $mail, string $hash, string $username) {
|
||||||
|
$query = "INSERT INTO user VALUES(UUID(), :mail, :hash, :username, CURDATE());";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':mail' => array($mail, PDO::PARAM_STR),
|
||||||
|
':hash' => array($hash, PDO::PARAM_STR),
|
||||||
|
':username' => array($username, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
|
||||||
|
$query = "SELECT id FROM user WHERE email=:mail;";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':mail' => array($mail, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
$results = $this->con->getResults();
|
||||||
|
|
||||||
|
return $this->token->getNewJsonToken($results[0]['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete User: (1:OK, 2:Unauthorize, 3:No User)
|
||||||
|
public function deleteUser(string $uuid) : int {
|
||||||
|
$query = "DELETE FROM user WHERE id=:uuid;";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':uuid' => array($uuid, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login User (get token)
|
||||||
|
public function login(string $mail, string $hash) {
|
||||||
|
$query = "SELECT hash, id FROM user WHERE email=:mail;";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':mail' => array($mail, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
$results = $this->con->getResults();
|
||||||
|
|
||||||
|
if(empty($results)) {
|
||||||
|
// Not Found
|
||||||
|
return 404;
|
||||||
|
}
|
||||||
|
if($hash !== (string) $results[0]['hash']) {
|
||||||
|
// Unauthorized
|
||||||
|
return 401;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_encode($this->token->getNewJsonToken($results[0]['id']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateMail(string $uuid, string $new_mail) {
|
||||||
|
$query = "UPDATE user SET email=:new_mail WHERE id=:uuid;";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':new_mail' => array($new_mail, PDO::PARAM_STR),
|
||||||
|
':uuid' => array($uuid, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateUsername(string $uuid, string $new_username) {
|
||||||
|
$query = "UPDATE user SET username=:new_username WHERE id=:uuid;";
|
||||||
|
$this->con->executeQuery($query, array(
|
||||||
|
':new_username' => array($new_username, PDO::PARAM_STR),
|
||||||
|
':uuid' => array($uuid, PDO::PARAM_STR)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
namespace Config;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Firebase\JWT\JWT;
|
||||||
|
use Firebase\JWT\Key;
|
||||||
|
|
||||||
|
class Token {
|
||||||
|
private string $key = 'passwd';
|
||||||
|
// Need to be in a config file
|
||||||
|
private string $path_to_key="../sym_keyfile.key";
|
||||||
|
|
||||||
|
function __construct()
|
||||||
|
{
|
||||||
|
#$file = fopen($this->path_to_key, 'r');
|
||||||
|
#$this->key = fread($file, filesize($this->path_to_key));
|
||||||
|
#fclose($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return json containing JWT with uuid and exp
|
||||||
|
function getNewJsonToken(string $uuid) :array {
|
||||||
|
$payload = [
|
||||||
|
'uuid' => $uuid,
|
||||||
|
'exp' => strtotime("+2month", time())
|
||||||
|
];
|
||||||
|
|
||||||
|
return ["token" => JWT::encode($payload, $this->key, 'HS256')];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the JWT authenticity
|
||||||
|
function verifyToken(string $jwt) :bool {
|
||||||
|
try {
|
||||||
|
JWT::decode($jwt, new Key($this->key, 'HS256'));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get uuid from JWT
|
||||||
|
// Missing error handling on bad JWT
|
||||||
|
function getUuidFromToken(string $jwt) :string {
|
||||||
|
$decoded = (array) JWT::decode($jwt, new Key($this->key, 'HS256'));
|
||||||
|
return $decoded['uuid'];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue