Skip to content

Commit fdfe2fb

Browse files
authored
feat: add header option (#47) (#52)
1 parent 675e597 commit fdfe2fb

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ fastify.register(require('fastify-basic-auth'), {
179179
})
180180
```
181181

182+
### `header` String (optional)
183+
184+
When supplied, the header option is the name of the header to get
185+
credentials from for validation.
186+
187+
```js
188+
fastify.register(require('fastify-basic-auth'), {
189+
validate,
190+
header: 'x-forwarded-authorization'
191+
})
192+
```
182193

183194
## License
184195

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface FastifyBasicAuthOptions {
2525
done: (err?: Error) => void
2626
): void | Promise<void>;
2727
authenticate?: boolean | { realm: string };
28+
header?: string;
2829
}
2930

3031
declare const fastifyBasicAuth: FastifyPlugin<FastifyBasicAuthOptions>

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ async function basicPlugin (fastify, opts) {
99
throw new Error('Basic Auth: Missing validate function')
1010
}
1111
const authenticateHeader = getAuthenticateHeader(opts.authenticate)
12+
const header = (opts.header && opts.header.toLowerCase()) || 'authorization'
13+
1214
const validate = opts.validate.bind(fastify)
1315
fastify.decorate('basicAuth', basicAuth)
1416

1517
function basicAuth (req, reply, next) {
16-
const credentials = auth(req)
18+
const credentials = auth.parse(req.headers[header])
1719
if (credentials == null) {
1820
done(new Unauthorized('Missing or bad formatted authorization header'))
1921
} else {

index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ app.register(fastifyBasicAuth, {
1717
expectType<string>(password)
1818
expectType<FastifyRequest>(req)
1919
expectType<FastifyReply>(reply)
20-
}
20+
},
21+
header: 'x-forwarded-authorization'
2122
})
2223

2324
app.register(fastifyBasicAuth, {

test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,47 @@ test('WWW-Authenticate Realm (authenticate: {realm: "example"})', t => {
260260
})
261261
})
262262

263+
test('Header option specified', t => {
264+
t.plan(2)
265+
266+
const fastify = Fastify()
267+
fastify.register(basicAuth, {
268+
validate,
269+
header: 'X-Forwarded-Authorization'
270+
})
271+
272+
function validate (username, password, req, res, done) {
273+
if (username === 'user' && password === 'pwd') {
274+
done()
275+
} else {
276+
done(new Error('Unauthorized'))
277+
}
278+
}
279+
280+
fastify.after(() => {
281+
fastify.route({
282+
method: 'GET',
283+
url: '/',
284+
preHandler: fastify.basicAuth,
285+
handler: (req, reply) => {
286+
reply.send({ hello: 'world' })
287+
}
288+
})
289+
})
290+
291+
fastify.inject({
292+
url: '/',
293+
method: 'GET',
294+
headers: {
295+
authorization: basicAuthHeader('notuser', 'notpwd'),
296+
'x-forwarded-authorization': basicAuthHeader('user', 'pwd')
297+
}
298+
}, (err, res) => {
299+
t.error(err)
300+
t.equal(res.statusCode, 200)
301+
})
302+
})
303+
263304
test('Missing validate function', t => {
264305
t.plan(1)
265306

0 commit comments

Comments
 (0)