-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate jwt/jwks handler from plugin
- Loading branch information
Showing
16 changed files
with
457 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { dirname, join } from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
import openapiAutoload from '../../index.js' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
const fixturesDir = join(__dirname, 'routes') | ||
|
||
export default async function app (fastify, opts) { | ||
fastify.register(openapiAutoload, { | ||
handlersDir: join(fixturesDir, 'handlers'), | ||
openapiOpts: { specification: join(fixturesDir, 'spec', 'test-spec.yaml') } | ||
}) | ||
} | ||
|
||
// export const options = { | ||
// https: { | ||
// key: readFileSync(join('local-certs', 'autotelic.localhost-key.pem')), | ||
// cert: readFileSync(join('local-certs', 'autotelic.localhost.pem')) | ||
// }, | ||
// maxParamLength: 500 | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "fastify-openapi-autoload-example", | ||
"description": "An example to configure @autotelic/fastify-openapi-autoload", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "fastify start -w -l info -P -o index.js" | ||
}, | ||
"dependencies": { | ||
"fastify": "^4.25.2", | ||
"fastify-cli": "^6.0.0", | ||
"fastify-plugin": "^4.5.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default async (fastify, { operationId }) => { | ||
fastify.decorate(operationId, async (req, reply) => { | ||
reply.code(200).send({ foo: 'bar' }) | ||
}) | ||
} | ||
|
||
export const autoConfig = { operationId: 'getFoo' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
openapi: 3.1.0 | ||
info: | ||
version: 1.0.0 | ||
title: Test Spec | ||
license: | ||
name: MIT | ||
|
||
paths: | ||
/foo: | ||
get: | ||
summary: test GET route /foo | ||
operationId: getFoo | ||
security: | ||
- bearerAuth: [] | ||
tags: | ||
- foo | ||
responses: | ||
'204': | ||
description: test GET route /foo | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
foo: | ||
type: string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import https from 'node:https' | ||
|
||
import fastifyJwt from '@fastify/jwt' | ||
import buildGetJwks from 'get-jwks' | ||
|
||
export function jwtJwksHandler ({ | ||
jwksOpts = {}, | ||
issuer, | ||
authRequestDecorator = defaultAuth, | ||
securityHandlers = defaultHandlers | ||
} = {}) { | ||
const { | ||
max = 100, | ||
ttl = 60 * 1000, | ||
timeout = 5000, | ||
providerDiscovery = false, | ||
agent = new https.Agent({ keepAlive: true }), | ||
...opts | ||
} = jwksOpts | ||
|
||
const getJwks = buildGetJwks({ | ||
max, | ||
ttl, | ||
timeout, | ||
issuersWhitelist: [issuer], | ||
checkIssuer: (iss) => iss === issuer, | ||
providerDiscovery, | ||
agent, | ||
...opts | ||
}) | ||
|
||
return function makeSecurityHandler (fastify) { | ||
// Register JWT verify | ||
fastify.register(fastifyJwt, { | ||
decode: { complete: true }, | ||
secret: (_request, token, callback) => { | ||
const { header: { kid, alg }, payload: { iss } } = token | ||
return getJwks.getPublicKey({ kid, domain: iss, alg }) | ||
.then(publicKey => callback(null, publicKey), callback) | ||
} | ||
}) | ||
|
||
// Decorate request with authenticate method | ||
fastify.decorateRequest('authenticate', authRequestDecorator) | ||
|
||
return securityHandlers | ||
} | ||
} | ||
|
||
async function defaultAuth (request) { | ||
try { | ||
const decodedToken = await request.jwtVerify(request) | ||
const { userId } = decodedToken | ||
return userId | ||
} catch (err) { | ||
return null | ||
} | ||
} | ||
|
||
const defaultHandlers = { | ||
async bearerAuth (request, reply, params) { | ||
try { | ||
const userId = await request.authenticate(request) | ||
if (userId == null) throw new Error('no user id') | ||
} catch (e) { | ||
throw new Error(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.