"kid" empty, unable to lookup correct key (JWT with PHP)

Viewed 57

I am trying to Implement the JWT with PHP following this tutorial: https://www.w3jar.com/how-to-implement-the-jwt-with-php/

and so far i am able to get the token but when i try to decode the token i face this error:

"kid" empty, unable to lookup correct key

index.php:

<?php
require 'JwtHandler.php';
$jwt = new JwtHandler();

$token = $jwt->jwtEncodeData(
    'http://localhost/php-auth-api/',
    array("name" => "John", "email" => "john@email.com", "id" => 21)
);

echo "<strong>Your Token is -</strong><br><div><code>$token</code></div>";
?>

JwtHandler.php:

require './vendor/autoload.php';

use Firebase\JWT\JWT;

class JwtHandler
{
protected $jwt_secrect;
protected $token;
protected $issuedAt;
protected $expire;
protected $jwt;

public function __construct()
{
    date_default_timezone_set('Asia/Kolkata');
    $this->issuedAt = time();

    $this->expire = $this->issuedAt + 3600;

    $this->jwt_secrect = "this_is_my_secrect";
}

public function jwtEncodeData($iss, $data)
{

    $this->token = array(
        "iss" => $iss,
        "aud" => $iss,
        "iat" => $this->issuedAt,
        "exp" => $this->expire,
        "data" => $data
    );

    $this->jwt = JWT::encode($this->token, $this->jwt_secrect, 'HS256');
    return $this->jwt;
}

public function jwtDecodeData($jwt_token)
{
    try {
        $decode = JWT::decode($jwt_token, $this->jwt_secrect, array('HS256'));
        return $decode->data;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}
}

decode.php:

<?php
if (isset($_GET['token'])) {
require 'JwtHandler.php';
$jwt = new JwtHandler();

$data =  $jwt->jwtDecodeData(trim($_GET['token']));

if(isset($data->id) && isset($data->name) && isset($data->email)):
    echo "<ul>
    <li>ID => $data->id</li>
    <li>Name => $data->name</li>
    <li>Email => $data->email</li>
    </ul>";
else:
    print_r($data);
endif;
}
?>
<form action="" method="GET">
<label for="_token"><strong>Enter Token</strong></label>
<input type="text" name="token" id="_token">
<input type="submit" value="Docode">
</form>

i did some research and i found this answer: https://stackoverflow.com/a/72310650/19663907

but i am not sure where to place the new instance of Key class, and where to use on the decode method (it is my first time working with JWT so be patient with me :) )

1 Answers

SOLUTION:

change the version of "firebase/php-jwt" from:

"require": {
    "firebase/php-jwt": "^6.3"
}

to:

"require": {
    "firebase/php-jwt": "^5.5.1"
}
Related