htpasswd.js
* htpasswd Authentication Adapter
* This authentication adapter authenticates users
* @module greppy/auth/adapter/htpasswd
* @author Hermann Mayer <hermann.mayer92@gmail.com>
* @param {Object} options - Options of the htpasswd authentication adapter
var HtpasswdAuthAdapter = function(options)
throw new Error('No file was specified to the options');
if (!fs.existsSync(options.file)) {
throw new Error('File "' + options.file + '" does not exists');
if (!fs.statSync(options.file).isFile()) {
throw new Error('File "' + options.file + '" is not a file');
fs.readFileSync(options.file, 'utf8').split('\n').forEach(function(line) {
self.cache[line.shift()] = line.join(':');
* Authenticate a user with his credentials against a htpasswd file.
* @param {String} user - Username
* @param {String} credentials - Password
* @param {Function} callback - Function to call when finished
HtpasswdAuthAdapter.prototype.authentication = function(user, credentials, callback)
if (!this.cache.hasOwnProperty(user)) {
return callback && callback(null, false);
pass.validate(credentials, this.cache[user], function(err, success) {
if (err || false === success) {
return callback && callback(null, false);
callback && callback(null, true);