From 4cce2f2c4157a8e281e7aef4b93d8f177dbeb8ee Mon Sep 17 00:00:00 2001 From: ayazismailhakki Date: Fri, 9 Aug 2019 23:03:12 +0300 Subject: [PATCH 1/3] feat: ability to pass strategy object to authenticate --- lib/middleware/authenticate.js | 9 ++++-- test/authenticator.test.js | 53 ++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) 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() { } From 380b5c87d028f8bf65ae35c75b2a306e65efd9bb Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Sun, 8 Dec 2019 16:03:53 -0800 Subject: [PATCH 2/3] Stylistic edits. --- lib/middleware/authenticate.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/middleware/authenticate.js b/lib/middleware/authenticate.js index 00db36ae..dc70cfc2 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|Object} name + * @param {Strategy|String|Array} name * @param {Object} options * @param {Function} callback * @return {Function} @@ -182,15 +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 ; - if( typeof layer === 'object'){ - prototype = layer; - }else{ + 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); } - if (!prototype) { return next(new Error('Unknown authentication strategy "' + layer + '"')); } - - var strategy = Object.create(prototype); // ----- BEGIN STRATEGY AUGMENTATION ----- From 5c288aec345222f33f65b4e6a3693f9077b6ff68 Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Sun, 8 Dec 2019 16:11:24 -0800 Subject: [PATCH 3/3] Move test. --- test/authenticator.middleware.test.js | 45 +++++++++++++++++++++++ test/authenticator.test.js | 51 +-------------------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/test/authenticator.middleware.test.js b/test/authenticator.middleware.test.js index 26452662..580759f4 100644 --- a/test/authenticator.middleware.test.js +++ b/test/authenticator.middleware.test.js @@ -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); + }); + }); + }); diff --git a/test/authenticator.test.js b/test/authenticator.test.js index 9911aa51..44bcfd1b 100644 --- a/test/authenticator.test.js +++ b/test/authenticator.test.js @@ -1,8 +1,7 @@ /* global describe, it, expect, before */ /* jshint expr: true, sub: true */ -var Authenticator = require('../lib/authenticator'), - chai = require('chai'); +var Authenticator = require('../lib/authenticator'); describe('Authenticator', function() { @@ -54,54 +53,6 @@ describe('Authenticator', function() { }); }); - 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() { }