-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicauth.js
61 lines (56 loc) · 1.91 KB
/
basicauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @fileOverview Basic Authentication middleware.
*
* Like sticks' normal basicauth but accepts regex
*/
var strings = require('ringo/utils/strings');
var base64 = require('ringo/base64');
/**
*
* @param {Function} next the wrapped middleware chain
* @param {Object} app the Stick Application object
* @returns {Function} a JSGI middleware function
*/
exports.middleware = function basicauth(next, app) {
var config = {};
app.basicauth = function(path, role, sha1) {
config[path] = {};
config[path].regex = new RegExp(path);
config[path][role] = sha1;
};
return function basicauth(req) {
// normalize multiple slashes in request path
var path = (req.scriptName + req.pathInfo).replace(/\/+/g, '/');
var toAuth;
for each (var realm in config) {
if (realm.regex.test(path)) {
toAuth = realm;
break;
}
}
if (toAuth) {
if (req.headers.authorization) { // Extract credentials from HTTP.
var credentials = base64.decode(req.headers.authorization
.replace(/Basic /, '')).split(':');
if (strings.digest(credentials[1], 'sha1') === toAuth[credentials[0]]) {
req.session.data.isAuthorized = true;
return next(req); // Authorization.
}
}
var msg = '401 Unauthorized';
return { // Access denied.
status: 401,
headers: {
'Content-Type': 'text/html',
'WWW-Authenticate': 'Basic realm="Secure Area"'
},
body: [
'<html><head><title>', msg, '</title></head>',
'<body><h1>', msg, '</h1>',
'</body></html>'
]
};
}
return next(req);
}
};