Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jaredhanson/passport
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.4.1
Choose a base ref
...
head repository: jaredhanson/passport
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.5.0
Choose a head ref

Commits on Jun 10, 2020

  1. Update README.

    jaredhanson committed Jun 10, 2020
    Copy the full SHA
    8d7cdf6 View commit details
  2. Update README.

    jaredhanson committed Jun 10, 2020
    Copy the full SHA
    1fd591d View commit details

Commits on Dec 11, 2020

  1. Add sponsors.

    jaredhanson committed Dec 11, 2020
    Copy the full SHA
    f8064a1 View commit details
  2. Fix image link.

    jaredhanson committed Dec 11, 2020
    Copy the full SHA
    3f217a1 View commit details
  3. Fix styling.

    jaredhanson committed Dec 11, 2020
    Copy the full SHA
    8d5e3f3 View commit details
  4. Fix styling.

    jaredhanson committed Dec 11, 2020
    Copy the full SHA
    624a399 View commit details

Commits on Dec 17, 2020

  1. Update SPONSORS.

    jaredhanson committed Dec 17, 2020
    Copy the full SHA
    9b9dbaa View commit details

Commits on May 2, 2021

  1. Add WorkOS to sponsors.

    jaredhanson committed May 2, 2021
    Copy the full SHA
    8eb97ae View commit details
  2. Update README.

    jaredhanson committed May 2, 2021
    Copy the full SHA
    22c36bc View commit details
  3. Update sponsors.

    jaredhanson committed May 2, 2021
    Copy the full SHA
    20154c6 View commit details
  4. Update sponsors.

    jaredhanson committed May 2, 2021
    Copy the full SHA
    2a52160 View commit details

Commits on May 6, 2021

  1. Update README.md

    pareek-naren authored May 6, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    02aa577 View commit details

Commits on May 7, 2021

  1. Merge pull request #804 from pareek-narendra/master

    On behalf of LoginRadius, Added SignUp URL at the LoginRadius Link
    jaredhanson authored May 7, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7f2a3da View commit details

Commits on Jun 29, 2021

  1. Update funding.

    jaredhanson committed Jun 29, 2021
    Copy the full SHA
    290622a View commit details
  2. Copy the full SHA
    b220766 View commit details

Commits on Sep 22, 2021

  1. Copy the full SHA
    fd07473 View commit details
  2. Copy the full SHA
    146197f View commit details

Commits on Sep 23, 2021

  1. Copy the full SHA
    4d5bed6 View commit details
  2. Copy the full SHA
    862a45e View commit details
  3. Copy the full SHA
    6861724 View commit details
  4. Copy the full SHA
    7f41957 View commit details
  5. Copy the full SHA
    e20e3f9 View commit details
  6. Copy the full SHA
    761a8d2 View commit details
  7. Copy the full SHA
    c5bc12d View commit details
  8. Merge pull request #848 from jaredhanson/no-monkeypatch

    No monkeypatch
    jaredhanson authored Sep 23, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    076850c View commit details
  9. Add CHANGELOG.

    jaredhanson committed Sep 23, 2021
    Copy the full SHA
    1a3c258 View commit details
  10. Copy the full SHA
    b524515 View commit details
  11. Set version in CHANGELOG.

    jaredhanson committed Sep 23, 2021
    Copy the full SHA
    dbf2ca6 View commit details
  12. 0.5.0

    jaredhanson committed Sep 23, 2021
    Copy the full SHA
    f5dc1da View commit details
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
github: jaredhanson
patreon: jaredhanson
ko_fi: jaredhanson
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2021-09-23
### Changed

- `initialize()` middleware extends request with `login()`, `logIn()`,
`logout()`, `logOut()`, `isAuthenticated()`, and `isUnauthenticated()`
functions.

### Removed

- `login()`, `logIn()`, `logout()`, `logOut()`, `isAuthenticated()`, and
`isUnauthenticated()` functions no longer added to `http.IncomingMessage.prototype`.

### Fixed

- `userProperty` option to `initialize()` middleware only affects the current
request, rather than all requests processed via singleton Passport instance,
eliminating a race condition in situations where `initialize()` middleware is
used multiple times in an application with `userProperty` set to different
values.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,20 @@ allows application-level decisions to be made by the developer. The API is
simple: you provide Passport a request to authenticate, and Passport provides
hooks for controlling what occurs when authentication succeeds or fails.

