csrb/api/application/helpers/authorization_helper.php
2019-10-22 20:33:36 +05:30

92 lines
2.9 KiB
PHP

<?php
class AUTHORIZATION
{
public static function validateTimestamp()
{
$CI =& get_instance();
$headersData = getallheaders();
if (!array_key_exists("Token",$headersData))
{
set_status_header(REST_CONTROLLER::HTTP_OK,"OK");
$response['status']=false;
$response['error']="Token required!";
echo json_encode($response);
exit;
}
else{
$token = self::validateToken($headersData['Token']);
if($token != false && (now() - $token->exp < ($CI->config->item('token_timeout') * 60))){
set_status_header(200,"OK");
return true;
}
else if($token == false){
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
$response['status']=false;
$response['error']="Invalid token!";
log_message('error','Invalid token!');
echo json_encode($response);
exit;
}
else{
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
$response['status'] = false;
$response['message'] = 'Token Experied';
log_message('error','Token Experied');
echo json_encode($response);
exit;
}
}
}
public static function validateToken($token)
{
return JWT::decode($token);
}
public static function generateToken($data)
{
$CI =& get_instance();
return JWT::encode($data, $CI->config->item('jwt_key'));
}
//check authorization user or not
public static function checkAuthorizationUser($routingName){
$CI =& get_instance();
$headersData = getallheaders();
if($routingName == PAYMET_GATEWAY_RESPONSE || $routingName == ROUTE_LOGIN_E)
{
return true;
}
if (!array_key_exists("Authorization",$headersData) OR $headersData['Authorization']!=$CI->config->item('authorization_key'))
{
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
$response['status']=false;
$response['error']="Access denied!";
log_message('error','Access denied!');
echo json_encode($response);
exit;
}
//For all route name except contant route
else if($routingName != ROUTE_LOGIN && $routingName != ROUTE_LOGIN_A && $routingName != ROUTE_LOGIN_B && $routingName != ROUTE_LOGIN_C && $routingName != ROUTE_LOGIN_D && $routingName != ROUTE_LOGIN_E && $routingName != ROUTE_LOGIN_F && $routingName != ROUTE_LOGIN_G && $routingName != ROUTE_LOGIN_H && $routingName != ROUTE_LOGIN_I && $routingName != ROUTE_LOGIN_J){
$validateToken = self::validateTimestamp();
return $validateToken;
}
else{
return true;
}
}
}