Skip to content

Some Refactoring #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const auth = require('basic-auth')
const assert = require('assert')

function ensureFunction(option, defaultValue) {
if(option == undefined)
if(option === undefined)
return function() { return defaultValue }

if(typeof option != 'function')
Expand All @@ -14,20 +14,20 @@ function ensureFunction(option, defaultValue) {
function buildMiddleware(options) {
var challenge = options.challenge != undefined ? !!options.challenge : false
var users = options.users || {}
var authorizer = options.authorizer || staticUsersAuthorizer
var isAsync = options.authorizeAsync != undefined ? !!options.authorizeAsync : false
var isAsync = options.hasOwnProperty(authorizeAsync) && !!options.authorizeAsync
var getResponseBody = ensureFunction(options.unauthorizedResponse, '')
var realm = ensureFunction(options.realm)
var authorizer

assert(typeof users == 'object', 'Expected an object for the basic auth users, found ' + typeof users + ' instead')
assert(typeof authorizer == 'function', 'Expected a function for the basic auth authorizer, found ' + typeof authorizer + ' instead')

function staticUsersAuthorizer(username, password) {
for(var i in users)
if(username == i && password == users[i])
return true

return false
if(options.hasOwnProperty('users')) {
assert(typeof users == 'object', 'Expected an object for the basic auth users, found ' + typeof users + ' instead')
assert(!options.authorizer, 'An users object cannot be combined with a custom authorizer')
authorizer = function(username, password) {
return users.indexOf(username) !== -1 && password === users[username]
}
} else {
assert(typeof options.authorizer == 'function', 'Expected a function for the basic auth authorizer, found ' + typeof authorizer + ' instead')
authorizer = options.authorizer
}

return function authMiddleware(req, res, next) {
Expand All @@ -41,12 +41,12 @@ function buildMiddleware(options) {
password: authentication.pass
}

var authorized = authorizer(authentication.name, authentication.pass, authorizerCallback)

if(isAsync)
return authorizer(authentication.name, authentication.pass, authorizerCallback)
else if(!authorizer(authentication.name, authentication.pass))
return unauthorized()
return authorized

return next()
return (authorized === true) ? next() : unauthorized()

function unauthorized() {
if(challenge) {
Expand Down