2016-11-08 08:54:59 +00:00
|
|
|
<?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(){
|
2016-11-08 09:05:08 +00:00
|
|
|
/* (1) Initialisation des permissions */
|
|
|
|
if( !isset($GLOBALS['PERM']) )
|
|
|
|
$GLOBALS['PERM'] = [];
|
|
|
|
|
|
|
|
|
2016-11-08 08:54:59 +00:00
|
|
|
/* (1) Gestion de AUTH (authentification) dans HEADER
|
|
|
|
---------------------------------------------------------*/
|
2016-11-08 09:05:08 +00:00
|
|
|
define('__TOKEN__', isset($_SERVER['PHP_AUTH_DIGEST']) ? $_SERVER['PHP_AUTH_DIGEST'] : '' );
|
2016-11-08 08:54:59 +00:00
|
|
|
|
|
|
|
/* (2) Gestion de AUTH en fonction du token
|
|
|
|
---------------------------------------------------------*/
|
2016-11-08 09:05:08 +00:00
|
|
|
define('__TOKEN__', preg_match('/^[a-f0-9]{64}$/', __TOKEN__, $match) ? $match[0] : null );
|
2016-11-08 08:54:59 +00:00
|
|
|
|
|
|
|
/* (3) On vérifie l'authentification par BDD
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !self::deepCheck() )
|
2016-11-08 09:05:08 +00:00
|
|
|
define('__TOKEN__', null);
|
2016-11-08 08:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function deepCheck(){
|
|
|
|
/* [1] Si aucune authentification
|
|
|
|
=========================================================*/
|
|
|
|
if( self::auth() == 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Vérification de l'authentification
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Fetch cyclic-hashing-system -> check files */
|
|
|
|
$fn = [
|
2016-11-08 09:05:08 +00:00
|
|
|
'hash' => __BUILD__.'/api/chs/hash',
|
|
|
|
'cycle' => __BUILD__.'/api/chs/cycle'
|
2016-11-08 08:54:59 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if( !is_file($fn['hash']) || !is_file($fn['hash']) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Read files -> check contents */
|
|
|
|
$fc = [
|
|
|
|
'hash' => file_get_contents($fn['hash']),
|
|
|
|
'cycle' => file_get_contents($fn['cycle'])
|
|
|
|
];
|
|
|
|
|
|
|
|
if( strlen($fc['hash']) !== 64 || !is_numeric($fc['cycle']) )
|
|
|
|
return false;
|
|
|
|
|
2016-11-08 09:05:08 +00:00
|
|
|
/* (3) Compares content */
|
|
|
|
if( __TOKEN__ !== self::secure_hash($fc['hash'], intval($fc['cycle'])) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (4) Stores new content */
|
|
|
|
file_put_contents($fn['hash'], __TOKEN__);
|
|
|
|
file_put_contents($fn['cycle'], intval($fc['cycle'])-1);
|
2016-11-08 08:54:59 +00:00
|
|
|
|
2016-11-08 09:05:08 +00:00
|
|
|
/* (5) Stores permission */
|
|
|
|
if( !in_array('cyclic-hash-system', $GLOBALS['PERM']) )
|
|
|
|
$GLOBALS['PERM'][] = 'cyclic-hash-system';
|
2016-11-08 08:54:59 +00:00
|
|
|
|
|
|
|
|
2016-11-08 09:05:08 +00:00
|
|
|
/* [3] Returns true if no error
|
2016-11-08 08:54:59 +00:00
|
|
|
=========================================================*/
|
|
|
|
return true;
|
2016-11-08 09:05:08 +00:00
|
|
|
|
2016-11-08 08:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-11-08 09:05:08 +00:00
|
|
|
/* [1] Mise à jour de l'authentification
|
2016-11-08 08:54:59 +00:00
|
|
|
=========================================================*/
|
2016-11-08 09:05:08 +00:00
|
|
|
self::check();
|
2016-11-08 08:54:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [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
|
2016-11-08 09:05:08 +00:00
|
|
|
if( !in_array($permission, $GLOBALS['PERM']) )
|
2016-11-08 08:54:59 +00:00
|
|
|
return Error::PermissionError;
|
|
|
|
|
|
|
|
|
2016-11-08 09:05:08 +00:00
|
|
|
/* [3] Si on a toutes les permissions requises
|
2016-11-08 08:54:59 +00:00
|
|
|
=========================================================*/
|
|
|
|
return Error::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LE NIVEAU D'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
* @return auth<int> Niveau d'authentification (0 à 2)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function auth(){
|
2016-11-08 09:05:08 +00:00
|
|
|
return is_null(__AUTH_) ? 0 : 1;
|
2016-11-08 08:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|