Skip to content

Commit

Permalink
Merge pull request #435 from jaredhanson/fix-lazy-session
Browse files Browse the repository at this point in the history
Improve compatibility with old strategies and new lazy sessions.
  • Loading branch information
jaredhanson committed Nov 9, 2015
2 parents aa74207 + 6909b22 commit 5334ee0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
16 changes: 14 additions & 2 deletions lib/framework/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ var initialize = require('../middleware/initialize')
* @return {Object}
* @api protected
*/
module.exports = function() {
exports = module.exports = function() {

// HTTP extensions.
require('../http/request');
exports.__monkeypatchNode();

return {
initialize: initialize,
authenticate: authenticate
};
};

exports.__monkeypatchNode = function() {
var http = require('http');
var IncomingMessageExt = require('../http/request');

http.IncomingMessage.prototype.login
http.IncomingMessage.prototype.logIn = IncomingMessageExt.logIn;
http.IncomingMessage.prototype.logout
http.IncomingMessage.prototype.logOut = IncomingMessageExt.logOut;
http.IncomingMessage.prototype.isAuthenticated = IncomingMessageExt.isAuthenticated;
http.IncomingMessage.prototype.isUnauthenticated = IncomingMessageExt.isUnauthenticated;
};
6 changes: 4 additions & 2 deletions lib/http/request.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* Module dependencies.
*/
var http = require('http')
, req = http.IncomingMessage.prototype;
//var http = require('http')
// , req = http.IncomingMessage.prototype;


var req = exports = module.exports = {};

/**
* Intiate a login session for `user`.
*
Expand Down
7 changes: 7 additions & 0 deletions lib/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Module dependencies.
*/
var http = require('http')
, IncomingMessageExt = require('../http/request')
, AuthenticationError = require('../errors/authenticationerror');


Expand Down Expand Up @@ -78,6 +79,12 @@ module.exports = function authenticate(passport, name, options, callback) {
}

return function authenticate(req, res, next) {
if (http.IncomingMessage.prototype.logIn
&& http.IncomingMessage.prototype.logIn !== IncomingMessageExt.logIn) {
require('../framework/connect').__monkeypatchNode();
}


// accumulator for failures from each strategy in the chain
var failures = [];

Expand Down
23 changes: 21 additions & 2 deletions test/http/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
/* jshint expr: true */

var http = require('http')
, Passport = require('../..').Passport;
, Passport = require('../..').Passport
, IncomingMessageExt = require('../../lib/http/request');

require('../../lib/http/request');
require('../../lib/framework/connect').__monkeypatchNode();


describe('http.ServerRequest', function() {

describe('prototoype', function() {
var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req.logIn = IncomingMessageExt.logIn;
req.logout = IncomingMessageExt.logout;
req.logOut = IncomingMessageExt.logOut;
req.isAuthenticated = IncomingMessageExt.isAuthenticated;
req.isUnauthenticated = IncomingMessageExt.isUnauthenticated;

it('should be extended with login', function() {
expect(req.login).to.be.an('function');
Expand All @@ -37,6 +44,7 @@ describe('http.ServerRequest', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand Down Expand Up @@ -77,6 +85,7 @@ describe('http.ServerRequest', function() {
passport._userProperty = 'currentUser';

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand Down Expand Up @@ -120,6 +129,7 @@ describe('http.ServerRequest', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand All @@ -145,6 +155,7 @@ describe('http.ServerRequest', function() {

describe('not establishing a session, without passport.initialize() middleware', function() {
var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;

var error;

Expand Down Expand Up @@ -180,6 +191,7 @@ describe('http.ServerRequest', function() {
});

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand Down Expand Up @@ -223,6 +235,7 @@ describe('http.ServerRequest', function() {
passport._userProperty = 'currentUser';

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand Down Expand Up @@ -269,6 +282,7 @@ describe('http.ServerRequest', function() {
});

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand Down Expand Up @@ -305,6 +319,7 @@ describe('http.ServerRequest', function() {

describe('establishing a session, without passport.initialize() middleware', function() {
var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
var user = { id: '1', username: 'root' };

it('should throw an exception', function() {
Expand All @@ -321,6 +336,7 @@ describe('http.ServerRequest', function() {
});

var req = new http.IncomingMessage();
req.login = IncomingMessageExt.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
Expand All @@ -343,6 +359,7 @@ describe('http.ServerRequest', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
req.logout = IncomingMessageExt.logout;
req.user = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = passport;
Expand All @@ -369,6 +386,7 @@ describe('http.ServerRequest', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
req.logout = IncomingMessageExt.logout;
req.currentUser = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = passport;
Expand All @@ -394,6 +412,7 @@ describe('http.ServerRequest', function() {

describe('existing session, without passport.initialize() middleware', function() {
var req = new http.IncomingMessage();
req.logout = IncomingMessageExt.logout;
req.user = { id: '1', username: 'root' };

req.logout();
Expand Down

0 comments on commit 5334ee0

Please sign in to comment.