I already found link/answer like this that explain how to generate a token and how to use it.
My website is using Smarty and has a lot of form already existing. I am trying to find a way to send a token inside the header of every request and catch all the request to verify the header.
e.g : In AngularJS, I can use $httpProvider.interceptors
and headers['Authorization'] = 'Bearer ' + token;
I would like to create something like this
class Interceptor {
$token;
function __construct(type) {
switch (type) {
case 'beforeSending':
$token = md5(uniqid(rand(), TRUE));
$_SESSION['token'] = $token;
$_SERVER['HTTP_authorization'] = 'Bearer ' + $token;
break;
case 'beforeExecuting':
if(hash_equals($_SESSION['token'], $_POST['token'])){
//continue
}else{
//error redirect to homePage or logout
}
break;
}
}
}
This should add the token stored in the session to the header of every request.
This should also check if every request contain a correct token.
Is there a way to achieve this globally instead of adding an input to every form and checking every call ?