Nodejs Verify a JWT token generated by PHP JWT kit Error JsonWebTokenError: invalid signature

407 Views Asked by At

I am creating jwt token in my webserver with PHP jwt tool as below,

    $key = 'key';
    $allowInfinity=true;

    //for mobile devices allow infinity
    if($allowInfinity===false) {
        $token = array(
            "iss" => "https://www.example.com/",
            "aud" => "https://www.example.com/",
            "iat" => time(),
            "nbf" => time(),
            "exp" => time() + (60 * 60)
        );
    }else{
        $token = array(
            "iss" => "https://www.example.com/",
            "aud" => "https://www.example.com/",
            "iat" => time(),
            "nbf" => time()
        );
    }
    return JWT::encode($token, $key);

In my Nodejs code with module JsonWebTokenError i try to verify the token as below,

      var jwt = require('jsonwebtoken');
      if (token !== undefined) {
            jwt.verify(token, 'key', { algorithms: ['HS256'], audience: 'https://www.example.com/', issuer: 'https://www.example.com/' }, function (err, decoded) {
                if (err) {
                    logging.log("error", "Unauthorised Access:" + util.inspect(err));
                    logging.log('error', "Unauthorised Access Headers: " + util.inspect(sockclient.request.headers) + "");
                }
                logging.log("error", "Decode:" + util.inspect(decoded));
            });
        } else {
            logging.log("error", "Unauthorised Access: No token found.");
            logging.log('error', "Unauthorised Access Headers: " + util.inspect(sockclient.request.headers) + "")
        }

However, when decoding I get the below error in my log file, Any idea on what I am doing wrong here ?

[2022-04-25T14:27:03.774] [ERROR] log_file - Unauthorised Access:JsonWebTokenError: invalid signature
    at /var/www/vhosts/example.com/chat/node_modules/jsonwebtoken/verify.js:133:19
    at getSecret (/var/www/vhosts/example.com/chat/node_modules/jsonwebtoken/verify.js:90:14)
    at Object.module.exports [as verify] (/var/www/vhosts/example.com/chat/node_modules/jsonwebtoken/verify.js:94:10)
    at Namespace.<anonymous> (/var/www/vhosts/example.com/chat/chat_server.js:218:17)
    at Namespace.emit (events.js:400:28)
    at Namespace.emitReserved (/var/www/vhosts/example.com/chat/node_modules/socket.io/dist/typed-events.js:56:22)
    at /var/www/vhosts/example.com/chat/node_modules/socket.io/dist/namespace.js:141:26
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
0

There are 0 best solutions below