forked from creationix/creationix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
53 lines (48 loc) · 1.5 KB
/
auth.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
// Based loosly on basicAuth from Connect
// Checker takes username and password and returns a user if valid
// Will force redirect if requested over HTTP instead of HTTPS
module.exports = function basicAuth(checker, realm) {
realm = realm || 'Authorization Required';
function unauthorized(res) {
res.writeHead(401, {
'WWW-Authenticate': 'Basic realm="' + realm + '"',
'Content-Length': 12
});
res.end("Unauthorized");
}
function badRequest(res) {
res.writeHead(400, {
"Content-Length": 11
});
res.end('Bad Request');
}
return function(req, res, next) {
// Check for non SSL connections
if (!req.socket.encrypted) {
var parts = req.headers.host.split(":");
var host = parts[0];
var port = parseInt(parts[1], 10);
if (port !== 80) {
port = port - (port % 1000) + 443;
host = host + ":" + port;
}
var url = "https://" + host + req.realUrl;
res.writeHead(301, {
Location: url,
"Content-Length": 0
});
res.end();
return;
}
var authorization = req.headers.authorization;
if (!authorization) return unauthorized(res);
var parts = authorization.split(' ');
var scheme = parts[0];
var credentials = new Buffer(parts[1], 'base64').toString().split(':');
if ('Basic' != scheme) return badRequest(res);
var user = checker(req, credentials[0], credentials[1]);
if (!user) return unauthorized(res);
req.remoteUser = user;
next();
}
};