@@ -17,8 +17,6 @@ get the middleware:
1717The middleware will now check incoming requests to match the credentials
1818` admin:supersecret ` .
1919
20- ## How it behaves
21-
2220The middleware will check incoming requests for a basic auth (` Authorization ` )
2321header, 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,
2826containing an object with ` user ` and ` password ` properties, filled with the credentials.
2927
28+ # Static Users
29+
3030If 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:
4141The middleware will check incoming requests to have a basic auth header matching
4242one of the three passed credentials.
4343
44+ # Custom authorization
45+
4446Alternatively, you can pass your own ` authorizer ` function, to check the credentials
4547however you want. It will be called with a username and password and is expected to
4648return ` 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
5658likely look up some data instead ;-)
5759
60+ # Custom Async Authorization
61+
5862Note that the ` authorizer ` function is expected to be synchronous here. This is
5963the default behavior, you can pass ` authorizeAsync: true ` in the options object to indicate
6064that 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.
0 commit comments