fastify-basic-auth
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -28,2 +28,3 @@ import { | ||
authenticate?: boolean | { realm: string }; | ||
header?: string; | ||
} | ||
@@ -30,0 +31,0 @@ |
@@ -12,2 +12,4 @@ 'use strict' | ||
const authenticateHeader = getAuthenticateHeader(opts.authenticate) | ||
const header = (opts.header && opts.header.toLowerCase()) || 'authorization' | ||
const validate = opts.validate.bind(fastify) | ||
@@ -17,3 +19,3 @@ fastify.decorate('basicAuth', basicAuth) | ||
function basicAuth (req, reply, next) { | ||
const credentials = auth(req) | ||
const credentials = auth.parse(req.headers[header]) | ||
if (credentials == null) { | ||
@@ -20,0 +22,0 @@ done(new Unauthorized('Missing or bad formatted authorization header')) |
@@ -20,3 +20,4 @@ import { expectType, expectAssignable } from 'tsd' | ||
expectType<FastifyReply>(reply) | ||
} | ||
}, | ||
header: 'x-forwarded-authorization' | ||
}) | ||
@@ -23,0 +24,0 @@ |
{ | ||
"name": "fastify-basic-auth", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Fastify basic auth plugin", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^15.0.0", | ||
"@types/node": "^16.0.0", | ||
"fastify": "^3.0.0", | ||
@@ -34,3 +34,3 @@ "fastify-auth": "^1.0.0", | ||
"tap": "^15.0.2", | ||
"tsd": "^0.14.0" | ||
"tsd": "^0.17.0" | ||
}, | ||
@@ -37,0 +37,0 @@ "dependencies": { |
@@ -182,5 +182,16 @@ # 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 | ||
Licensed under [MIT](./LICENSE). |
41
test.js
@@ -263,2 +263,43 @@ 'use strict' | ||
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 => { | ||
@@ -265,0 +306,0 @@ t.plan(1) |
Sorry, the diff of this file is not supported yet
31285
851
197