Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-basic-auth

Package Overview
Dependencies
Maintainers
13
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-basic-auth - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

51

index.js

@@ -7,11 +7,16 @@ 'use strict'

async function basicPlugin (fastify, opts) {
function basicPlugin (fastify, opts, next) {
if (typeof opts.validate !== 'function') {
throw new Error('Basic Auth: Missing validate function')
return next(new Error('Basic Auth: Missing validate function'))
}
const authenticateHeader = getAuthenticateHeader(opts.authenticate)
const authenticateHeader = getAuthenticateHeader(opts.authenticate, next)
const validate = opts.validate.bind(fastify)
fastify.decorate('basicAuth', basicAuth)
next()
function basicAuth (req, reply, next) {
if (authenticateHeader) {
reply.header(authenticateHeader.key, authenticateHeader.value)
}
const credentials = auth(req)

@@ -27,9 +32,4 @@ if (credentials == null) {

function done (err, realm) {
// TODO remove in the next major
if (typeof err === 'string') {
realm = err
err = undefined
}
if (err) {
function done (err) {
if (err !== undefined) {
// We set the status code to be 401 if it is not set

@@ -41,4 +41,2 @@ if (!err.statusCode) {

} else {
const header = realm ? formatRealm(realm) : authenticateHeader
reply.header('WWW-Authenticate', header)
next()

@@ -50,28 +48,23 @@ }

function getAuthenticateHeader (authenticate) {
function getAuthenticateHeader (authenticate, next) {
if (!authenticate) return false
if (authenticate === true) {
return 'Basic'
return {
key: 'WWW-Authenticate',
value: 'Basic'
}
}
if (typeof authenticate === 'object') {
const realm = formatRealm(authenticate.realm)
if (realm) {
return realm
const realm = (authenticate.realm && typeof authenticate.realm === 'string')
? authenticate.realm
: ''
return {
key: 'WWW-Authenticate',
value: 'Basic' + (realm ? ` realm="${realm}"` : '')
}
}
throw new Error('Basic Auth: Invalid authenticate option')
next(new Error('Basic Auth: Invalid authenticate option'))
}
function formatRealm (realm) {
switch (typeof realm) {
case 'undefined':
return 'Basic'
case 'boolean':
return 'Basic'
case 'string':
return `Basic realm="${realm}"`
}
}
module.exports = fp(basicPlugin, {

@@ -78,0 +71,0 @@ fastify: '3.x',

{
"name": "fastify-basic-auth",
"version": "1.1.0",
"version": "1.2.0",
"description": "Fastify basic auth plugin",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -58,3 +58,3 @@ # fastify-basic-auth

if (username !== 'Tyrion' || password !== 'wine') {
throw new Error('Winter is coming')
return new Error('Winter is coming')
}

@@ -83,3 +83,3 @@ }

if (username !== 'Tyrion' || password !== 'wine') {
throw new Error('Winter is coming')
return new Error('Winter is coming')
}

@@ -137,30 +137,4 @@ }

It is also possible to override set the `realm` dynamically by returning it
as the first argument.
See code above for examples.
```js
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
fastify.register(require('fastify-basic-auth'), { validate, authenticate })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'Wine') {
throw new Error('Winter is coming')
}
// custom realm
return 'Lannister'
}
fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
onRequest: fastify.basicAuth,
handler: async (req, reply) => {
return { hello: 'world' }
}
})
})
```
### `authenticate` <Boolean|Object> (optional, default: false)

@@ -198,4 +172,5 @@

## License
Licensed under [MIT](./LICENSE).

@@ -239,4 +239,4 @@ 'use strict'

}, (err, res) => {
t.equal(res.headers['www-authenticate'], 'Basic realm="example"')
t.error(err)
t.equal(res.headers['www-authenticate'], 'Basic realm="example"')
t.equal(res.statusCode, 200)

@@ -575,22 +575,2 @@ })

test('Invalid options (realm is a number)', t => {
t.plan(1)
const fastify = Fastify()
fastify
.register(basicAuth, { validate, authenticate: { realm: 42 } })
function validate (username, password, req, res, done) {
if (username === 'user' && password === 'pwd') {
done()
} else {
done(new Error('Unauthorized'))
}
}
fastify.ready(function (err) {
t.equal(err.message, 'Basic Auth: Invalid authenticate option')
})
})
test('Invalid options (authenticate realm)', t => {

@@ -629,43 +609,4 @@ t.plan(3)

}, (err, res) => {
t.error(err)
t.equal(res.headers['www-authenticate'], 'Basic')
t.equal(res.statusCode, 200)
})
})
test('Invalid options (authenticate realm = undefined)', t => {
t.plan(3)
const fastify = Fastify()
fastify
.register(basicAuth, { validate, authenticate: { realm: undefined } })
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('user', 'pwd')
}
}, (err, res) => {
t.error(err)
t.equal(res.headers['www-authenticate'], 'Basic')
t.equal(res.statusCode, 200)

@@ -675,86 +616,4 @@ })

test('WWW-Authenticate Realm dynamic realm', t => {
t.plan(3)
const fastify = Fastify()
const authenticate = {
realm: true
}
fastify.register(basicAuth, { validate, authenticate })
function validate (username, password, req, res, done) {
if (username === 'user' && password === 'pwd') {
done(null, 'root')
} 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('user', 'pwd')
}
}, (err, res) => {
t.error(err)
t.equal(res.headers['www-authenticate'], 'Basic realm="root"')
t.equal(res.statusCode, 200)
})
})
test('WWW-Authenticate Realm dynamic realm promise', t => {
t.plan(3)
const fastify = Fastify()
const authenticate = {
realm: true
}
fastify.register(basicAuth, { validate, authenticate })
function validate (username, password, req, res) {
if (username === 'user' && password === 'pwd') {
return Promise.resolve('root')
} else {
return Promise.reject(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('user', 'pwd')
}
}, (err, res) => {
t.error(err)
t.equal(res.headers['www-authenticate'], 'Basic realm="root"')
t.equal(res.statusCode, 200)
})
})
function basicAuthHeader (username, password) {
return 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64')
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc