Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Nov 30, 2017
2 parents 906a0ec + 821a474 commit 13c550c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,5 @@ This project is supported by ![](http://passportjs.org/images/supported_logo.svg

Copyright (c) 2011-2015 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>

<a target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/vK9dyjRnnWsMzzJTQ57fRJpH/jaredhanson/passport'> <img alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/vK9dyjRnnWsMzzJTQ57fRJpH/jaredhanson/passport.svg' /></a>

14 changes: 12 additions & 2 deletions lib/authenticator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Module dependencies.
*/
var SessionStrategy = require('./strategies/session');
var SessionStrategy = require('./strategies/session')
, SessionManager = require('./sessionmanager');


/**
Expand All @@ -28,7 +29,8 @@ function Authenticator() {
*/
Authenticator.prototype.init = function() {
this.framework(require('./framework/connect')());
this.use(new SessionStrategy());
this.use(new SessionStrategy(this.deserializeUser.bind(this)));
this._sm = new SessionManager({ key: this._key }, this.serializeUser.bind(this));
};

/**
Expand Down Expand Up @@ -231,6 +233,14 @@ Authenticator.prototype.session = function(options) {
return this.authenticate('session', options);
};

// TODO: Make session manager pluggable
/*
Authenticator.prototype.sessionManager = function(mgr) {
this._sm = mgr;
return this;
}
*/

/**
* Registers a function used to serialize user objects into the session.
*
Expand Down
14 changes: 3 additions & 11 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,8 @@ req.logIn = function(user, options, done) {
if (typeof done != 'function') { throw new Error('req#login requires a callback function'); }

var self = this;
this._passport.instance.serializeUser(user, this, function(err, obj) {
this._passport.instance._sm.logIn(this, user, function(err) {
if (err) { self[property] = null; return done(err); }
if (!self._passport.session) {
self._passport.session = {};
}
self._passport.session.user = obj;
if (!self.session) {
self.session = {};
}
self.session[self._passport.instance._key] = self._passport.session;
done();
});
} else {
Expand All @@ -77,8 +69,8 @@ req.logOut = function() {
}

this[property] = null;
if (this._passport && this._passport.session) {
delete this._passport.session.user;
if (this._passport) {
this._passport.instance._sm.logOut(this);
}
};

Expand Down
38 changes: 38 additions & 0 deletions lib/sessionmanager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function SessionManager(options, serializeUser) {
if (typeof options == 'function') {
serializeUser = options;
options = undefined;
}
options = options || {};

this._key = options.key || 'passport';
this._serializeUser = serializeUser;
}

SessionManager.prototype.logIn = function(req, user, cb) {
var self = this;
this._serializeUser(user, req, function(err, obj) {
if (err) {
return cb(err);
}
if (!req._passport.session) {
req._passport.session = {};
}
req._passport.session.user = obj;
if (!req.session) {
req.session = {};
}
req.session[self._key] = req._passport.session;
cb();
});
}

SessionManager.prototype.logOut = function(req, cb) {
if (req._passport && req._passport.session) {
delete req._passport.session.user;
}
cb && cb();
}


module.exports = SessionManager;
12 changes: 10 additions & 2 deletions lib/strategies/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ var pause = require('pause')
*
* @api public
*/
function SessionStrategy() {
function SessionStrategy(options, deserializeUser) {
if (typeof options == 'function') {
deserializeUser = options;
options = undefined;
}
options = options || {};

Strategy.call(this);
this.name = 'session';
this._deserializeUser = deserializeUser;
}

/**
Expand Down Expand Up @@ -50,11 +57,12 @@ SessionStrategy.prototype.authenticate = function(req, options) {
// matter, refer to: https://github.com/jaredhanson/passport/pull/106

var paused = options.pauseStream ? pause(req) : null;
req._passport.instance.deserializeUser(su, req, function(err, user) {
this._deserializeUser(su, req, function(err, user) {
if (err) { return self.error(err); }
if (!user) {
delete req._passport.session.user;
} else {
// TODO: Remove instance access
var property = req._passport.instance._userProperty || 'user';
req[property] = user;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passport",
"version": "0.3.2",
"version": "0.4.0",
"description": "Simple, unobtrusive authentication for Node.js.",
"keywords": [
"express",
Expand Down
6 changes: 3 additions & 3 deletions test/authenticator.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ describe('Authenticator', function() {

describe('handling a request', function() {
var passport = new Authenticator();
passport.deserializeUser(function(user, done) {
done(null, { id: user });
});

var request, error;

Expand All @@ -230,9 +233,6 @@ describe('Authenticator', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, { id: user });
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand Down
14 changes: 6 additions & 8 deletions test/strategies/session.pause.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('SessionStrategy', function() {


var SessionStrategy = $require('../../lib/strategies/session', { pause: pause });
var strategy = new SessionStrategy();
var strategy = new SessionStrategy(function(user, req, done) {
done(null, { id: user });
});

var request, pass = false;

Expand All @@ -31,9 +33,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, { id: user });
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand Down Expand Up @@ -79,7 +78,9 @@ describe('SessionStrategy', function() {


var SessionStrategy = $require('../../lib/strategies/session', { pause: pause });
var strategy = new SessionStrategy();
var strategy = new SessionStrategy(function(user, req, done) {
done(null, false);
});

var request, pass = false;

Expand All @@ -94,9 +95,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, false);
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand Down
35 changes: 20 additions & 15 deletions test/strategies/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ describe('SessionStrategy', function() {
});

describe('handling a request with a login session', function() {
var strategy = new SessionStrategy(function(user, req, done) {
done(null, { id: user });
});

var request, pass = false;

before(function(done) {
Expand All @@ -54,9 +58,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, { id: user });
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand All @@ -79,6 +80,10 @@ describe('SessionStrategy', function() {
});

describe('handling a request with a login session serialized to 0', function() {
var strategy = new SessionStrategy(function(user, req, done) {
done(null, { id: user });
});

var request, pass = false;

before(function(done) {
Expand All @@ -92,9 +97,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, { id: user });
};
req._passport.session = {};
req._passport.session.user = 0;
})
Expand All @@ -117,6 +119,10 @@ describe('SessionStrategy', function() {
});

describe('handling a request with a login session that has been invalidated', function() {
var strategy = new SessionStrategy(function(user, req, done) {
done(null, false);
});

var request, pass = false;

before(function(done) {
Expand All @@ -130,9 +136,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, false);
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand All @@ -154,6 +157,10 @@ describe('SessionStrategy', function() {
});

describe('handling a request with a login session and setting custom user property', function() {
var strategy = new SessionStrategy(function(user, req, done) {
done(null, { id: user });
});

var request, pass = false;

before(function(done) {
Expand All @@ -168,9 +175,6 @@ describe('SessionStrategy', function() {
req._passport = {};
req._passport.instance = {};
req._passport.instance._userProperty = 'currentUser';
req._passport.instance.deserializeUser = function(user, req, done) {
done(null, { id: user });
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand All @@ -192,6 +196,10 @@ describe('SessionStrategy', function() {
});

describe('handling a request with a login session that encounters an error when deserializing', function() {
var strategy = new SessionStrategy(function(user, req, done) {
done(new Error('something went wrong'));
});

var request, error;

before(function(done) {
Expand All @@ -205,9 +213,6 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance.deserializeUser = function(user, req, done) {
done(new Error('something went wrong'));
};
req._passport.session = {};
req._passport.session.user = '123456';
})
Expand Down

0 comments on commit 13c550c

Please sign in to comment.