How to create JWKs endpoint with pre existing base64 p12 keystore in Nodejs

449 Views Asked by At

I am working to create JWKs endpoint for a Nodejs application which can be used by another application to access public key. I only have access to base64 encoded keystore (.p12) and password in application source code.

Found related solutions using node-jose library but everywhere either a new keystore was generated or keystore was imported in .json format to node-jose. None of examples show how to import base64 encoded keystore.

To create new keystore:

const fs = require('fs');
const jose = require('jose');

const keyStore = jose.JWK.createkeyStore();

keyStore.generate('RSA', 2048, {alg: 'RS256', use: 'sig' })
.then(result => {
  fs.writeFileSync(
    'keys.json',
    JSON.stringify(keyStore.toJSON(true), null, '  ')
  )
})

To import keystore in .json format:

router.get('/jwks', async(req, res) => {
  const ks = fs.readFileSync('keys.json')
  const keyStore = await jose.JWK.asKeyStore(ks.toString())

  res.send(keyStore.toJSON())
})

Reference: https://ctrltilde.com/jwks-and-node-jose/

I know that once I can successfully import the keystore to node-jose, then keyStore.toJSON() can be used to create JWKs but couldn't find a solution on how to use base64 encoded keystore with node-jose library or a different approach.

Please suggest how to solve this problem to create JWKs for a Nodejs application if only data I have access to is base64 encoded keystore and password.

0

There are 0 best solutions below