Skip to content

Commit

Permalink
feat: ability to pass strategy object to authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
ayZagen committed Aug 9, 2019
1 parent 1c8ede3 commit 4cce2f2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/middleware/authenticate.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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);
Expand Down
53 changes: 51 additions & 2 deletions 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() {
Expand Down Expand Up @@ -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() {
}
Expand Down

0 comments on commit 4cce2f2

Please sign in to comment.