简体   繁体   English

如何使用node.js JavaScript创建jwt

[英]How to jwt creation with node.js javascript

I need to create a JWT(Json Web Token) , I have client credentials token in ES256 algorithm. 我需要创建一个JWT(Json Web令牌),我在ES256算法中具有客户端凭据令牌。

Requirement - > https://docs.talkdesk.com/docs/using-a-signed-jwt 要求-> 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. 所以我有在本地安装Nodejs,当我试图让这项工作给我错误时,我是JavaScript的新手。 may anyone please help me to get it work. 可能有人请帮助我使它工作。

Error is internal/crypto/sig.js80 错误是内部/密码/ 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'})

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. 我按照以下命令在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. 尝试更正密钥结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM