Empty keys when generating JSON Web Key Set (JWKS)

926 Views Asked by At

I'm trying to create a JSON web key set for use with a node.js API client. However when logging my keys they seem to be empty.

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));
1

There are 1 best solutions below

0
On BEST ANSWER

keystore.generate is an asynchronous function. It returns a promise which won't be executed immediately. You can use .then or async/await to get the results of a promise.

Here is an example from the libraries github:

keystore.generate("oct", 256). then(function(result) { // {result} is a jose.JWK.Key key = result; });

Applying this to your code:

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));
});

If you are new to javascript asynchronous functions, take a look at the most popular question and answer on the topic: How do I return the response from an asynchronous call?