-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
executable file
·44 lines (38 loc) · 1.53 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
var parseArgs = require('minimist')(process.argv.slice(2));
//console.dir(parseArgs);
function authReject(response) {
response.statusCode = 401;
response.setHeader('WWW-Authenticate','Basic realm="HDHomeRun Transcoder"');
response.end('<html><body>Invalid password</body></html>');
return false;
}
function validateAuth(request,response) {
if (parseArgs.user && parseArgs.pass) {
var auth = request.headers['authorization']; // auth is in base64(username:password) so we need to decode the base64
if (auth) {
try {
//var tmp = auth.split(' '); // Split on a space, the original auth looks like "Basic Y2hhcmxlczoxMjM0NQ==" and we need the 2nd part
var plain_auth = new Buffer(auth.split(' ')[1], 'base64').toString(); // create a buffer and tell it the data coming in is base64
//console.log("Decoded Authorization ", plain_auth);
// At this point plain_auth = "username:password"
var creds = plain_auth.split(':'); // split on a ':'
var username = creds[0];
var password = creds[1];
console.log(username + ":" + password);
if (username == parseArgs.user && password == parseArgs.pass)
return true; // successfully validated
}
catch (err) {
// fall down to authReject
}
}
return authReject(response);
}
// if we get here, we're good
return true;
}
module.exports = {
validate: function (request,response) {
return validateAuth(request,response);
}
};