Skip to content

Commit

Permalink
Merge branch 'tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Strauss committed Feb 16, 2017
2 parents 530dd62 + f8718f7 commit d4d12a4
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 17 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,17 @@ function getUnauthorizedResponse(req) {

Per default the middleware will not add a `WWW-Authenticate` challenge header to
responses of unauthorized requests. You can enable that by adding `challenge: true`
to the options object. This will cause most browsers to show a popup to enter credentials
on unauthorized responses:
to the options object. This will cause most browsers to show a popup to enter
credentials on unauthorized responses. You can set the realm (the realm
identifies the system to authenticate against and can be used by clients to save
credentials) of the challenge by passing a static string or a function that gets
passed the request object and is expected to return the challenge:

```js
app.use(basicAuth({
users: { 'someuser': 'somepassword' },
challenge: true
challenge: true,
realm: 'Imb4T3st4pp'
}))
```

Expand All @@ -148,8 +152,11 @@ node example.js
This will start a small express server listening at port 8080. Just look at the file,
try out the requests and play around with the options.

## To Do
## Tests

- Allow to set a realm for the challenge
- Some kind of automated testing with the example server
- Decide what should be included in `1.0.0`
The cases in the `example.js` are also used for automated testing. So if you want
to contribute or just make sure that the package still works, simply run:

```shell
npm test
```
22 changes: 22 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ var jsonBodyAuth = basicAuth({
unauthorizedResponse: { foo: 'bar' }
})

//Uses a custom realm
var realmAuth = basicAuth({
challenge: true,
realm: 'test'
})

//Uses a custom realm function
var realmFunctionAuth = basicAuth({
challenge: true,
realm: function (req) {
return 'bla'
}
})

app.get('/static', staticUserAuth, function(req, res) {
res.status(200).send('You passed')
})
Expand Down Expand Up @@ -88,6 +102,14 @@ app.get('/jsonbody', jsonBodyAuth, function(req, res) {
res.status(200).send('You passed')
})

app.get('/realm', realmAuth, function(req, res) {
res.status(200).send('You passed')
})

app.get('/realmfunction', realmFunctionAuth, function(req, res) {
res.status(200).send('You passed')
})

app.listen(8080, function() {
console.log("Listening!")
})
Expand Down
31 changes: 21 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const auth = require('basic-auth')
const assert = require('assert')

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

if(typeof option != 'function')
return function() { return option }

return option
}

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 getResponseBody = options.unauthorizedResponse
var getResponseBody = ensureFunction(options.unauthorizedResponse, '')
var realm = ensureFunction(options.realm)

if(!getResponseBody)
getResponseBody = function() { return '' }
else if(typeof getResponseBody != 'function')
getResponseBody = function() { return options.unauthorizedResponse }

assert(typeof getResponseBody == 'function', 'Expected a string or function for the unauthorizedResponse option')
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')

Expand Down Expand Up @@ -44,9 +49,15 @@ function buildMiddleware(options) {
return next()

function unauthorized() {
//TODO: Allow to set realm for the challenge
if(challenge)
res.set('WWW-Authenticate', 'Basic')
if(challenge) {
var challengeString = 'Basic'
var realmName = realm(req)

if(realmName)
challengeString += ' realm="' + realmName + '"'

res.set('WWW-Authenticate', challengeString)
}

//TODO: Allow response body to be JSON (maybe autodetect?)
const response = getResponseBody(req)
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.3.3",
"description": "Plug & play basic auth middleware for express",
"main": "index.js",
"scripts": {
"test": "mocha test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/LionC/express-basic-auth.git"
Expand All @@ -22,5 +25,10 @@
"homepage": "https://github.com/LionC/express-basic-auth#readme",
"dependencies": {
"basic-auth": "^1.0.4"
},
"devDependencies": {
"mocha": "^3.2.0",
"should": "^11.2.0",
"supertest": "^3.0.0"
}
}
Loading

0 comments on commit d4d12a4

Please sign in to comment.