Skip to content

Commit 4416b90

Browse files
author
Leon Strauss
committed
wrote examples and added more documentation
1 parent 928a45e commit 4416b90

2 files changed

Lines changed: 107 additions & 3 deletions

File tree

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ get the middleware:
1717
The middleware will now check incoming requests to match the credentials
1818
`admin:supersecret`.
1919

20-
## How it behaves
21-
2220
The middleware will check incoming requests for a basic auth (`Authorization`)
2321
header, parse it and check if the credentials are legit.
2422

@@ -27,8 +25,10 @@ header, parse it and check if the credentials are legit.
2725
**If a request is successfully authorized**, an `auth` property will be added to the request,
2826
containing an object with `user` and `password` properties, filled with the credentials.
2927

28+
# Static Users
29+
3030
If you simply want to check basic auth against one or multiple static credentials,
31-
you can simply pass the credentials as in the example above:
31+
you can pass those credentials as in the example above:
3232

3333
app.use(basicAuth({
3434
users: {
@@ -41,6 +41,8 @@ you can simply pass the credentials as in the example above:
4141
The middleware will check incoming requests to have a basic auth header matching
4242
one of the three passed credentials.
4343

44+
# Custom authorization
45+
4446
Alternatively, you can pass your own `authorizer` function, to check the credentials
4547
however you want. It will be called with a username and password and is expected to
4648
return `true` or `false` to indicate that the credentials were approved or not:
@@ -55,6 +57,8 @@ This will authorize all requests with credentials where the username begins with
5557
`'A'` and the password begins with `'secret'`. In an actual application you would
5658
likely look up some data instead ;-)
5759

60+
# Custom Async Authorization
61+
5862
Note that the `authorizer` function is expected to be synchronous here. This is
5963
the default behavior, you can pass `authorizeAsync: true` in the options object to indicate
6064
that your authorizer is asynchronous. In this case it will be passed a callback
@@ -73,3 +77,26 @@ Let's look at the same authorizer again, but this time asynchronous:
7377
else
7478
return cb(null, false)
7579
}
80+
81+
# Challenge
82+
83+
Per default the middleware will not add a `WWW-Authenticate` challenge header to
84+
responses of unauthorized requests. You can enable that by adding `challenge: true`
85+
to the options object. This will cause most browsers to show a popup to enter credentials
86+
on unauthorized responses:
87+
88+
app.use(basicAuth({
89+
users: { 'someuser': 'somepassword' },
90+
challenge: true
91+
}));
92+
93+
# Try it
94+
95+
The repository contains an `example.js` that you can run to play around and try
96+
the middleware. To use it just put it somewhere (or leave it where it is), run
97+
98+
npm install express express-basic-auth
99+
node example.js
100+
101+
This will start a small express server listening at port 8080. Just look at the file,
102+
try out the requests and play around with the options.

example.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var express = require('express');
2+
3+
var app = express();
4+
5+
var basicAuth = require('./index.js');
6+
7+
/**
8+
* express-basic-auth
9+
*
10+
* Example server. Just run in the same folder:
11+
*
12+
* npm install express express-basic-auth
13+
*
14+
* and then run this file with node ('node example.js')
15+
*
16+
* You can send GET requests to localhost:8080/async , /custom, /challenge or /static
17+
* and see how it refuses or accepts your request matching the basic auth settings.
18+
*/
19+
20+
//Requires basic auth with username 'Admin' and password 'secret1234'
21+
var staticUserAuth = basicAuth({
22+
users: {
23+
'Admin': 'secret1234'
24+
},
25+
challenge: false
26+
});
27+
28+
//Uses a custom (synchronous) authorizer function
29+
var customAuthorizerAuth = basicAuth({
30+
authorizer: myAuthorizer
31+
});
32+
33+
//Same, but sends a basic auth challenge header when authorization fails
34+
var challengeAuth = basicAuth({
35+
authorizer: myAuthorizer,
36+
challenge: true
37+
});
38+
39+
//Uses a custom asynchronous authorizer function
40+
var asyncAuth = basicAuth({
41+
authorizer: myAsyncAuthorizer,
42+
authorizeAsync: true
43+
});
44+
45+
46+
app.get('/static', staticUserAuth, function(req, res) {
47+
res.status(200).send('You passed');
48+
});
49+
50+
app.get('/custom', customAuthorizerAuth, function(req, res) {
51+
res.status(200).send('You passed');
52+
});
53+
54+
app.get('/challenge', challengeAuth, function(req, res) {
55+
res.status(200).send('You passed');
56+
});
57+
58+
app.get('/async', asyncAuth, function(req, res) {
59+
res.status(200).send('You passed');
60+
});
61+
62+
app.listen(8080, function() {
63+
console.log("Listening!");
64+
});
65+
66+
//Cusotm authorizer checking if the username starts with 'A' and the password with 'secret'
67+
function myAuthorizer(username, password) {
68+
return username.startsWith('A') && password.startsWith('secret');
69+
}
70+
71+
//Same but asynchronous
72+
function myAsyncAuthorizer(username, password, cb) {
73+
if(username.startsWith('A') && password.startsWith('secret'))
74+
return cb(null, true);
75+
else
76+
return cb(null, false)
77+
}

0 commit comments

Comments
 (0)