Reputation: 337
I am creating rest api using codeigniter, Rest Server, and firebase php-jwt . following is my code to create jwt token.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/REST_Controller.php';
require_once APPPATH . '/libraries/JWT.php';
use \Firebase\JWT\JWT;
class Welcome extends REST_Controller
{
public function __construct($config = 'rest')
{
parent::__construct($config);
$this->load->model("Users_model");
}
public function login_post()
{
$username = $this->post('username');
$password = $this->post('password');
$invalidLogin = ['invalid' => $username];
if(!$username || !$password){
$this->response($invalidLogin, REST_Controller::HTTP_NOT_FOUND);
}
$id = $this->Users_model->checkLogin($username,$password);
if($id) {
$token['id'] = $id;
$token['username'] = $username;
$date = new DateTime();
$token['iat'] = $date->getTimestamp();
$token['exp'] = $date->getTimestamp() + 60*60*5;
$output['id_token'] = JWT::encode($token, $this->config->item('jwt_secret_key'));
$this->set_response($output, REST_Controller::HTTP_OK);
} else {
$this->set_response($invalidLogin, REST_Controller::HTTP_NOT_FOUND);
}
}
/******** This is target test function having error **********/
public function test_post()
{
$token = $this->input->get_request_header('Authorization');
try{
$payload = JWT::decode($token, $this->config->item('jwt_secret_key'),array('HS256'));
$this->response($payload, REST_Controller::HTTP_OK);
} catch (Exception $ex) {
$response = array("message" => $ex->getMessage());
$this->response($response, REST_Controller::HTTP_UNAUTHORIZED);
}
}
}
i have following files in my libraries folder
when i tested with postman with false token (altering string in signature) the error is given that
An uncaught Exception was encountered
Type: Error Message: Class 'Firebase\JWT\SignatureInvalidException' not found Filename: .../application/libraries/JWT.php</p> Line Number: 113
following code is there on Jwt.php
// Check the signature
if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
throw new SignatureInvalidException('Signature verification failed'); // This is line 113
}
but i have this file in my libraries folder. any help would be appreciated.
Upvotes: 0
Views: 4472
Reputation: 1580
I think you have to require_once
signature before JWT.php
file will be something like this in codeigniter libraries.
class SignatureInvalidException extends \UnexpectedValueException
{
}
Upvotes: 4