Javascript 生成JSON Web密钥集(JWKS)时的空密钥

Javascript 生成JSON Web密钥集(JWKS)时的空密钥,javascript,node.js,jwt,jwk,jose,Javascript,Node.js,Jwt,Jwk,Jose,我正在尝试创建一个JSON web密钥集,用于node.js API客户端。但是,当记录我的密钥时,它们似乎是空的 const {JWKS} = require("jose"); const keyAlg = "RSA"; const keySize = 2048; const keyUse = "sig"; const alg = "RS256"; const keystore = new JWKS.KeyStor

我正在尝试创建一个JSON web密钥集,用于node.js API客户端。但是,当记录我的密钥时,它们似乎是空的

const {JWKS} = require("jose");
const keyAlg = "RSA";
const keySize = 2048;
const keyUse = "sig";
const alg = "RS256";
const keystore = new JWKS.KeyStore();
keystore.generate(keyAlg, keySize, {
  alg,
  use: keyUse,
});

console.log("Public keys");
console.log("This can be used as the jwks in your API client configuration in the portal");
console.log(JSON.stringify(keystore.toJWKS(), null, 4));
console.log("Private keys");
console.log("This can be used as the keys value when configuring the api client");
console.log(JSON.stringify(keystore.toJWKS(true), null, 4));

keystore.generate
是一个异步函数。它返回一个不会立即执行的承诺。您可以使用
.then
或async/await获得承诺的结果

以下是来自github的库:

keystore.generate(“oct”,256)。 然后(函数(结果){ //{result}是一个jose.JWK.Key 关键=结果; });

将此应用于代码:

const {JWKS} = require("jose");
const keyAlg = "RSA";
const keySize = 2048;
const keyUse = "sig";
const alg = "RS256";
const keystore = new JWKS.KeyStore();
keystore.generate(keyAlg, keySize, {
  alg,
  use: keyUse,
}).then(function(result) {
  
  console.log(result);
  console.log("Public keys");
  console.log("This can be used as the jwks in your API client configuration in the portal");
  console.log(JSON.stringify(keystore.toJWKS(), null, 4));
  console.log("Private keys");
  console.log("This can be used as the keys value when configuring the api client");
  console.log(JSON.stringify(keystore.toJWKS(true), null, 4));
});

如果您不熟悉javascript异步函数,请查看关于该主题最流行的问答: