Skip to content

Commit 3ec8ae4

Browse files
aubergine10aubergine10
authored andcommitted
Updated readme.md for review - totaljs#43
Added note on caching credentials to avoid excessive db lookups, also added note on req.uri.auth
1 parent 5ea05d4 commit 3ec8ae4

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

authorization-www-basic/readme.md

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Features covered by this example:
99

1010
See the `/controllers/default.js` for sample code.
1111

12+
> **Note:** BAA doesn't attempt to encrypt credentials and as such should only be used on HTTPS connections.
13+
1214
### Reading credentials
1315

1416
To read credentials, use the `.baa()` method in a route handler function:
@@ -39,7 +41,7 @@ function authorization() {
3941
// ...
4042

4143
if (auth.empty) { // ask user to login
42-
this.baa('Log in, bro.'); // or whatever prompt you want the user to see
44+
this.baa('Admin Login Required.'); // or whatever prompt you want the user to see
4345
return;
4446
}
4547

@@ -50,10 +52,10 @@ function authorization() {
5052
This sends a response back to the browser which has a `WWW-Authenticate` HTTP header like this:
5153

5254
```
53-
WWW-Authenticate: Basic realm="Log in, bro."
55+
WWW-Authenticate: Basic realm="Admin Login Required."
5456
```
5557

56-
On seeing that header, the browser will display the prompt (`Log in, bro.`) along with a basic login form with fields for username and password. When the user submits the form, the browser will retry the request, only this time it will have the required `Authorization` HTTP header that we are looking for.
58+
On seeing that header, the browser will display the prompt (`Admin Login Required.`) along with a basic login form with fields for username and password. When the user submits the form, the browser will retry the request, only this time it will have the required `Authorization` HTTP header that we are looking for.
5759

5860
### Validating credentials
5961

@@ -74,7 +76,7 @@ function authorization() {
7476
} else {
7577

7678
// ask them to login again?
77-
this.baa('Wrong details, try again, bro.');
79+
this.baa('Admin Login Required.');
7880
return;
7981

8082
// or maybe just throw a #401 error?
@@ -86,8 +88,72 @@ function authorization() {
8688
}
8789
```
8890

89-
> Note: The browser will keep sending the `Authorization` header on subsequent requests for about 15 minutes or more, effectively keeping the user logged in (from user perspective). Downside is that, server-side, you have to re-check the credentials on every request.
91+
### Bonus 1: Server-side caching
92+
93+
The browser will keep sending the `Authorization` header on subsequent requests for about 15 minutes, effectively keeping the user logged in (from user perspective). Downside is that, server-side, you have to re-check the credentials on every request. As such it's probably worth keeping a cache of validated credentials to avoid excessive database lookups, for example:
94+
95+
```javascript
96+
var baaCache = {};
97+
98+
function authorization() {
99+
100+
// ...
101+
102+
if ( (baaCache[auth.user] && baaCache[auth.user] === auth.password) || isValidLogin( auth.user, auth.password ) ) {
103+
104+
baaCache[auth.user] = auth.password; // cache
90105

91-
## Notes
106+
// do authorised stuff
107+
108+
} else {
109+
// ...
110+
}
111+
}
92112

93-
BAA doesn't make any attempt to encrypt the login details it sends via the `Authorization` HTTP header so, ideally, you should only ever use BAA over HTTPS connections.
113+
function housekeeping(tick) {
114+
if (tick % 5 === 0) // every 5 mins clear cache
115+
baaCache = {};
116+
}
117+
118+
// add this to export.install() at top of script:
119+
F.on('service', housekeeping)
120+
121+
// also add an export.uninstall() to remove the listener
122+
export.uninstall = function() {
123+
F.removeListener('service', housekeeping);
124+
}
125+
```
126+
### Bonus 2: URI authentication
127+
128+
The `.baa()` method only checks request HTTP headers for credentials, it doesn't check for credentials in the URI like this:
129+
130+
```
131+
https://user:password@www.example.com/
132+
```
133+
134+
If you wish to accept credentials in the URI, use `.req.uri.auth`:
135+
136+
```javascript
137+
function authorization() {
138+
139+
// ...
140+
141+
if (auth.empty) { // check for URI auth first, before asking user to login
142+
143+
if (this.req.uri.auth) { // found credentials on auth, use those instead
144+
145+
let creds = this.req.uri.auth.split(':');
146+
auth.user = creds[0];
147+
auth.password = creds[1];
148+
auth.empty = false;
149+
150+
} else {
151+
this.baa('Admin Login Required.'); // or whatever prompt you want the user to see
152+
return;
153+
}
154+
155+
}
156+
157+
// ...
158+
}
159+
```

0 commit comments

Comments
 (0)