Skip to content

How do I create JWKS? #654

Discussion options

You must be logged in to vote
import fs from 'node:fs/promises'
import { generateKeyPair, exportJWK } from 'jose'
import Fastify from 'fastify'

const keyFile = './keys.json'

const fileExists = await fs
  .stat(keyFile)
  .then(() => true)
  .catch(() => false)

if (!fileExists) {
  const { privateKey } = await generateKeyPair('RS256')
  await fs.writeFile(keyFile, JSON.stringify(await exportJWK(privateKey)))
}

const { kty, n, e } = JSON.parse(await fs.readFile(keyFile, 'utf-8'))
const publicKey = { kty, n, e }

const fastify = Fastify({
  logger: true
})

fastify.get('/jwks', async (request, reply) => {
  reply.send({
    keys: [publicKey]
  })
})

fastify.listen({ port: 3042 }, (err, address) => {
  if (err) {
    f…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@amitava82
Comment options

Answer selected by amitava82
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants