Skip to content

Commit b611bd0

Browse files
fix(fastify): flatten ByuJwtAuthenticator options (#156)
1 parent c2b95ab commit b611bd0

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

packages/fastify/README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,20 @@ fastify.register(ByuJwtProvider, {
1717
prefix: '/example/v1',
1818
development: process.env.NODE_ENV === 'development',
1919
/** May pass in ByuJwt options from @byu-oit/jwt */
20-
byuJwtOptions: {
21-
issuer: 'https://api.byu.edu',
22-
additionalValidations: [() => {
23-
if(false) throw new Error('This will never happen')
24-
}]
25-
}
26-
20+
issuer: 'https://api.byu.edu',
21+
additionalValidations: [(jwt) => {
22+
if(false) throw new Error('This will never happen')
23+
}]
2724
})
2825

2926
await fastify.listen({ port: 3000 }).catch(console.error)
3027
```
3128

3229
## Options
30+
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.
3331

34-
| property | type | default | description |
35-
|-----------------------|---------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
36-
| prefix | string | `undefined` | Will only authenticate routes matching this prefix. |
37-
| development | boolean | false | skips JWT verification for development purposes but will throw an error if NODE_ENV is set to `production`. |
38-
| basePath | string | `undefined` | will validate that the audience starts with the provided basePath in production. |
39-
| 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. |
32+
| property | type | default | description |
33+
|------------------|---------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
34+
| prefix | string | `undefined` | Will only authenticate routes matching this prefix. |
35+
| development | boolean | false | skips JWT verification for development purposes but will throw an error if NODE_ENV is set to `production`. |
36+
| basePath | string | `undefined` | will validate that the audience starts with the provided basePath in production. |

packages/fastify/src/ByuJwtAuthenticator.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { TokenError } from 'fast-jwt'
33
import { type IncomingHttpHeaders } from 'http'
44
import { BYU_JWT_ERROR_CODES, ByuJwtError } from './ByuJwtError.js'
55

6-
export interface ByuJwtAuthenticatorOptions {
7-
byuJwtOptions?: CreateByuJwtOptions
6+
export interface ByuJwtAuthenticatorOptions extends CreateByuJwtOptions {
87
development?: boolean
98
basePath?: string
109
}
@@ -16,9 +15,8 @@ export class ByuJwtAuthenticator {
1615
private readonly ByuJwt: typeof ByuJwt
1716
private readonly development: boolean
1817

19-
constructor ({ development, basePath, byuJwtOptions = {} }: ByuJwtAuthenticatorOptions = {}) {
18+
constructor ({ development, basePath, ...byuJwtOptions }: ByuJwtAuthenticatorOptions = {}) {
2019
this.development = development ?? false
21-
2220
/** Extra validation step if basePath is provided */
2321
if (basePath != null) {
2422
if (byuJwtOptions.additionalValidations == null) {

packages/fastify/test/ByuJwtProvider.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const errorHandler = (error: ByuJwtError, request: FastifyRequest, reply: Fastif
1313

1414
test('authenticated user', async t => {
1515
const fastify = Fastify()
16-
await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, development })
16+
await fastify.register(ByuJwtProvider, { issuer, development })
1717
fastify.get('/', (request) => request.caller)
1818
const result = await fastify.inject({ url: '/', headers: { 'x-jwt-assertion': expiredJwt } }).then(res => res.json())
1919
t.is(result.netId, 'stuft2')
2020
})
2121

2222
test('cannot fetch key', async t => {
2323
const fastify = Fastify()
24-
await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, basePath: '/test' })
24+
await fastify.register(ByuJwtProvider, { issuer, basePath: '/test' })
2525
fastify.get('/', (request) => request.caller)
2626
const result = await fastify.inject({ url: '/', headers: { 'x-jwt-assertion': expiredJwt } }).then(res => res.json())
2727
t.is(result.message, 'Cannot fetch key.')
@@ -30,7 +30,7 @@ test('cannot fetch key', async t => {
3030
test('missing expected JWT', async t => {
3131
const fastify = Fastify()
3232
fastify.setErrorHandler(errorHandler)
33-
await fastify.register(ByuJwtProvider, { byuJwtOptions: { issuer }, development })
33+
await fastify.register(ByuJwtProvider, { issuer, development })
3434
fastify.get('/', () => true)
3535
const result = await fastify.inject('/').then(res => res.json<ByuJwtError>())
3636
t.is(result.message, 'Missing expected JWT')

0 commit comments

Comments
 (0)