---

<p align="center">
<sup>Sponsors</sup>
<br>
<a href="https://www.loginradius.com/?utm_source=sponsership&utm_medium=github&utm_campaign=jaredhanson"><img src="https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/loginradius.png"></a><br/>
<a href="https://accounts.loginradius.com/auth.aspx?action=register?utm_source=sponsership&utm_medium=github&utm_campaign=jaredhanson">LoginRadius</a> is built for the developer community to integrate robust Authentication and Single Sign-On in just a few lines of code.<br/><a href="https://accounts.loginradius.com/auth.aspx?action=register?utm_source=sponsership&utm_medium=github&utm_campaign=jaredhanson">FREE Signup</a>
<br><br>
<a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=passport_js&utm_source=github"><img src="https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/workos.png"></a><br/>
<a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=passport_js&utm_source=github"><b>Your app, enterprise-ready.</b><br/>Start selling to enterprise customers with just a few lines of code. Add Single Sign-On (and more) in minutes instead of months.</a>
</p>

---

Status:
[![Build](https://travis-ci.org/jaredhanson/passport.svg?branch=master)](https://travis-ci.org/jaredhanson/passport)
[![Coverage](https://coveralls.io/repos/jaredhanson/passport/badge.svg?branch=master)](https://coveralls.io/r/jaredhanson/passport)
7 changes: 7 additions & 0 deletions SPONSORS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## Gold Sponsors

[![LoginRadius](https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/loginradius.png)](https://www.loginradius.com/)
<br><br>
[![WorkOS](https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/workos.png)](https://workos.com/)

## Sponsors

- [CodePilot.ai](https://codepilot.ai/)
- Kelly Burke
- [Matt Miller](https://mmiller.me/)
5 changes: 1 addition & 4 deletions lib/authenticator.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ function Authenticator() {
this._deserializers = [];
this._infoTransformers = [];
this._framework = null;
this._userProperty = 'user';

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

@@ -128,8 +127,6 @@ Authenticator.prototype.framework = function(fw) {
*/
Authenticator.prototype.initialize = function(options) {
options = options || {};
this._userProperty = options.userProperty || 'user';

return this._framework.initialize(this, options);
};

19 changes: 1 addition & 18 deletions lib/framework/connect.js
Original file line number Diff line number Diff line change
@@ -8,32 +8,15 @@ var initialize = require('../middleware/initialize')
* Framework support for Connect/Express.
*
* This module provides support for using Passport with Express. It exposes
* middleware that conform to the `fn(req, res, next)` signature and extends
* Node's built-in HTTP request object with useful authentication-related
* functions.
* middleware that conform to the `fn(req, res, next)` signature.
*
* @return {Object}
* @api protected
*/
exports = module.exports = function() {

// HTTP extensions.
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;
};
23 changes: 3 additions & 20 deletions lib/http/request.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/**
* Module dependencies.
*/
//var http = require('http')
// , req = http.IncomingMessage.prototype;


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

/**
@@ -35,10 +28,7 @@ req.logIn = function(user, options, done) {
}
options = options || {};

var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
var property = this._userProperty || 'user';
var session = (options.session === undefined) ? true : options.session;

this[property] = user;
@@ -63,10 +53,7 @@ req.logIn = function(user, options, done) {
*/
req.logout =
req.logOut = function() {
var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
var property = this._userProperty || 'user';

this[property] = null;
if (this._passport) {
@@ -81,11 +68,7 @@ req.logOut = function() {
* @api public
*/
req.isAuthenticated = function() {
var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}

var property = this._userProperty || 'user';
return (this[property]) ? true : false;
};

6 changes: 0 additions & 6 deletions lib/middleware/authenticate.js
Original file line number Diff line number Diff line change
@@ -92,12 +92,6 @@ 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 = [];

25 changes: 19 additions & 6 deletions lib/middleware/initialize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Module dependencies.
*/
var IncomingMessageExt = require('../http/request');


/**
* Passport initialization.
*
@@ -39,17 +45,24 @@
* @return {Function}
* @api public
*/
module.exports = function initialize(passport) {
module.exports = function initialize(passport, options) {
options = options || {};

return function initialize(req, res, next) {
req.login =
req.logIn = IncomingMessageExt.logIn;
req.logout =
req.logOut = IncomingMessageExt.logOut;
req.isAuthenticated = IncomingMessageExt.isAuthenticated;
req.isUnauthenticated = IncomingMessageExt.isUnauthenticated;

if (options.userProperty) {
req._userProperty = options.userProperty;
}

req._passport = {};
req._passport.instance = passport;

if (req.session && req.session[passport._key]) {
// load data from existing session
req._passport.session = req.session[passport._key];
}

next();
};
};
15 changes: 8 additions & 7 deletions lib/sessionmanager.js
Original file line number Diff line number Diff line change
@@ -15,22 +15,23 @@ SessionManager.prototype.logIn = function(req, user, cb) {
if (err) {
return cb(err);
}
if (!req._passport.session) {
req._passport.session = {};
}
req._passport.session.user = obj;
// TODO: Error if session isn't available here.
if (!req.session) {
req.session = {};
}
req.session[self._key] = req._passport.session;
if (!req.session[self._key]) {
req.session[self._key] = {};
}
req.session[self._key].user = obj;
cb();
});
}

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

cb && cb();
}

10 changes: 5 additions & 5 deletions lib/strategies/session.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ function SessionStrategy(options, deserializeUser) {

Strategy.call(this);
this.name = 'session';
this._key = options.key || 'passport';
this._deserializeUser = deserializeUser;
}

@@ -47,8 +48,8 @@ SessionStrategy.prototype.authenticate = function(req, options) {

var self = this,
su;
if (req._passport.session) {
su = req._passport.session.user;
if (req.session[this._key]) {
su = req.session[this._key].user;
}

if (su || su === 0) {
@@ -60,10 +61,9 @@ SessionStrategy.prototype.authenticate = function(req, options) {
this._deserializeUser(su, req, function(err, user) {
if (err) { return self.error(err); }
if (!user) {
delete req._passport.session.user;
delete req.session[self._key].user;
} else {
// TODO: Remove instance access
var property = req._passport.instance._userProperty || 'user';
var property = req._userProperty || 'user';
req[property] = user;
}
self.pass();
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passport",
"version": "0.4.1",
"version": "0.5.0",
"description": "Simple, unobtrusive authentication for Node.js.",
"keywords": [
"express",
@@ -22,6 +22,10 @@
"bugs": {
"url": "http://github.com/jaredhanson/passport/issues"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/jaredhanson"
},
"license": "MIT",
"licenses": [
{
Binary file added sponsors/loginradius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sponsors/workos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 9 additions & 16 deletions test/authenticator.middleware.test.js
Original file line number Diff line number Diff line change
@@ -35,8 +35,8 @@ describe('Authenticator', function() {
expect(error).to.be.undefined;
});

it('should set user property on authenticator', function() {
expect(passport._userProperty).to.equal('user');
it('should not set user property on request', function() {
expect(request._userProperty).to.be.undefined;
});

it('should not initialize namespace within session', function() {
@@ -48,10 +48,6 @@ describe('Authenticator', function() {
expect(request._passport.instance).to.be.an.instanceOf(Authenticator);
expect(request._passport.instance).to.equal(passport);
});

it('should not expose session storage on internal request property', function() {
expect(request._passport.session).to.be.undefined;
});
});

describe('handling a request with custom user property', function() {
@@ -75,8 +71,8 @@ describe('Authenticator', function() {
expect(error).to.be.undefined;
});

it('should set user property on authenticator', function() {
expect(passport._userProperty).to.equal('currentUser');
it('should set user property on request', function() {
expect(request._userProperty).to.equal('currentUser');
});

it('should not initialize namespace within session', function() {
@@ -88,10 +84,6 @@ describe('Authenticator', function() {
expect(request._passport.instance).to.be.an.instanceOf(Authenticator);
expect(request._passport.instance).to.equal(passport);
});

it('should not expose session storage on internal request property', function() {
expect(request._passport.session).to.be.undefined;
});
});

});
@@ -278,8 +270,9 @@ describe('Authenticator', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.next(function(err) {
error = err;
@@ -298,8 +291,8 @@ describe('Authenticator', function() {
});

it('should maintain session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.equal('123456');
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.equal('123456');
});
});

132 changes: 90 additions & 42 deletions test/http/request.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* global describe, it, expect, before */
/* jshint expr: true */

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

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


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

// TODO: Test that these are extended by initialize/authenticate
/*
describe('prototoype', function() {
var req = new http.IncomingMessage();
@@ -30,16 +30,21 @@ describe('http.ServerRequest', function() {
expect(req.isUnauthenticated).to.be.an('function');
});
});
*/

describe('#login', function() {

describe('not establishing a session', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req.session['passport'] = {};

var error;

@@ -68,18 +73,22 @@ describe('http.ServerRequest', function() {
});

it('should not serialize user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

describe('not establishing a session and setting custom user property', function() {
var passport = new Passport();
passport._userProperty = 'currentUser';

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req.session['passport'] = {};
req._userProperty = 'currentUser';

var error;

@@ -112,17 +121,21 @@ describe('http.ServerRequest', function() {
});

it('should not serialize user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

describe('not establishing a session and invoked without a callback', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req.session['passport'] = {};

var user = { id: '1', username: 'root' };
req.login(user, { session: false });
@@ -139,12 +152,15 @@ describe('http.ServerRequest', function() {
});

it('should not serialize user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

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

var error;

@@ -179,10 +195,13 @@ describe('http.ServerRequest', function() {
done(null, user.id);
});

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};

var error;

@@ -211,7 +230,7 @@ describe('http.ServerRequest', function() {
});

it('should serialize user', function() {
expect(req._passport.session.user).to.equal('1');
expect(req.session['passport'].user).to.equal('1');
});
});

@@ -220,12 +239,15 @@ describe('http.ServerRequest', function() {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport._userProperty = 'currentUser';

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req._userProperty = 'currentUser';

var error;

@@ -258,7 +280,7 @@ describe('http.ServerRequest', function() {
});

it('should serialize user', function() {
expect(req._passport.session.user).to.equal('1');
expect(req.session['passport'].user).to.equal('1');
});
});

@@ -268,10 +290,14 @@ describe('http.ServerRequest', function() {
done(new Error('something went wrong'));
});

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req.session['passport'] = {};

var error;

@@ -299,12 +325,13 @@ describe('http.ServerRequest', function() {
});

it('should not serialize user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

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

it('should throw an exception', function() {
@@ -320,10 +347,12 @@ describe('http.ServerRequest', function() {
done(null, user.id);
});

var req = new http.IncomingMessage();
var req = new Object();
req.login = request.login;
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req.session = {};
req.session['passport'] = {};

var user = { id: '1', username: 'root' };

@@ -342,12 +371,16 @@ describe('http.ServerRequest', function() {
describe('existing session', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
var req = new Object();
req.logout = request.logout;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req.user = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = passport;
req._passport.session = {};
req._passport.session.user = '1';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '1';

req.logout();

@@ -361,20 +394,24 @@ describe('http.ServerRequest', function() {
});

it('should clear serialized user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

describe('existing session and clearing custom user property', function() {
var passport = new Passport();

var req = new http.IncomingMessage();
var req = new Object();
req.logout = request.logout;
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req.currentUser = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = passport;
req._passport.instance._userProperty = 'currentUser';
req._passport.session = {};
req._passport.session.user = '1';
req._userProperty = 'currentUser';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '1';

req.logout();

@@ -388,12 +425,15 @@ describe('http.ServerRequest', function() {
});

it('should clear serialized user', function() {
expect(req._passport.session.user).to.be.undefined;
expect(req.session['passport'].user).to.be.undefined;
});
});

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

req.logout();
@@ -414,7 +454,9 @@ describe('http.ServerRequest', function() {
describe('#isAuthenticated', function() {

describe('with a user', function() {
var req = new http.IncomingMessage();
var req = new Object();
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req.user = { id: '1', username: 'root' };

it('should be authenticated', function() {
@@ -424,11 +466,13 @@ describe('http.ServerRequest', function() {
});

describe('with a user set on custom property', function() {
var req = new http.IncomingMessage();
var req = new Object();
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req.currentUser = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = {};
req._passport.instance._userProperty = 'currentUser';
req._userProperty = 'currentUser';

it('should be authenticated', function() {
expect(req.isAuthenticated()).to.be.true;
@@ -437,7 +481,9 @@ describe('http.ServerRequest', function() {
});

describe('without a user', function() {
var req = new http.IncomingMessage();
var req = new Object();
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;

it('should not be authenticated', function() {
expect(req.isAuthenticated()).to.be.false;
@@ -446,7 +492,9 @@ describe('http.ServerRequest', function() {
});

describe('with a null user', function() {
var req = new http.IncomingMessage();
var req = new Object();
req.isAuthenticated = request.isAuthenticated;
req.isUnauthenticated = request.isUnauthenticated;
req.user = null;

it('should not be authenticated', function() {
20 changes: 0 additions & 20 deletions test/middleware/initialize.test.js
Original file line number Diff line number Diff line change
@@ -37,10 +37,6 @@ describe('middleware/initialize', function() {
expect(request._passport.instance).to.be.an.instanceOf(Passport);
expect(request._passport.instance).to.equal(passport);
});

it('should not expose empty object as session storage on internal request property', function() {
expect(request._passport.session).to.be.undefined;
});
});

describe('handling a request with a new session', function() {
@@ -74,10 +70,6 @@ describe('middleware/initialize', function() {
expect(request._passport.instance).to.be.an.instanceOf(Passport);
expect(request._passport.instance).to.equal(passport);
});

it('should not expose session storage on internal request property', function() {
expect(request._passport.session).to.be.undefined;
});
});

describe('handling a request with an existing session', function() {
@@ -115,12 +107,6 @@ describe('middleware/initialize', function() {
expect(request._passport.instance).to.be.an.instanceOf(Passport);
expect(request._passport.instance).to.equal(passport);
});

it('should expose session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(1);
expect(request._passport.session.user).to.equal('123456');
});
});

describe('handling a request with an existing session using custom session key', function() {
@@ -159,12 +145,6 @@ describe('middleware/initialize', function() {
expect(request._passport.instance).to.be.an.instanceOf(Passport);
expect(request._passport.instance).to.equal(passport);
});

it('should expose session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(1);
expect(request._passport.session.user).to.equal('123456');
});
});

});
18 changes: 10 additions & 8 deletions test/strategies/session.pause.test.js
Original file line number Diff line number Diff line change
@@ -33,8 +33,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate({ pauseStream: true });
});
@@ -53,8 +54,8 @@ describe('SessionStrategy', function() {
});

it('should maintain session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.equal('123456');
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.equal('123456');
});

it('should pause request', function() {
@@ -95,8 +96,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate({ pauseStream: true });
});
@@ -114,8 +116,8 @@ describe('SessionStrategy', function() {
});

it('should remove user from session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.be.undefined;
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.be.undefined;
});

it('should pause request', function() {
46 changes: 26 additions & 20 deletions test/strategies/session.test.js
Original file line number Diff line number Diff line change
@@ -26,7 +26,8 @@ describe('SessionStrategy', function() {
request = req;

req._passport = {};
req._passport.session = {};
req.session = {};
req.session['passport'] = {};
})
.authenticate();
});
@@ -58,8 +59,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate();
});
@@ -74,8 +76,8 @@ describe('SessionStrategy', function() {
});

it('should maintain session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.equal('123456');
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.equal('123456');
});
});

@@ -97,8 +99,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = 0;
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = 0;
})
.authenticate();
});
@@ -113,8 +116,8 @@ describe('SessionStrategy', function() {
});

it('should maintain session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.equal(0);
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.equal(0);
});
});

@@ -136,8 +139,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate();
});
@@ -151,8 +155,8 @@ describe('SessionStrategy', function() {
});

it('should remove user from session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.be.undefined;
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.be.undefined;
});
});

@@ -174,9 +178,10 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance._userProperty = 'currentUser';
req._passport.session = {};
req._passport.session.user = '123456';
req._userProperty = 'currentUser';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate();
});
@@ -213,8 +218,9 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.session = {};
req._passport.session.user = '123456';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
})
.authenticate();
});
@@ -229,8 +235,8 @@ describe('SessionStrategy', function() {
});

it('should maintain session', function() {
expect(request._passport.session).to.be.an('object');
expect(request._passport.session.user).to.equal('123456');
expect(request.session['passport']).to.be.an('object');
expect(request.session['passport'].user).to.equal('123456');
});
});