diff --git a/lib/middleware/authenticate.js b/lib/middleware/authenticate.js index ccc9b2ac..00db36ae 100644 --- a/lib/middleware/authenticate.js +++ b/lib/middleware/authenticate.js @@ -61,7 +61,7 @@ var http = require('http') * * passport.authenticate('twitter'); * - * @param {String|Array} name + * @param {String|Array|Object} name * @param {Object} options * @param {Function} callback * @return {Function} @@ -182,7 +182,12 @@ module.exports = function authenticate(passport, name, options, callback) { // Get the strategy, which will be used as prototype from which to create // a new instance. Action functions will then be bound to the strategy // within the context of the HTTP request/response pair. - var prototype = passport._strategy(layer); + var prototype ; + if( typeof layer === 'object'){ + prototype = layer; + }else{ + prototype = passport._strategy(layer); + } if (!prototype) { return next(new Error('Unknown authentication strategy "' + layer + '"')); } var strategy = Object.create(prototype); diff --git a/test/authenticator.test.js b/test/authenticator.test.js index fb7a1394..9911aa51 100644 --- a/test/authenticator.test.js +++ b/test/authenticator.test.js @@ -1,7 +1,8 @@ /* global describe, it, expect, before */ /* jshint expr: true, sub: true */ -var Authenticator = require('../lib/authenticator'); +var Authenticator = require('../lib/authenticator'), + chai = require('chai'); describe('Authenticator', function() { @@ -52,7 +53,55 @@ describe('Authenticator', function() { expect(authenticator._strategies['default']).to.be.undefined; }); }); - + + describe('with object strategy', function() { + function Strategy() { + } + Strategy.prototype.authenticate = function(req) { + var user = { id: '1', username: 'jaredhanson' }; + this.success(user); + }; + + var passport = new Authenticator(); + + var request, error; + + before(function(done) { + chai.connect.use(passport.authorize(new Strategy())) + .req(function(req) { + request = req; + + req.logIn = function(user, options, done) { + this.user = user; + done(); + }; + }) + .next(function(err) { + error = err; + done(); + }) + .dispatch(); + }); + + it('should not error', function() { + expect(error).to.be.undefined; + }); + + it('should not set user', function() { + expect(request.user).to.be.undefined; + }); + + it('should set account', function() { + expect(request.account).to.be.an('object'); + expect(request.account.id).to.equal('1'); + expect(request.account.username).to.equal('jaredhanson'); + }); + + it('should not set authInfo', function() { + expect(request.authInfo).to.be.undefined; + }); + }); + it('should throw if lacking a name', function() { function Strategy() { }