diff --git a/packages/fastify/README.md b/packages/fastify/README.md index c17ad8f..2afa2b3 100644 --- a/packages/fastify/README.md +++ b/packages/fastify/README.md @@ -17,23 +17,20 @@ fastify.register(ByuJwtProvider, { prefix: '/example/v1', development: process.env.NODE_ENV === 'development', /** May pass in ByuJwt options from @byu-oit/jwt */ - byuJwtOptions: { - issuer: 'https://api.byu.edu', - additionalValidations: [() => { - if(false) throw new Error('This will never happen') - }] - } - + issuer: 'https://api.byu.edu', + additionalValidations: [(jwt) => { + if(false) throw new Error('This will never happen') + }] }) await fastify.listen({ port: 3000 }).catch(console.error) ``` ## Options +In addition to the three properties below, you can also pass in any options that are defined in [BYU JWT](https://byu-oit.github.io/byu-jwt-nodejs/modules/BYU_JWT.html#md:options) documentation as well. -| property | type | default | description | -|-----------------------|---------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| prefix | string | `undefined` | Will only authenticate routes matching this prefix. | -| development | boolean | false | skips JWT verification for development purposes but will throw an error if NODE_ENV is set to `production`. | -| basePath | string | `undefined` | will validate that the audience starts with the provided basePath in production. | -| byuJwtOptions | object | `undefined` | an object that contains any ByuJwt options passed in. See the [BYU JWT](https://byu-oit.github.io/byu-jwt-nodejs/modules/BYU_JWT.html#md:options) Documentation for a full list of those options. | +| property | type | default | description | +|------------------|---------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| prefix | string | `undefined` | Will only authenticate routes matching this prefix. | +| development | boolean | false | skips JWT verification for development purposes but will throw an error if NODE_ENV is set to `production`. | +| basePath | string | `undefined` | will validate that the audience starts with the provided basePath in production. | diff --git a/packages/fastify/src/ByuJwtAuthenticator.ts b/packages/fastify/src/ByuJwtAuthenticator.ts index 4fad6f7..f8f6261 100644 --- a/packages/fastify/src/ByuJwtAuthenticator.ts +++ b/packages/fastify/src/ByuJwtAuthenticator.ts @@ -3,8 +3,7 @@ import { TokenError } from 'fast-jwt' import { type IncomingHttpHeaders } from 'http' import { BYU_JWT_ERROR_CODES, ByuJwtError } from './ByuJwtError.js' -export interface ByuJwtAuthenticatorOptions { - byuJwtOptions?: CreateByuJwtOptions +export interface ByuJwtAuthenticatorOptions extends CreateByuJwtOptions { development?: boolean basePath?: string } @@ -16,9 +15,8 @@ export class ByuJwtAuthenticator { private readonly ByuJwt: typeof ByuJwt private readonly development: boolean - constructor ({ development, basePath, byuJwtOptions = {} }: ByuJwtAuthenticatorOptions = {}) { + constructor ({ development, basePath, ...byuJwtOptions }: ByuJwtAuthenticatorOptions = {}) { this.development = development ?? false - /** Extra validation step if basePath is provided */ if (basePath != null) { if (byuJwtOptions.additionalValidations == null) { diff --git a/packages/fastify/test/ByuJwtProvider.spec.ts b/packages/fastify/test/ByuJwtProvider.spec.ts index 85a4156..0c570a2 100644 --- a/packages/fastify/test/ByuJwtProvider.spec.ts +++ b/packages/fastify/test/ByuJwtProvider.spec.ts @@ -13,7 +13,7 @@ const errorHandler = (error: ByuJwtError, request: FastifyRequest, reply: Fastif test('authenticated user', async t => { const fastify = Fastify() - await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, development }) + await fastify.register(ByuJwtProvider, { issuer, development }) fastify.get('/', (request) => request.caller) const result = await fastify.inject({ url: '/', headers: { 'x-jwt-assertion': expiredJwt } }).then(res => res.json()) t.is(result.netId, 'stuft2') @@ -21,7 +21,7 @@ test('authenticated user', async t => { test('cannot fetch key', async t => { const fastify = Fastify() - await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, basePath: '/test' }) + await fastify.register(ByuJwtProvider, { issuer, basePath: '/test' }) fastify.get('/', (request) => request.caller) const result = await fastify.inject({ url: '/', headers: { 'x-jwt-assertion': expiredJwt } }).then(res => res.json()) t.is(result.message, 'Cannot fetch key.') @@ -30,7 +30,7 @@ test('cannot fetch key', async t => { test('missing expected JWT', async t => { const fastify = Fastify() fastify.setErrorHandler(errorHandler) - await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, development }) + await fastify.register(ByuJwtProvider, { issuer, development }) fastify.get('/', () => true) const result = await fastify.inject('/').then(res => res.json()) t.is(result.message, 'Missing expected JWT')