1

I need to create a JWT(Json Web Token) , I have client credentials token in ES256 algorithm.

Requirement - > https://docs.talkdesk.com/docs/using-a-signed-jwt

SO I have Install Nodejs in local , when I am trying to get this work its giving me error, I am new to javascript. may anyone please help me to get it work.

Error is internal/crypto/sig.js80

 const http = require('http');
 var jwt = require('jsonwebtoken');
 var uuid = require('uuid/v4');

//create a server object:

var private_key = 'private_key goes here'
 private_key = "-----BEGIN PRIVATE KEY-----\n" + private_key + "\n-----END PRIVATE KEY-----"

var header = {
  kid: 'bdef4554463d8078be9af1d9de55'
}

var payload = {
  iss: 'a57bb14a44455e98800d6a513953fc0',
  sub: 'a57bb14a445541e98800d6a513953fc0',
  aud: 'https://c2performdev.talkdeskid.com/oauth/token',
  jti: uuid(),
  exp: Math.floor(Date.now() / 1000) + 300,
 iat: Math.floor(Date.now() / 1000)
}

 token = jwt.sign(payload, private_key, {header: header, algorithm: 'ES256'})

1 Answer 1

6

There is issue with the key. The same code works fine if key is ok.

var fs = require("fs");
const http = require("http");
var jwt = require("jsonwebtoken");
var uuid = require("uuid/v4");

//create a server object:

var jwt = require("jsonwebtoken");

var private_key = fs.readFileSync("./private.pem");
var header = {
  kid: "bdef4554463d8078be9af1d9de55"
};

var payload = {
  iss: "a57bb14a44455e98800d6a513953fc0",
  sub: "a57bb14a445541e98800d6a513953fc0",
  aud: "https://c2performdev.talkdeskid.com/oauth/token",
  jti: uuid(),
  exp: Math.floor(Date.now() / 1000) + 300,
  iat: Math.floor(Date.now() / 1000)
};

token = jwt.sign(payload, private_key, { header: header, algorithm: "ES256" });

console.log(token);

I following command to generate key in mac.

openssl ecparam -genkey -name secp256k1 -noout -out private.pem
openssl ec -in private.pem -pubout -out public.pem

the body is key file is as follows.

-----BEGIN EC PRIVATE KEY-----
MHQCAQEEICu0QorVDVCvfc9JeiDlUxK4IJnx69vxOwLYZsPtVcmToAcGBSuBBAAK
oUQDQgAEQi9ENvV3eiN/hVed5eBqOTUa5v+olsdHk51RZbJNT7Rwz42tNSMrzucr
Jhn7xIOvQgw0NH5Tad+BE7ybIakSUg==
-----END EC PRIVATE KEY-----

this does not match what you have done. Try correcting the key structure.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for you reply , the key given in question is not correct , as I have changed them for security , and I am using windows terminal , the token we are received from talkdesk docs.talkdesk.com/docs/using-a-signed-jwt , I need to create a JWT token to get it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.