Last active
October 23, 2016 18:45
-
-
Save emanchado/71fe7d04dcb40fbef886d1213a727f47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"), | |
auth = require("basic-auth"), | |
md5 = require("apache-md5"); | |
var heads = require("robohydra").heads, | |
RoboHydraHead = heads.RoboHydraHead; | |
function loadPasswords(filePath) { | |
var passwordFileContents = fs.readFileSync(filePath).toString(); | |
var passwords = {}; | |
passwordFileContents.split("\n").forEach(function(line) { | |
var colonIndex = line.indexOf(":"); | |
var user = line.slice(0, colonIndex), | |
pass = line.slice(colonIndex + 1); | |
if (user && pass) { | |
passwords[user] = pass; | |
} | |
}); | |
return passwords; | |
} | |
function authHeadForPasswordFile(passwordFilePath) { | |
var passwords = loadPasswords(passwordFilePath); | |
return new RoboHydraHead({ | |
name: 'admin-ui-auth', | |
path: '/robohydra-admin.*', | |
handler: function(req, res, next) { | |
var credentials = auth(req) || {}; | |
var hashedPass = passwords[credentials.name]; | |
if (passwords.hasOwnProperty(credentials.name) && | |
md5(credentials.pass, hashedPass) === hashedPass) { | |
next(req, res); | |
return; | |
} | |
res.statusCode = 401; | |
res.headers['WWW-Authenticate'] = 'Basic realm="example"'; | |
res.send('Access denied'); | |
} | |
}); | |
} | |
module.exports.getBodyParts = function(conf) { | |
var passwordFilePath = conf.passwordpath || "htpasswd"; | |
var authHead = authHeadForPasswordFile(passwordFilePath); | |
conf.robohydra.registerDynamicHead(authHead, {priority: "admin"}); | |
return {}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "onionscantest", | |
"version": "0.0.1", | |
"dependencies": { | |
"apache-md5": "^1.1.1", | |
"basic-auth": "^1.0.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment