-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
43 lines (30 loc) · 948 Bytes
/
utils.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
exports.authorization = function(authorization) {
if(!authorization)
return null;
var parts = authorization.split(' ');
if(parts.length != 2 || parts[0] != 'Basic')
return null;
var creds = new Buffer(parts[1], 'base64').toString(),
i = creds.indexOf(':');
if(i == -1)
return null;
var username = creds.slice(0, i);
password = creds.slice(i + 1);
return [username, password];
};
exports.unauthorized = function(res, realm, reason) {
reason = reason || 'authentication required';
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
res.json({error: 'unauthorized', reason: reason});
}
exports.rawRequest = function(url, headers, req, data) {
var s = req.method + ' ' + url + ' HTTP/1.1\r\n';
for(var header in headers) {
s += header + ': ' + headers[header] + '\r\n';
}
s += '\r\n';
if(data)
s += data.toString();
return s;
}