CI =& get_instance(); /** * jwt config file load */ $this->CI->load->config('jwt'); /** * Load Config Items Values */ $this->token_key = $this->CI->config->item('jwt_key'); $this->token_algorithm = $this->CI->config->item('jwt_algorithm'); $this->token_header = $this->CI->config->item('token_header'); $this->token_expire_time = $this->CI->config->item('token_expire_time'); } /** * Generate Token * @param: {array} data */ public function generateToken($data = null) { if ($data AND is_array($data)) { // add api time key in user array() $data['API_TIME'] = time(); try { return JWT::encode($data, $this->token_key, $this->token_algorithm); } catch(Exception $e) { return 'Message: ' .$e->getMessage(); } } else { return "Token Data Undefined!"; } } /** * Validate Token with Header * @return : user informations */ public function validateToken($headers, $login = false) { /** * Check Session Login */ if(!$this->session_exp($login)){ return ['status' => FALSE, 'message' => 'Please try login again.']; } /** * Authorization Header Exists */ $token_data = $this->tokenIsExist($headers); if($token_data['status'] === TRUE) { try { /** * Token Decode */ try { $token_decode = JWT::decode($token_data['token'], $this->token_key, array($this->token_algorithm)); } catch(Exception $e) { $this->session_exp($login); return ['status' => FALSE, 'message' => $e->getMessage()]; } if(!empty($token_decode) AND is_object($token_decode)) { // Check Token API Time [API_TIME] if (empty($token_decode->API_TIME OR !is_numeric($token_decode->API_TIME))) { $this->session_exp($login); return ['status' => FALSE, 'message' => 'Token Time Not Define! Please try login again.']; } else { /** * Check Token Time Valid */ $time_difference = strtotime('now') - $token_decode->API_TIME; if( $time_difference >= $this->token_expire_time ) { // remove session datas $this->session_exp($login); return ['status' => FALSE, 'message' => 'Token Time Expire! Please try login again.']; }else { /** * All Validation False Return Data */ return ['status' => TRUE, 'data' => $token_decode]; } } }else{ // $this->session_exp($login); return ['status' => FALSE, 'message' => 'Forbidden']; } } catch(Exception $e) { $this->session_exp($login); return ['status' => FALSE, 'message' => $e->getMessage().'! Please try login again.']; } }else { $this->session_exp($login); // Authorization Header Not Found! return ['status' => FALSE, 'message' => $token_data['message'].'! Please try login again.' ]; } } private function session_exp($login){ if ($login) { return true; }else{ return false; } } /** * Token Header Check * @param: request headers */ private function tokenIsExist($headers) { if(!empty($headers) AND is_array($headers)) { foreach ($headers as $header_name => $header_value) { if (strtolower(trim($header_name)) == strtolower(trim($this->token_header))) return ['status' => TRUE, 'token' => $header_value]; } } return ['status' => FALSE, 'message' => 'Token is not defined.']; } }