diff --git a/lib/passport-local/strategy.js b/lib/passport-local/strategy.js index 45ff60f..c4ef957 100644 --- a/lib/passport-local/strategy.js +++ b/lib/passport-local/strategy.js @@ -66,12 +66,13 @@ util.inherits(Strategy, passport.Strategy); * @param {Object} req * @api protected */ -Strategy.prototype.authenticate = function(req) { +Strategy.prototype.authenticate = function(req, options) { + options = options || {}; var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField); var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField); if (!username || !password) { - return this.fail(new BadRequestError('Missing credentials')); + return this.fail(new BadRequestError(options.badRequestMessage || 'Missing credentials')); } var self = this; diff --git a/test/strategy-test.js b/test/strategy-test.js index a05d2df..8926b4d 100644 --- a/test/strategy-test.js +++ b/test/strategy-test.js @@ -500,6 +500,40 @@ vows.describe('LocalStrategy').addBatch({ }, }, + 'strategy handling a request with a body, but no username or password, and badRequestMessage option': { + topic: function() { + var strategy = new LocalStrategy(function(){}); + return strategy; + }, + + 'after augmenting with actions': { + topic: function(strategy) { + var self = this; + var req = {}; + strategy.fail = function(info) { + self.callback(null, info); + } + + req.body = {}; + process.nextTick(function () { + strategy.authenticate(req, { badRequestMessage: 'Something is wrong with this request' }); + }); + }, + + 'should fail authentication' : function(err) { + // fail action was called, resulting in test callback + assert.isTrue(true); + }, + 'should pass BadReqestError as additional info' : function(err, info) { + assert.instanceOf(info, Error); + assert.instanceOf(info, BadRequestError); + }, + 'should set message correctly' : function(err, info) { + assert.equal(info.message, 'Something is wrong with this request'); + }, + }, + }, + 'strategy constructed without a verify callback': { 'should throw an error': function (strategy) { assert.throws(function() { new LocalStrategy() });