Skip to content

Commit

Permalink
Implement compat mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Dec 16, 2021
1 parent 5c29557 commit 3253056
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/middleware/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
};
16 changes: 16 additions & 0 deletions test/authenticator.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
});
});

});
Expand Down
63 changes: 63 additions & 0 deletions test/middleware/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
});
});

});

0 comments on commit 3253056

Please sign in to comment.