182 lines
6.8 KiB
PHP
182 lines
6.8 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_UNAUTHORIZED,"Unauthorized");
|
|
$response['status']=false;
|
|
$response['error']="Token not found!";
|
|
$custom_log_info = 'Token not found! '.'Token for Authendication Not Found. Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
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!";
|
|
$custom_log_info = 'Invalid token! Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
else{
|
|
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
|
|
$response['status'] = false;
|
|
$response['message'] = 'Token Experied';
|
|
$custom_log_info = 'Token Experied! Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
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();
|
|
//print_r($headersData);
|
|
|
|
$role_permission_array = array();
|
|
if(array_key_exists($CI->uri->uri_string, $CI->config->item('api_user_auth_info')))
|
|
{
|
|
$role_permission_array = $CI->config->item('api_user_auth_info')[$CI->uri->uri_string];
|
|
}
|
|
|
|
|
|
if( $routingName == ROUTE_LOGIN_X || $routingName == ROUTE_LOGIN_Y || $routingName == ROUTE_LOGIN_Z || $routingName == ROUTE_LOGIN_W || $routingName == ROUTE_LOGIN_V || $routingName == ROUTE_LOGIN_A || $routingName == TURN_SERVER_API || $routingName == VIDEO_CHAT_ACCESS_KEY_API || $routingName ==SAVEROOMINFO_API || $routingName ==SAVESMCCREDITPDIMAGE_API || $routingName ==GETSMCPDIMGSECTIONDATA_API)
|
|
{
|
|
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!";
|
|
$custom_log_info = 'Access denied! '.' Authorization key does not exist or not match. API Name - '.$routingName.' - Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
else if(isset($headersData['mode']))
|
|
{
|
|
if($headersData['mode'] == '0777')
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
$validateToken = self::validateTimestamp();
|
|
return $validateToken;
|
|
}
|
|
}
|
|
//not checking routes of applicant img capturing functionality
|
|
else if( $routingName == 'getPDImgDetails' || $routingName == 'uploadPDImgByApplicant')
|
|
{
|
|
return true;
|
|
}
|
|
//For all route name except contant route
|
|
else if($routingName!=ROUTE_LOGIN){
|
|
$validateToken = self::validateTimestamp();
|
|
|
|
// Check role base arry for current user have permission to access requested API
|
|
if(count($role_permission_array))
|
|
{
|
|
if(array_key_exists("Token",$headersData))
|
|
{
|
|
$token_payload = json_decode(json_encode(JWT::decode($headersData['Token'])), true);
|
|
// print_r($token_payload);die();
|
|
|
|
//check role info existing in token paylod
|
|
if(array_key_exists('custom:role_id',$token_payload))
|
|
{
|
|
if(in_array($token_payload['custom:role_id'],$role_permission_array)){ return $validateToken; }
|
|
else {
|
|
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
|
|
$response['status']=false;
|
|
$response['error']="Unauthorized Role Access";
|
|
$custom_log_info = 'Unauthorized Role Access - '. $token_payload['custom:role_id'] . ' Email - '.$token_payload['email'].' API Name- '.$routingName.' Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
set_status_header(REST_CONTROLLER::HTTP_UNAUTHORIZED,"Unauthorized");
|
|
$response['status']=false;
|
|
$response['error']="Could not Identify Role!";
|
|
$custom_log_info = 'Could not Identify Role!. API Name- '.$routingName.' Request INFO - '. self::apiRequestInfo();
|
|
log_message('error',$custom_log_info);
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
} // end of role based authorization logic
|
|
|
|
|
|
return $validateToken;
|
|
}
|
|
else{
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public static function apiRequestInfo()
|
|
{
|
|
$CI =& get_instance();
|
|
$CI->load->library('user_agent');
|
|
if ($CI->agent->is_browser())
|
|
{
|
|
$agent = $CI->agent->browser().' '.$CI->agent->version();
|
|
}
|
|
elseif ($CI->agent->is_robot())
|
|
{
|
|
$agent = $CI->agent->robot();
|
|
}
|
|
elseif ($CI->agent->is_mobile())
|
|
{
|
|
$agent = $CI->agent->mobile();
|
|
}
|
|
else
|
|
{
|
|
$agent = 'Unidentified User Agent';
|
|
}
|
|
// $logger = MYLOG::logFunction('request_logger');
|
|
//$logger->info('REQUEST_INFO',array('ip' => $this->input->ip_address(),'platform' => $this->agent->platform(),'browser' => $agent));
|
|
return $CI->input->ip_address().'#'.$CI->agent->platform().'#'.$agent;
|
|
}
|
|
|
|
} |