From 43039011923c0ef2af953f504ccfff9a00b8623d Mon Sep 17 00:00:00 2001 From: Matthew O'Donoghue Date: Mon, 12 Jul 2021 16:56:18 +0100 Subject: [PATCH] feat: add header option (#47) Allow for a 'header' property to be specified in the options object. If present use the value specified as the name of the header from which to extract credentials for validation. --- README.md | 11 +++++++++++ index.d.ts | 1 + index.js | 4 +++- index.test-d.ts | 3 ++- test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bed4154..4025817 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,17 @@ fastify.register(require('fastify-basic-auth'), { }) ``` +### `header` String (optional) + +When supplied, the header option is the name of the header to get +credentials from for validation. + +```js +fastify.register(require('fastify-basic-auth'), { + validate, + header: 'x-forwarded-authorization' +}) +``` ## License diff --git a/index.d.ts b/index.d.ts index 16603ac..0bc2d90 100644 --- a/index.d.ts +++ b/index.d.ts @@ -25,6 +25,7 @@ export interface FastifyBasicAuthOptions { done: (err?: Error) => void ): void | Promise; authenticate?: boolean | { realm: string }; + header?: string; } declare const fastifyBasicAuth: FastifyPlugin diff --git a/index.js b/index.js index 0b637a7..576af31 100644 --- a/index.js +++ b/index.js @@ -9,11 +9,13 @@ async function basicPlugin (fastify, opts) { throw new Error('Basic Auth: Missing validate function') } const authenticateHeader = getAuthenticateHeader(opts.authenticate) + const header = (opts.header && opts.header.toLowerCase()) || 'authorization' + const validate = opts.validate.bind(fastify) fastify.decorate('basicAuth', basicAuth) function basicAuth (req, reply, next) { - const credentials = auth(req) + const credentials = auth.parse(req.headers[header]) if (credentials == null) { done(new Unauthorized('Missing or bad formatted authorization header')) } else { diff --git a/index.test-d.ts b/index.test-d.ts index 86de954..3ed19b4 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -17,7 +17,8 @@ app.register(fastifyBasicAuth, { expectType(password) expectType(req) expectType(reply) - } + }, + header: 'x-forwarded-authorization' }) app.register(fastifyBasicAuth, { diff --git a/test.js b/test.js index 33ce6c3..ef07a8f 100644 --- a/test.js +++ b/test.js @@ -260,6 +260,47 @@ test('WWW-Authenticate Realm (authenticate: {realm: "example"})', t => { }) }) +test('Header option specified', t => { + t.plan(2) + + const fastify = Fastify() + fastify.register(basicAuth, { + validate, + header: 'X-Forwarded-Authorization' + }) + + function validate (username, password, req, res, done) { + if (username === 'user' && password === 'pwd') { + done() + } else { + done(new Error('Unauthorized')) + } + } + + fastify.after(() => { + fastify.route({ + method: 'GET', + url: '/', + preHandler: fastify.basicAuth, + handler: (req, reply) => { + reply.send({ hello: 'world' }) + } + }) + }) + + fastify.inject({ + url: '/', + method: 'GET', + headers: { + authorization: basicAuthHeader('notuser', 'notpwd'), + 'x-forwarded-authorization': basicAuthHeader('user', 'pwd') + } + }, (err, res) => { + t.error(err) + t.equal(res.statusCode, 200) + }) +}) + test('Missing validate function', t => { t.plan(1)