diff --git a/Readme.md b/Readme.md index 994310c..9f67bc2 100644 --- a/Readme.md +++ b/Readme.md @@ -82,7 +82,8 @@ Defaults to `rfc6750`. * `addHook`: If `false`, this plugin will not register `onRequest` hook automatically. Instead it provides two decorations: `fastify.verifyBearerAuth` and `fastify.verifyBearerAuthFactory` -* `hook`: Specify the hook to register the plugin, accepts `onRequest` or `preParsing`. Defaults to `onRequest` (optional) +* `addHook`: If `false`, this plugin will not register any hook automatically. Instead, it provides two decorations: `fastify.verifyBearerAuth` and + `fastify.verifyBearerAuthFactory`. If `true` or nullish, it will default to `onRequest`. You can also specify `onRequest` or `preParsing` to register the respective hook * `verifyErrorLogLevel`: An optional string specifying the log level for verification errors. It must be a valid log level supported by Fastify, or an exception will be thrown when registering the plugin. By default, this option is set to `error` diff --git a/index.js b/index.js index 5253103..6510550 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,10 @@ const { FST_BEARER_AUTH_INVALID_HOOK, FST_BEARER_AUTH_INVALID_LOG_LEVEL } = requ const validHooks = new Set(['onRequest', 'preParsing']) function fastifyBearerAuth (fastify, options, done) { - options = { addHook: true, verifyErrorLogLevel: 'error', ...options } + options = { verifyErrorLogLevel: 'error', ...options } + if (options.addHook === true || options.addHook == null) { + options.addHook = 'onRequest' + } if ( Object.hasOwn(fastify.log, 'error') === false || @@ -32,19 +35,18 @@ function fastifyBearerAuth (fastify, options, done) { } try { - if (options.addHook === true) { - options.hook ||= 'onRequest' - if (!validHooks.has(options.hook)) { + if (options.addHook) { + if (!validHooks.has(options.addHook)) { done(new FST_BEARER_AUTH_INVALID_HOOK()) } - if (options.hook === 'preParsing') { + if (options.addHook === 'preParsing') { const verifyBearerAuth = verifyBearerAuthFactory(options) fastify.addHook('preParsing', (request, reply, _payload, done) => { verifyBearerAuth(request, reply, done) }) } else { - fastify.addHook(options.hook, verifyBearerAuthFactory(options)) + fastify.addHook(options.addHook, verifyBearerAuthFactory(options)) } } else { fastify.decorate('verifyBearerAuthFactory', verifyBearerAuthFactory) diff --git a/test/hooks.test.js b/test/hooks.test.js index ccfac28..eb7190e 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -11,7 +11,7 @@ const authorization = 'Bearer 123456' test('onrequest hook used by default', async (t) => { t.plan(9) const fastify = Fastify() - fastify.register(plugin, { keys, hook: undefined }).get('/test', (_req, res) => { + fastify.register(plugin, { keys, addHook: undefined }).get('/test', (_req, res) => { res.send({ hello: 'world' }) }) @@ -40,7 +40,7 @@ test('onrequest hook used by default', async (t) => { test('preParsing hook used when specified', async (t) => { t.plan(9) const fastify = Fastify() - fastify.register(plugin, { keys, hook: 'preParsing' }).get('/test', (_req, res) => { + fastify.register(plugin, { keys, addHook: 'preParsing' }).get('/test', (_req, res) => { res.send({ hello: 'world' }) }) @@ -69,7 +69,7 @@ test('preParsing hook used when specified', async (t) => { test('onrequest hook used when specified', async (t) => { t.plan(9) const fastify = Fastify() - fastify.register(plugin, { keys, hook: 'onRequest' }).get('/test', (_req, res) => { + fastify.register(plugin, { keys, addHook: 'onRequest' }).get('/test', (_req, res) => { res.send({ hello: 'world' }) }) @@ -99,7 +99,7 @@ test('error when invalid hook specified', async (t) => { t.plan(1) const fastify = Fastify() try { - await fastify.register(plugin, { keys, hook: 'onResponse' }) + await fastify.register(plugin, { keys, addHook: 'onResponse' }) } catch (err) { t.assert.strictEqual(err.message, 'options.hook must be either "onRequest" or "preParsing"') } diff --git a/types/index.d.ts b/types/index.d.ts index 8fd898d..38c4faf 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -21,8 +21,7 @@ declare namespace fastifyBearerAuth { contentType?: string | undefined; bearerType?: string; specCompliance?: 'rfc6749' | 'rfc6750'; - addHook?: boolean | undefined; - hook?: 'onRequest' | 'preParsing' | undefined; + addHook?: boolean | 'onRequest' | 'preParsing' | undefined; verifyErrorLogLevel?: string; } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 7122d1e..e7a4a6f 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -17,7 +17,7 @@ const pluginOptionsAuthPromise: FastifyBearerAuthOptions = { errorResponse: (err: Error) => { return { error: err.message } }, contentType: '', bearerType: '', - hook: 'onRequest' + addHook: 'onRequest' } const pluginOptionsKeyArray: FastifyBearerAuthOptions = { @@ -34,8 +34,7 @@ const pluginOptionsUndefined: FastifyBearerAuthOptions = { errorResponse: (err: Error) => { return { error: err.message } }, contentType: undefined, bearerType: undefined, - addHook: undefined, - hook: undefined + addHook: undefined } expectAssignable<{ @@ -52,7 +51,7 @@ expectAssignable<{ errorResponse?: (err: Error) => { error: string }; contentType?: string | undefined; bearerType?: string; - addHook?: boolean | undefined; + addHook?: boolean | 'onRequest' | 'preParsing' | undefined; }>(pluginOptionsKeyArray) expectAssignable<{ @@ -61,8 +60,7 @@ expectAssignable<{ errorResponse?: (err: Error) => { error: string }; contentType?: string | undefined; bearerType?: string; - addHook?: boolean | undefined; - hook?: 'onRequest' | 'preParsing' | undefined; + addHook?: boolean | 'onRequest' | 'preParsing' | undefined; }>(pluginOptionsUndefined) expectAssignable<{