base.js
* This is the most basic authentication handler possible. It only calls the
* "authenticate" method of the passed authentication adapters.
* Specialised authentication handlers must inherit this base handler
* and overwrite the "middleware" and "post" methods.
* @module greppy/auth/handler/base
* @author Ralf Grawunder <r.grawunder@googlemail.com>
* @author Hermann Mayer <hermann.mayer92@gmail.com>
* @param {Object} options - Options of the base authentication handler
var BaseAuthHandler = function(options)
* Do some preparation before the authentication and fetch the
* representations of the user and credentials according to the
* @param {Object} req - The Request
* @param {Object} res - The Response
* @param {Function} next - Function to call when finished
BaseAuthHandler.prototype.middleware = function(req, res, next)
var username = req.body.user || '';
var credentials = req.body.credentials || '';
this.process(username, credentials, req, next);
* Use a configured authentication adapter to authenticate.
* @param {Object} username - The given username
* @param {Object} credentials - The given password
* @param {Object} req - The Request
* @param {Function} next - Function to call when finished
BaseAuthHandler.prototype.process = function(username, credentials, req, callback)
throw new Error('No options specified');
throw new Error('No adapter(s) were specified as option');
if (!util.isArray(this.options.adapter)) {
this.options.adapter = [this.options.adapter];
async.map(this.options.adapter, function(adapter, callback) {
if ('function' !== typeof adapter.authentication) {
new Error('Specified adapter does not have a valid authentication method')
adapter.authentication(username, credentials, function(err, isAuthenticated, entity) {
return callback && callback(err);
req.greppy.auth.entity = entity;
callback && callback(null, isAuthenticated);
if ('function' === typeof self.options.error) {
return callback && callback(err);
results.forEach(function(result) {
'Authentification ' + (success ? 'succeeded' : 'failed') +
' for ' + (username ? username.green.bold : '[none]')
if (false === success && 'function' === typeof self.options.error) {
if (true === success && 'function' === typeof self.options.success) {
self.options.success(username);
self.post(success, function() {
return callback && callback(null, success);
* Do some postprocessing after the authentication.
* @param {Boolean} isAuthenticated - Has the user been authenticated?
* @param {Function} callback - Function to call when finished
BaseAuthHandler.prototype.post = function(isAuthenticated, callback)