How do I create JWKS? #654
Replies: 2 comments 2 replies
-
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) {
fastify.log.error(err)
process.exit(1)
}
})Pay attention to what is done with Best to read the spec on this to learn more about it. And also see the example: (note: it's not best idea to store those private keys on disk) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
amitava82
-
|
Now somewhere else, you can simply: import { createRemoteJWKSet } from "jose"
createRemoteJWKSet(new URL("http://localhost:3042/jwks")) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to figure out how to generate JWKS. So far all the examples I could find use node-jose library.
Beta Was this translation helpful? Give feedback.
All reactions