183 lines
4.3 KiB
PHP
183 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace api\core;
|
|
|
|
use \database\core\Repo;
|
|
use \error\core\Error;
|
|
|
|
|
|
|
|
|
|
class Authentification{
|
|
|
|
// Contiendra les erreurs
|
|
public $error;
|
|
|
|
|
|
/*************************/
|
|
/* SECURE SHA1 ALGORITHM */
|
|
/*************************/
|
|
public static function secure_hash($data, $depth=1){
|
|
/* [1] On hash @depth times
|
|
=========================================================*/
|
|
$hash = $data;
|
|
$c = 0;
|
|
|
|
for( $h = 0 ; $h < $depth ; $h++ ){
|
|
$hash = hash('sha256', '">\[..|{@#))'.hash('sha256', $hash.'_)Q@#((%*_$%(@#') );
|
|
$c++;
|
|
}
|
|
|
|
|
|
/* [2] Return result
|
|
=========================================================*/
|
|
return $hash;
|
|
}
|
|
|
|
|
|
|
|
|
|
/* INITIALISATION DU SYSTEME ET MISE A JOUR CONSTANTES D'AUTHENTIFICATION
|
|
*
|
|
*
|
|
*/
|
|
public static function check(){
|
|
/* (1) Initialisation des variables
|
|
---------------------------------------------------------*/
|
|
/* (1) Token de header */
|
|
if( !isset($GLOBALS['TOKEN']) )
|
|
$GLOBALS['TOKEN'] = null;
|
|
|
|
/* (1) Liste des permissions */
|
|
if( !isset($GLOBALS['PERM']) )
|
|
$GLOBALS['PERM'] = [];
|
|
|
|
|
|
/* (2) Gestion de AUTH (authentification) dans HEADER
|
|
---------------------------------------------------------*/
|
|
$GLOBALS['TOKEN'] = isset($_SERVER['PHP_AUTH_DIGEST']) ? $_SERVER['PHP_AUTH_DIGEST'] : '';
|
|
|
|
/* (3) Gestion de AUTH en fonction du token
|
|
---------------------------------------------------------*/
|
|
$GLOBALS['TOKEN'] = preg_match('/^[a-f0-9]{64}$/', $GLOBALS['TOKEN'], $match) ? $match[0] : null;
|
|
|
|
echo "regexp- ".$GLOBALS['TOKEN']."\n";
|
|
|
|
/* (4) On vérifie l'authentification par BDD
|
|
---------------------------------------------------------*/
|
|
if( !self::deepCheck() )
|
|
$GLOBALS['TOKEN'] = null;
|
|
}
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'AUTHENTIFICATION
|
|
*
|
|
*
|
|
*/
|
|
public static function deepCheck(){
|
|
/* [1] Si aucun token
|
|
=========================================================*/
|
|
if( is_null($GLOBALS['TOKEN']) )
|
|
return false;
|
|
|
|
echo 'a';
|
|
|
|
/* [2] Vérification du système
|
|
=========================================================*/
|
|
/* (1) Fetch cyclic-hashing-system -> check file */
|
|
$fn = __BUILD__.'/api/chs/hash';
|
|
|
|
if( !is_file($fn) )
|
|
return false;
|
|
echo 'b';
|
|
|
|
/* (2) Read file -> check content */
|
|
$fc = file_get_contents($fn);
|
|
|
|
if( strlen($fc) !== 64 )
|
|
return false;
|
|
echo 'c';
|
|
|
|
/* [3] Hash comparison
|
|
=========================================================*/
|
|
/* (1) Compares content */
|
|
$hashed = self::secure_hash($GLOBALS['TOKEN']);
|
|
|
|
if( $hashed !== $fc )
|
|
return false;
|
|
echo 'd';
|
|
|
|
/* (2) Stores new content */
|
|
file_put_contents($fn, $GLOBALS['TOKEN']);
|
|
|
|
/* (3) Stores permission */
|
|
if( !in_array('cyclic-hash', $GLOBALS['PERM']) )
|
|
$GLOBALS['PERM'][] = 'cyclic-hash';
|
|
|
|
|
|
/* [4] Returns true if no error
|
|
=========================================================*/
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DES ACCES EN FONCTION DE PERMISSIONS ATTENDUES
|
|
*
|
|
* @module<String> Module concerné
|
|
* @expected<array> Liste des permissions attendues
|
|
*
|
|
* @return status<Boolean> Si FALSE, pas la permission, sinon si
|
|
*
|
|
*/
|
|
public static function permission($module, $expected){
|
|
/* [1] Setup
|
|
=========================================================*/
|
|
/* (1) If no expected, return true */
|
|
if( !is_array($expected) || count($expected) === 0 )
|
|
return true;
|
|
|
|
/* (2) Mise à jour de l'authentification */
|
|
self::check();
|
|
|
|
|
|
var_dump('expected');
|
|
var_dump($expected);
|
|
var_dump('yours');
|
|
var_dump($GLOBALS['PERM']);
|
|
|
|
/* [2] Gestion des permissions
|
|
=========================================================*/
|
|
/* (1) Vérification de toutes les permissions requises */
|
|
foreach($expected as $permission)
|
|
// Si il manque au minimum une permission, on retourne FALSE
|
|
if( !in_array($permission, $GLOBALS['PERM']) )
|
|
return Error::PermissionError;
|
|
|
|
|
|
/* [3] Si on a toutes les permissions requises
|
|
=========================================================*/
|
|
return Error::Success;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LE NIVEAU D'AUTHENTIFICATION
|
|
*
|
|
* @return auth<int> Niveau d'authentification (0 à 2)
|
|
*
|
|
*/
|
|
public static function auth(){
|
|
return !is_array($GLOBALS['PERM']) ? 0 : count($GLOBALS['PERM']);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|