diff --git a/lib/middleware/initialize.js b/lib/middleware/initialize.js index 90b869b6..0b9760e4 100644 --- a/lib/middleware/initialize.js +++ b/lib/middleware/initialize.js @@ -52,7 +52,17 @@ module.exports = function initialize(passport, options) { if (options.userProperty) { req._userProperty = options.userProperty; } - + + var compat = (options.compat === undefined) ? true : options.compat; + if (compat) { + // NOTE: Compat mode also requires that the `passport` instance have + // an `_sm` variable set to the SessionManager. + passport._userProperty = options.userProperty || 'user'; + + req._passport = {}; + req._passport.instance = passport; + } + next(); }; }; diff --git a/test/authenticator.middleware.test.js b/test/authenticator.middleware.test.js index bf6d0074..a6d77ef0 100644 --- a/test/authenticator.middleware.test.js +++ b/test/authenticator.middleware.test.js @@ -42,6 +42,14 @@ describe('Authenticator', function() { it('should not initialize namespace within session', function() { expect(request.session.passport).to.be.undefined; }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Authenticator); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('user'); + }); }); describe('handling a request with custom user property', function() { @@ -72,6 +80,14 @@ describe('Authenticator', function() { it('should not initialize namespace within session', function() { expect(request.session.passport).to.be.undefined; }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Authenticator); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('currentUser'); + }); }); }); diff --git a/test/middleware/initialize.test.js b/test/middleware/initialize.test.js index acef7a38..b572f93b 100644 --- a/test/middleware/initialize.test.js +++ b/test/middleware/initialize.test.js @@ -31,6 +31,14 @@ describe('middleware/initialize', function() { it('should not error', function() { expect(error).to.be.undefined; }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Passport); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('user'); + }); }); describe('handling a request with a new session', function() { @@ -58,6 +66,14 @@ describe('middleware/initialize', function() { it('should not initialize namespace within session', function() { expect(request.session.passport).to.be.undefined; }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Passport); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('user'); + }); }); describe('handling a request with an existing session', function() { @@ -89,6 +105,14 @@ describe('middleware/initialize', function() { expect(Object.keys(request.session.passport)).to.have.length(1); expect(request.session.passport.user).to.equal('123456'); }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Passport); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('user'); + }); }); describe('handling a request with an existing session using custom session key', function() { @@ -121,6 +145,45 @@ describe('middleware/initialize', function() { expect(Object.keys(request.session.authentication)).to.have.length(1); expect(request.session.authentication.user).to.equal('123456'); }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.an('object'); + expect(request._passport.instance).to.be.an.instanceOf(Passport); + expect(request._passport.instance).to.equal(passport); + expect(request._passport.instance._sm).to.be.an('object'); + expect(request._passport.instance._userProperty).to.equal('user'); + }); + }); + + describe('handling a request with a new session without compat mode', function() { + var passport = new Passport(); + var request, error; + + before(function(done) { + chai.connect.use(initialize(passport, { compat: false })) + .req(function(req) { + request = req; + + req.session = {}; + }) + .next(function(err) { + error = err; + done(); + }) + .dispatch(); + }); + + it('should not error', function() { + expect(error).to.be.undefined; + }); + + it('should not initialize namespace within session', function() { + expect(request.session.passport).to.be.undefined; + }); + + it('should expose authenticator on internal request property', function() { + expect(request._passport).to.be.undefined; + }); }); });