Skip to content

Commit

Permalink
Merge pull request #749 from jaredhanson/ayZagen-feat/strategy-as-object
Browse files Browse the repository at this point in the history
Feature: Pass instantiated strategy to authenticate.
  • Loading branch information
jaredhanson committed Dec 9, 2019
2 parents 1c8ede3 + 5c288ae commit 08f57c2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/middleware/authenticate.js
Expand Up @@ -61,7 +61,7 @@ var http = require('http')
*
* passport.authenticate('twitter');
*
* @param {String|Array} name
* @param {Strategy|String|Array} name
* @param {Object} options
* @param {Function} callback
* @return {Function}
Expand Down Expand Up @@ -182,10 +182,15 @@ 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);
if (!prototype) { return next(new Error('Unknown authentication strategy "' + layer + '"')); }

var strategy = Object.create(prototype);
var strategy, prototype;
if (typeof layer.authenticate == 'function') {
strategy = layer;
} else {
prototype = passport._strategy(layer);
if (!prototype) { return next(new Error('Unknown authentication strategy "' + layer + '"')); }

strategy = Object.create(prototype);
}


// ----- BEGIN STRATEGY AUGMENTATION -----
Expand Down
45 changes: 45 additions & 0 deletions test/authenticator.middleware.test.js
Expand Up @@ -150,6 +150,51 @@ describe('Authenticator', function() {
});
});

describe('handling a request with instantiated 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.authenticate(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 set user', function() {
expect(request.user).to.be.an('object');
expect(request.user.id).to.equal('1');
expect(request.user.username).to.equal('jaredhanson');
});

it('should set authInfo', function() {
expect(request.authInfo).to.be.an('object');
expect(Object.keys(request.authInfo)).to.have.length(0);
});
});

});


Expand Down
2 changes: 1 addition & 1 deletion test/authenticator.test.js
Expand Up @@ -52,7 +52,7 @@ describe('Authenticator', function() {
expect(authenticator._strategies['default']).to.be.undefined;
});
});

it('should throw if lacking a name', function() {
function Strategy() {
}
Expand Down

0 comments on commit 08f57c2

Please sign in to comment.