Skip to content

Commit d4d12a4

Browse files
author
Leon Strauss
committed
Merge branch 'tests'
2 parents 530dd62 + f8718f7 commit d4d12a4

5 files changed

Lines changed: 321 additions & 17 deletions

File tree

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,17 @@ function getUnauthorizedResponse(req) {
125125

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

131134
```js
132135
app.use(basicAuth({
133136
users: { 'someuser': 'somepassword' },
134-
challenge: true
137+
challenge: true,
138+
realm: 'Imb4T3st4pp'
135139
}))
136140
```
137141

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

151-
## To Do
155+
## Tests
152156

153-
- Allow to set a realm for the challenge
154-
- Some kind of automated testing with the example server
155-
- Decide what should be included in `1.0.0`
157+
The cases in the `example.js` are also used for automated testing. So if you want
158+
to contribute or just make sure that the package still works, simply run:
159+
160+
```shell
161+
npm test
162+
```

example.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ var jsonBodyAuth = basicAuth({
6060
unauthorizedResponse: { foo: 'bar' }
6161
})
6262

63+
//Uses a custom realm
64+
var realmAuth = basicAuth({
65+
challenge: true,
66+
realm: 'test'
67+
})
68+
69+
//Uses a custom realm function
70+
var realmFunctionAuth = basicAuth({
71+
challenge: true,
72+
realm: function (req) {
73+
return 'bla'
74+
}
75+
})
76+
6377
app.get('/static', staticUserAuth, function(req, res) {
6478
res.status(200).send('You passed')
6579
})
@@ -88,6 +102,14 @@ app.get('/jsonbody', jsonBodyAuth, function(req, res) {
88102
res.status(200).send('You passed')
89103
})
90104

105+
app.get('/realm', realmAuth, function(req, res) {
106+
res.status(200).send('You passed')
107+
})
108+
109+
app.get('/realmfunction', realmFunctionAuth, function(req, res) {
110+
res.status(200).send('You passed')
111+
})
112+
91113
app.listen(8080, function() {
92114
console.log("Listening!")
93115
})

index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
const auth = require('basic-auth')
22
const assert = require('assert')
33

4+
function ensureFunction(option, defaultValue) {
5+
if(option == undefined)
6+
return function() { return defaultValue }
7+
8+
if(typeof option != 'function')
9+
return function() { return option }
10+
11+
return option
12+
}
13+
414
function buildMiddleware(options) {
515
var challenge = options.challenge != undefined ? !!options.challenge : false
616
var users = options.users || {}
717
var authorizer = options.authorizer || staticUsersAuthorizer
818
var isAsync = options.authorizeAsync != undefined ? !!options.authorizeAsync : false
9-
var getResponseBody = options.unauthorizedResponse
19+
var getResponseBody = ensureFunction(options.unauthorizedResponse, '')
20+
var realm = ensureFunction(options.realm)
1021

11-
if(!getResponseBody)
12-
getResponseBody = function() { return '' }
13-
else if(typeof getResponseBody != 'function')
14-
getResponseBody = function() { return options.unauthorizedResponse }
15-
16-
assert(typeof getResponseBody == 'function', 'Expected a string or function for the unauthorizedResponse option')
1722
assert(typeof users == 'object', 'Expected an object for the basic auth users, found ' + typeof users + ' instead')
1823
assert(typeof authorizer == 'function', 'Expected a function for the basic auth authorizer, found ' + typeof authorizer + ' instead')
1924

@@ -44,9 +49,15 @@ function buildMiddleware(options) {
4449
return next()
4550

4651
function unauthorized() {
47-
//TODO: Allow to set realm for the challenge
48-
if(challenge)
49-
res.set('WWW-Authenticate', 'Basic')
52+
if(challenge) {
53+
var challengeString = 'Basic'
54+
var realmName = realm(req)
55+
56+
if(realmName)
57+
challengeString += ' realm="' + realmName + '"'
58+
59+
res.set('WWW-Authenticate', challengeString)
60+
}
5061

5162
//TODO: Allow response body to be JSON (maybe autodetect?)
5263
const response = getResponseBody(req)

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.3.3",
44
"description": "Plug & play basic auth middleware for express",
55
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha test.js"
8+
},
69
"repository": {
710
"type": "git",
811
"url": "git+https://github.com/LionC/express-basic-auth.git"
@@ -22,5 +25,10 @@
2225
"homepage": "https://github.com/LionC/express-basic-auth#readme",
2326
"dependencies": {
2427
"basic-auth": "^1.0.4"
28+
},
29+
"devDependencies": {
30+
"mocha": "^3.2.0",
31+
"should": "^11.2.0",
32+
"supertest": "^3.0.0"
2533
}
2634
}

0 commit comments

Comments
 (0)