Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. var fs = require("fs"),
  2. auth = require("basic-auth"),
  3. md5 = require("apache-md5");
  4. var heads = require("robohydra").heads,
  5. RoboHydraHead = heads.RoboHydraHead;
  6.  
  7. function loadPasswords(filePath) {
  8. var passwordFileContents = fs.readFileSync(filePath).toString();
  9. var passwords = {};
  10.  
  11. passwordFileContents.split("\n").forEach(function(line) {
  12. var colonIndex = line.indexOf(":");
  13. var user = line.slice(0, colonIndex),
  14. pass = line.slice(colonIndex + 1);
  15.  
  16. if (user && pass) {
  17. passwords[user] = pass;
  18. }
  19. });
  20.  
  21. return passwords;
  22. }
  23.  
  24. function authHeadForPasswordFile(passwordFilePath) {
  25. var passwords = loadPasswords(passwordFilePath);
  26.  
  27. return new RoboHydraHead({
  28. name: 'admin-ui-auth',
  29. path: '/robohydra-admin.*',
  30. handler: function(req, res, next) {
  31. var credentials = auth(req) || {};
  32. var hashedPass = passwords[credentials.name];
  33.  
  34. if (passwords.hasOwnProperty(credentials.name) &&
  35. md5(credentials.pass, hashedPass) === hashedPass) {
  36. next(req, res);
  37. return;
  38. }
  39.  
  40. res.statusCode = 401;
  41. res.headers['WWW-Authenticate'] = 'Basic realm="example"';
  42. res.send('Access denied');
  43. }
  44. });
  45. }
  46.  
  47. module.exports.getBodyParts = function(conf) {
  48. var passwordFilePath = conf.passwordpath || "htpasswd";
  49. var authHead = authHeadForPasswordFile(passwordFilePath);
  50.  
  51. conf.robohydra.registerDynamicHead(authHead, {priority: "admin"});
  52.  
  53. return {};
  54. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement