Skip to content

Commit

Permalink
Implement support for badRequestMessage option.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jul 29, 2012
1 parent 47f4a13 commit 0d88891
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/passport-local/strategy.js
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions test/strategy-test.js
Expand Up @@ -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() });
Expand Down

0 comments on commit 0d88891

Please sign in to comment.