add: api.module.authentication (authentication methods for admin + user) | upd: config.modules (updated config according to @1)

This commit is contained in:
xdrm-brackets 2017-11-26 13:49:13 +01:00
parent cf35a8ade4
commit 88f1d3b871
2 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,104 @@
<?php
namespace api\module;
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
use \api\core\AuthSystemDefault;
class authentication{
public function __construct(){}
public function __destruct(){}
public function POST_admin($argv){
extract($argv);
/* (1) Logout by default
---------------------------------------------------------*/
$_SESSION['TOKEN'] = [];
/* (2) Search for @id_admin from username
---------------------------------------------------------*/
/* (1) Fetch by username */
$fetched_admin = Repo::request('admin', 'getByUsername', $username);
/* (2) If not found -> error */
if( !is_array($fetched_admin) || !isset($fetched_admin['id_admin']) || !is_numeric($fetched_admin['id_admin']) )
return ['connected' => false];
/* (3) Extract @id_admin */
$id_admin = intval( $fetched_admin['id_admin'] );
/* (3) Check password for admin
---------------------------------------------------------*/
/* (1) Check password */
$valid_pass = Repo::request('admin', 'checkPassword', $id_admin, $password);
/* (2) If wrong password -> error */
if( !$valid_pass )
return ['connected' => false];
/* (4) Update session to be connected
---------------------------------------------------------*/
/* (1) Update session */
$_SESSION['TOKEN'] = 'a'.$fetched_admin['token'];
new AuthSystemDefault;
/* (2) Return status */
return ['connected' => true];
}
public function POST_user($argv){
extract($argv);
/* (1) Logout by default
---------------------------------------------------------*/
$_SESSION['TOKEN'] = [];
/* (2) Search for @id_user from username
---------------------------------------------------------*/
/* (1) Fetch by username */
$fetched_user = Repo::request('user', 'getByUsername', $username);
/* (2) If not found -> error */
if( !is_array($fetched_user) || !isset($fetched_user['id_user']) || !is_numeric($fetched_user['id_user']) )
return ['connected' => false];
/* (3) Extract @id_user */
$id_user = intval( $fetched_user['id_user'] );
/* (3) Check password for user
---------------------------------------------------------*/
/* (1) Check password */
$valid_pass = Repo::request('user', 'checkPassword', $id_user, $password);
/* (2) If wrong password -> error */
if( !$valid_pass )
return ['connected' => false];
/* (4) Update session to be connected
---------------------------------------------------------*/
/* (1) Update session */
$_SESSION['TOKEN'] = 'u'.$fetched_user['token'];
new AuthSystemDefault;
/* (2) Return status */
return ['connected' => true];
}
}

View File

@ -1,5 +1,34 @@
{ {
"authentication": {
"POST admin": {
"description": "Connexion administrateur",
"permissions": [],
"parameters": {
"username": { "description": "Identifiant de l'administrateur.", "type": "varchar(3,20,alphanumeric)" },
"password": { "description": "Mot de passe de l'administrateur.", "type": "text" }
},
"output": {
"connected": { "description": "Vrai si connecté.", "type": "boolean" }
}
},
"POST user": {
"description": "Connexion utilisateur",
"permissions": [],
"parameters": {
"username": { "description": "Identifiant de l'utilisateur.", "type": "varchar(3,20,alphanumeric)" },
"password": { "description": "Mot de passe de l'utilisateur'.", "type": "text" }
},
"output": {
"connected": { "description": "Vrai si connecté.", "type": "boolean" }
}
}
},
"RESTexample": { "RESTexample": {
"POST article": { "POST article": {
"description": "Posts a new article", "description": "Posts a new article",