From 7047316a3998f17826385180a5b956f695d6a61f Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Tue, 21 Sep 2021 11:58:37 -0700 Subject: [PATCH 1/3] Assign authInfo when using assignProperty option. --- lib/middleware/authenticate.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/middleware/authenticate.js b/lib/middleware/authenticate.js index dc70cfc2..841d2821 100644 --- a/lib/middleware/authenticate.js +++ b/lib/middleware/authenticate.js @@ -247,7 +247,16 @@ module.exports = function authenticate(passport, name, options, callback) { } if (options.assignProperty) { req[options.assignProperty] = user; - return next(); + if (options.authInfo !== false) { + passport.transformAuthInfo(info, req, function(err, tinfo) { + if (err) { return next(err); } + req.authInfo = tinfo; + next(); + }); + } else { + next(); + } + return; } req.logIn(user, options, function(err) { From b4e4cfffc8f3ab05b43ad84f785c22800b327dfe Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Mon, 27 Nov 2023 14:17:59 -0800 Subject: [PATCH 2/3] Fix test to allow setting of authInfo from authorize call. --- test/authenticator.middleware.test.js | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/authenticator.middleware.test.js b/test/authenticator.middleware.test.js index a6d77ef0..9cd260f8 100644 --- a/test/authenticator.middleware.test.js +++ b/test/authenticator.middleware.test.js @@ -245,6 +245,55 @@ describe('Authenticator', function() { expect(request.account.username).to.equal('jaredhanson'); }); + it('should set authInfo to empty object', function() { + expect(request.authInfo).to.deep.equal({}); + }); + }); + + describe('handling a request with authInfo disabled', function() { + function Strategy() { + } + Strategy.prototype.authenticate = function(req) { + var user = { id: '1', username: 'jaredhanson' }; + this.success(user); + }; + + var passport = new Authenticator(); + passport.use('success', new Strategy()); + + var request, error; + + before(function(done) { + chai.connect.use(passport.authorize('success', { authInfo: false })) + .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; }); From 0f2f81c0e05d9bab89af380923c44f8467bf3b22 Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Mon, 27 Nov 2023 14:44:46 -0800 Subject: [PATCH 3/3] Fix test to allow setting of authInfo with assignProperty. --- test/middleware/authenticate.success.test.js | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/middleware/authenticate.success.test.js b/test/middleware/authenticate.success.test.js index 3216b678..c7007b1b 100644 --- a/test/middleware/authenticate.success.test.js +++ b/test/middleware/authenticate.success.test.js @@ -98,6 +98,55 @@ describe('middleware/authenticate', function() { expect(request.account.username).to.equal('jaredhanson'); }); + it('should set authInfo to empty object', function() { + expect(request.authInfo).to.deep.equal({}); + }); + }); + + describe('success that assigns a specific property with authInfo disabled', function() { + function Strategy() { + } + Strategy.prototype.authenticate = function(req) { + var user = { id: '1', username: 'jaredhanson' }; + this.success(user); + }; + + var passport = new Passport(); + passport.use('success', new Strategy()); + + var request, error; + + before(function(done) { + chai.connect.use(authenticate(passport, 'success', { assignProperty: 'account', authInfo: false })) + .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; });