pax_global_header00006660000000000000000000000064142417141710014514gustar00rootroot0000000000000052 comment=c33067bc5aa81a6dd827076d810bf788bb6acac7 passport-0.6.0/000077500000000000000000000000001424171417100133725ustar00rootroot00000000000000passport-0.6.0/.github/000077500000000000000000000000001424171417100147325ustar00rootroot00000000000000passport-0.6.0/.github/FUNDING.yml000066400000000000000000000000741424171417100165500ustar00rootroot00000000000000github: jaredhanson patreon: jaredhanson ko_fi: jaredhanson passport-0.6.0/.github/ISSUE_TEMPLATE.md000066400000000000000000000035511424171417100174430ustar00rootroot00000000000000** READ THIS FIRST! ** #### Are you looking for help? Reminder: The issue tracker is not a support forum. Issues should only be filed in this project once they are able to be reproduced and confirmed as a flaw in the software or incorrect information in associated documention. If you are encountering problems integrating this module into your application, please post a question on the [discussion forum](https://github.com/passport/discuss) rather than filing an issue. #### Is this a security issue? Do not open issues that might have security implications. Potential security vulnerabilities should be reported privately to jaredhanson@gmail.com. Once any vulerabilities have been repaired, the details will be disclosed publicly in a responsible manner. This also allows time for coordinating with affected parties in order to mitigate negative consequences. If neither of the above two scenarios apply to your situation, you should open an issue. Delete this paragraph and the text above, and fill in the information requested below. ### Expected behavior ### Actual behavior ### Steps to reproduce ```js // Format code using Markdown code blocks ``` ### Environment * Operating System: * Node version: * passport version: passport-0.6.0/.github/PULL_REQUEST_TEMPLATE.md000066400000000000000000000035621424171417100205410ustar00rootroot00000000000000** READ THIS FIRST! ** #### Are you implementing a new feature? Requests for new features should first be discussed on the [developer forum](https://github.com/passport/develop). This allows the community to gather feedback and assess whether or not there is an existing way to achieve the desired functionality. If it is determined that a new feature needs to be implemented, include a link to the relevant discussion along with the pull request. #### Is this a security patch? Do not open pull requests that might have security implications. Potential security vulnerabilities should be reported privately to jaredhanson@gmail.com. Once any vulerabilities have been repaired, the details will be disclosed publicly in a responsible manner. This also allows time for coordinating with affected parties in order to mitigate negative consequences. If neither of the above two scenarios apply to your situation, you should open a pull request. Delete this paragraph and the text above, and fill in the information requested below. ### Checklist - [ ] I have read the [CONTRIBUTING](https://github.com/jaredhanson/passport/blob/master/CONTRIBUTING.md) guidelines. - [ ] I have added test cases which verify the correct operation of this feature or patch. - [ ] I have added documentation pertaining to this feature or patch. - [ ] The automated test suite (`$ make test`) executes successfully. - [ ] The automated code linting (`$ make lint`) executes successfully. passport-0.6.0/.github/workflows/000077500000000000000000000000001424171417100167675ustar00rootroot00000000000000passport-0.6.0/.github/workflows/node.yml000066400000000000000000000017111424171417100204370ustar00rootroot00000000000000# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions name: Node.js CI on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: - '17' - '16' - '14' - '12' - '10' - '8' - '6' - '4' # - '3' # io.js # - '2' # io.js # - '1' # io.js - '0.12' - '0.10' # - '0.8' steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test passport-0.6.0/.gitignore000066400000000000000000000001131424171417100153550ustar00rootroot00000000000000docs/ reports/ # Mac OS X .DS_Store # Node.js node_modules npm-debug.log passport-0.6.0/.jshintrc000066400000000000000000000004531424171417100152210ustar00rootroot00000000000000{ "node": true, "bitwise": true, "camelcase": true, "curly": true, "forin": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, "noempty": true, "nonew": true, "quotmark": "single", "undef": true, "unused": true, "trailing": true, "laxcomma": true } passport-0.6.0/.npmignore000066400000000000000000000001441424171417100153700ustar00rootroot00000000000000CONTRIBUTING.md Makefile SPONSORS.md docs/ examples/ reports/ test/ .github/ .jshintrc .travis.yml passport-0.6.0/.travis.yml000066400000000000000000000005411424171417100155030ustar00rootroot00000000000000language: "node_js" node_js: - "13" - "12" - "11" - "10" - "9" - "8" - "7" - "6" - "5" - "4" - "3" # io.js - "2" # io.js - "1" # io.js - "0.12" - "0.10" # - "0.8" before_install: - "npm install make-node@0.3.x -g" - "preinstall-compat" script: - "make test-cov" after_success: - "make report-cov" sudo: false passport-0.6.0/CHANGELOG.md000066400000000000000000000044461424171417100152130ustar00rootroot00000000000000# 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). ## [Unreleased] ## [0.6.0] - 2022-05-20 ### Security - Improved robustness against session fixation attacks in cases where there is physical access to the same system or the application is susceptible to cross-site scripting (XSS). ## [0.5.3] - 2022-05-16 ### Fixed - `initialize()` middleware extends request with `login()`, `logIn()`, `logout()`, `logOut()`, `isAuthenticated()`, and `isUnauthenticated()` functions again, reverting change from 0.5.1. ## [0.5.2] - 2021-12-16 ### Fixed - Introduced a compatibility layer for strategies that depend directly on `passport@0.4.x` or earlier (such as `passport-azure-ad`), which were broken by the removal of private variables in `passport@0.5.1`. ## [0.5.1] - 2021-12-15 ### Added - Informative error message in session strategy if session support is not available. ### Changed - `authenticate()` middleware, rather than `initialize()` middleware, extends request with `login()`, `logIn()`, `logout()`, `logOut()`, `isAuthenticated()`, and `isUnauthenticated()` functions. ## [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. [Unreleased]: https://github.com/jaredhanson/passport/compare/v0.6.0...HEAD [0.6.0]: https://github.com/jaredhanson/passport/compare/v0.5.3...v0.6.0 [0.5.3]: https://github.com/jaredhanson/passport/compare/v0.5.2...v0.5.3 [0.5.2]: https://github.com/jaredhanson/passport/compare/v0.5.1...v0.5.2 [0.5.1]: https://github.com/jaredhanson/passport/compare/v0.5.0...v0.5.1 passport-0.6.0/CONTRIBUTING.md000066400000000000000000000006331424171417100156250ustar00rootroot00000000000000## Contributing ### Tests The test suite is located in the `test/` directory. All new features are expected to have corresponding test cases with complete code coverage. Patches that increase test coverage are happily accepted. Ensure that the test suite passes by executing: ```bash $ make test ``` Coverage reports can be generated and viewed by executing: ```bash $ make test-cov $ make view-cov ``` passport-0.6.0/LICENSE000066400000000000000000000020741424171417100144020ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2011-2021 Jared Hanson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. passport-0.6.0/Makefile000066400000000000000000000006301424171417100150310ustar00rootroot00000000000000include node_modules/make-node/main.mk SOURCES = lib/*.js lib/**/*.js TESTS = test/*.test.js test/**/*.test.js LCOVFILE = ./reports/coverage/lcov.info MOCHAFLAGS = --require ./test/bootstrap/node view-docs: open ./docs/index.html view-cov: open ./reports/coverage/lcov-report/index.html clean: clean-docs clean-cov -rm -r $(REPORTSDIR) clobber: clean -rm -r node_modules .PHONY: clean clobber passport-0.6.0/README.md000066400000000000000000000204211424171417100146500ustar00rootroot00000000000000[![passport banner](http://cdn.auth0.com/img/passport-banner-github.png)](http://passportjs.org) # Passport Passport is [Express](http://expressjs.com/)-compatible authentication middleware for [Node.js](http://nodejs.org/). Passport's sole purpose is to authenticate requests, which it does through an extensible set of plugins known as _strategies_. Passport does not mount routes or assume any particular database schema, which maximizes flexibility and 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. ---

Sponsors

Your app, enterprise-ready.
Start selling to enterprise customers with just a few lines of code. Add Single Sign-On (and more) in minutes instead of months.


--- 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) [![Dependencies](https://david-dm.org/jaredhanson/passport.svg)](https://david-dm.org/jaredhanson/passport) ## Install ``` $ npm install passport ``` ## Usage #### Strategies Passport uses the concept of strategies to authenticate requests. Strategies can range from verifying username and password credentials, delegated authentication using [OAuth](http://oauth.net/) (for example, via [Facebook](http://www.facebook.com/) or [Twitter](http://twitter.com/)), or federated authentication using [OpenID](http://openid.net/). Before authenticating requests, the strategy (or strategies) used by an application must be configured. ```javascript passport.use(new LocalStrategy( function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (!user.verifyPassword(password)) { return done(null, false); } return done(null, user); }); } )); ``` There are 480+ strategies. Find the ones you want at: [passportjs.org](http://passportjs.org) #### Sessions Passport will maintain persistent login sessions. In order for persistent sessions to work, the authenticated user must be serialized to the session, and deserialized when subsequent requests are made. Passport does not impose any restrictions on how your user records are stored. Instead, you provide functions to Passport which implements the necessary serialization and deserialization logic. In a typical application, this will be as simple as serializing the user ID, and finding the user by ID when deserializing. ```javascript passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function (err, user) { done(err, user); }); }); ``` #### Middleware To use Passport in an [Express](http://expressjs.com/) or [Connect](http://senchalabs.github.com/connect/)-based application, configure it with the required `passport.initialize()` middleware. If your application uses persistent login sessions (recommended, but not required), `passport.session()` middleware must also be used. ```javascript var app = express(); app.use(require('serve-static')(__dirname + '/../../public')); app.use(require('cookie-parser')()); app.use(require('body-parser').urlencoded({ extended: true })); app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true })); app.use(passport.initialize()); app.use(passport.session()); ``` #### Authenticate Requests Passport provides an `authenticate()` function, which is used as route middleware to authenticate requests. ```javascript app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); }); ``` ## Strategies Passport has a comprehensive set of **over 480** authentication strategies covering social networking, enterprise integration, API services, and more. ## Search all strategies There is a **Strategy Search** at [passportjs.org](http://passportjs.org) The following table lists commonly used strategies: |Strategy | Protocol |Developer | |---------------------------------------------------------------|--------------------------|------------------------------------------------| |[Local](https://github.com/jaredhanson/passport-local) | HTML form |[Jared Hanson](https://github.com/jaredhanson) | |[OpenID](https://github.com/jaredhanson/passport-openid) | OpenID |[Jared Hanson](https://github.com/jaredhanson) | |[BrowserID](https://github.com/jaredhanson/passport-browserid) | BrowserID |[Jared Hanson](https://github.com/jaredhanson) | |[Facebook](https://github.com/jaredhanson/passport-facebook) | OAuth 2.0 |[Jared Hanson](https://github.com/jaredhanson) | |[Google](https://github.com/jaredhanson/passport-google) | OpenID |[Jared Hanson](https://github.com/jaredhanson) | |[Google](https://github.com/jaredhanson/passport-google-oauth) | OAuth / OAuth 2.0 |[Jared Hanson](https://github.com/jaredhanson) | |[Twitter](https://github.com/jaredhanson/passport-twitter) | OAuth |[Jared Hanson](https://github.com/jaredhanson) | |[Azure Active Directory](https://github.com/AzureAD/passport-azure-ad) | OAuth 2.0 / OpenID / SAML |[Azure](https://github.com/azuread) | ## Examples - For a complete, working example, refer to the [example](https://github.com/passport/express-4.x-local-example) that uses [passport-local](https://github.com/jaredhanson/passport-local). - **Local Strategy**: Refer to the following tutorials for setting up user authentication via LocalStrategy (`passport-local`): - Mongo - Express v3x - [Tutorial](http://mherman.org/blog/2016/09/25/node-passport-and-postgres/#.V-govpMrJE5) / [working example](https://github.com/mjhea0/passport-local-knex) - Express v4x - [Tutorial](http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/) / [working example](https://github.com/mjhea0/passport-local-express4) - Postgres - [Tutorial](http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/) / [working example](https://github.com/mjhea0/passport-local-express4) - **Social Authentication**: Refer to the following tutorials for setting up various social authentication strategies: - Express v3x - [Tutorial](http://mherman.org/blog/2013/11/10/social-authentication-with-passport-dot-js/) / [working example](https://github.com/mjhea0/passport-examples) - Express v4x - [Tutorial](http://mherman.org/blog/2015/09/26/social-authentication-in-node-dot-js-with-passport) / [working example](https://github.com/mjhea0/passport-social-auth) ## Related Modules - [Locomotive](https://github.com/jaredhanson/locomotive) — Powerful MVC web framework - [OAuthorize](https://github.com/jaredhanson/oauthorize) — OAuth service provider toolkit - [OAuth2orize](https://github.com/jaredhanson/oauth2orize) — OAuth 2.0 authorization server toolkit - [connect-ensure-login](https://github.com/jaredhanson/connect-ensure-login) — middleware to ensure login sessions The [modules](https://github.com/jaredhanson/passport/wiki/Modules) page on the [wiki](https://github.com/jaredhanson/passport/wiki) lists other useful modules that build upon or integrate with Passport. ## License [The MIT License](http://opensource.org/licenses/MIT) Copyright (c) 2011-2021 Jared Hanson <[https://www.jaredhanson.me/](https://www.jaredhanson.me/)> passport-0.6.0/SPONSORS.md000066400000000000000000000007571424171417100152130ustar00rootroot00000000000000## Gold Sponsors [![WorkOS](https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/workos.png)](https://workos.com/)
[![Snyk](https://raw.githubusercontent.com/jaredhanson/passport/master/sponsors/snyk.png)](https://snyk.io/) ## Sponsors - [CodePilot.ai](https://codepilot.ai/) - [Jeremy Combs](https://github.com/jmcombs) - [Gadget](https://gadget.dev/) - Kelly Burke - [Matt Miller](https://mmiller.me/) ## Past Sponsors - [LoginRadius](https://www.loginradius.com/) passport-0.6.0/lib/000077500000000000000000000000001424171417100141405ustar00rootroot00000000000000passport-0.6.0/lib/authenticator.js000066400000000000000000000320601424171417100173510ustar00rootroot00000000000000/** * Module dependencies. */ var SessionStrategy = require('./strategies/session') , SessionManager = require('./sessionmanager'); /** * `Authenticator` constructor. * * @api public */ function Authenticator() { this._key = 'passport'; this._strategies = {}; this._serializers = []; this._deserializers = []; this._infoTransformers = []; this._framework = null; this.init(); } /** * Initialize authenticator. * * @api protected */ Authenticator.prototype.init = function() { this.framework(require('./framework/connect')()); this.use(new SessionStrategy({ key: this._key }, this.deserializeUser.bind(this))); this._sm = new SessionManager({ key: this._key }, this.serializeUser.bind(this)); }; /** * Utilize the given `strategy` with optional `name`, overridding the strategy's * default name. * * Examples: * * passport.use(new TwitterStrategy(...)); * * passport.use('api', new http.BasicStrategy(...)); * * @param {String|Strategy} name * @param {Strategy} strategy * @return {Authenticator} for chaining * @api public */ Authenticator.prototype.use = function(name, strategy) { if (!strategy) { strategy = name; name = strategy.name; } if (!name) { throw new Error('Authentication strategies must have a name'); } this._strategies[name] = strategy; return this; }; /** * Un-utilize the `strategy` with given `name`. * * In typical applications, the necessary authentication strategies are static, * configured once and always available. As such, there is often no need to * invoke this function. * * However, in certain situations, applications may need dynamically configure * and de-configure authentication strategies. The `use()`/`unuse()` * combination satisfies these scenarios. * * Examples: * * passport.unuse('legacy-api'); * * @param {String} name * @return {Authenticator} for chaining * @api public */ Authenticator.prototype.unuse = function(name) { delete this._strategies[name]; return this; }; /** * Setup Passport to be used under framework. * * By default, Passport exposes middleware that operate using Connect-style * middleware using a `fn(req, res, next)` signature. Other popular frameworks * have different expectations, and this function allows Passport to be adapted * to operate within such environments. * * If you are using a Connect-compatible framework, including Express, there is * no need to invoke this function. * * Examples: * * passport.framework(require('hapi-passport')()); * * @param {Object} name * @return {Authenticator} for chaining * @api public */ Authenticator.prototype.framework = function(fw) { this._framework = fw; return this; }; /** * Passport's primary initialization middleware. * * This middleware must be in use by the Connect/Express application for * Passport to operate. * * Options: * - `userProperty` Property to set on `req` upon login, defaults to _user_ * * Examples: * * app.use(passport.initialize()); * * app.use(passport.initialize({ userProperty: 'currentUser' })); * * @param {Object} options * @return {Function} middleware * @api public */ Authenticator.prototype.initialize = function(options) { options = options || {}; return this._framework.initialize(this, options); }; /** * Middleware that will authenticate a request using the given `strategy` name, * with optional `options` and `callback`. * * Examples: * * passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' })(req, res); * * passport.authenticate('local', function(err, user) { * if (!user) { return res.redirect('/login'); } * res.end('Authenticated!'); * })(req, res); * * passport.authenticate('basic', { session: false })(req, res); * * app.get('/auth/twitter', passport.authenticate('twitter'), function(req, res) { * // request will be redirected to Twitter * }); * app.get('/auth/twitter/callback', passport.authenticate('twitter'), function(req, res) { * res.json(req.user); * }); * * @param {String} strategy * @param {Object} options * @param {Function} callback * @return {Function} middleware * @api public */ Authenticator.prototype.authenticate = function(strategy, options, callback) { return this._framework.authenticate(this, strategy, options, callback); }; /** * Middleware that will authorize a third-party account using the given * `strategy` name, with optional `options`. * * If authorization is successful, the result provided by the strategy's verify * callback will be assigned to `req.account`. The existing login session and * `req.user` will be unaffected. * * This function is particularly useful when connecting third-party accounts * to the local account of a user that is currently authenticated. * * Examples: * * passport.authorize('twitter-authz', { failureRedirect: '/account' }); * * @param {String} strategy * @param {Object} options * @return {Function} middleware * @api public */ Authenticator.prototype.authorize = function(strategy, options, callback) { options = options || {}; options.assignProperty = 'account'; var fn = this._framework.authorize || this._framework.authenticate; return fn(this, strategy, options, callback); }; /** * Middleware that will restore login state from a session. * * Web applications typically use sessions to maintain login state between * requests. For example, a user will authenticate by entering credentials into * a form which is submitted to the server. If the credentials are valid, a * login session is established by setting a cookie containing a session * identifier in the user's web browser. The web browser will send this cookie * in subsequent requests to the server, allowing a session to be maintained. * * If sessions are being utilized, and a login session has been established, * this middleware will populate `req.user` with the current user. * * Note that sessions are not strictly required for Passport to operate. * However, as a general rule, most web applications will make use of sessions. * An exception to this rule would be an API server, which expects each HTTP * request to provide credentials in an Authorization header. * * Examples: * * app.use(connect.cookieParser()); * app.use(connect.session({ secret: 'keyboard cat' })); * app.use(passport.initialize()); * app.use(passport.session()); * * Options: * - `pauseStream` Pause the request stream before deserializing the user * object from the session. Defaults to _false_. Should * be set to true in cases where middleware consuming the * request body is configured after passport and the * deserializeUser method is asynchronous. * * @param {Object} options * @return {Function} middleware * @api public */ 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. * * Examples: * * passport.serializeUser(function(user, done) { * done(null, user.id); * }); * * @api public */ Authenticator.prototype.serializeUser = function(fn, req, done) { if (typeof fn === 'function') { return this._serializers.push(fn); } // private implementation that traverses the chain of serializers, attempting // to serialize a user var user = fn; // For backwards compatibility if (typeof req === 'function') { done = req; req = undefined; } var stack = this._serializers; (function pass(i, err, obj) { // serializers use 'pass' as an error to skip processing if ('pass' === err) { err = undefined; } // an error or serialized object was obtained, done if (err || obj || obj === 0) { return done(err, obj); } var layer = stack[i]; if (!layer) { return done(new Error('Failed to serialize user into session')); } function serialized(e, o) { pass(i + 1, e, o); } try { var arity = layer.length; if (arity == 3) { layer(req, user, serialized); } else { layer(user, serialized); } } catch(e) { return done(e); } })(0); }; /** * Registers a function used to deserialize user objects out of the session. * * Examples: * * passport.deserializeUser(function(id, done) { * User.findById(id, function (err, user) { * done(err, user); * }); * }); * * @api public */ Authenticator.prototype.deserializeUser = function(fn, req, done) { if (typeof fn === 'function') { return this._deserializers.push(fn); } // private implementation that traverses the chain of deserializers, // attempting to deserialize a user var obj = fn; // For backwards compatibility if (typeof req === 'function') { done = req; req = undefined; } var stack = this._deserializers; (function pass(i, err, user) { // deserializers use 'pass' as an error to skip processing if ('pass' === err) { err = undefined; } // an error or deserialized user was obtained, done if (err || user) { return done(err, user); } // a valid user existed when establishing the session, but that user has // since been removed if (user === null || user === false) { return done(null, false); } var layer = stack[i]; if (!layer) { return done(new Error('Failed to deserialize user out of session')); } function deserialized(e, u) { pass(i + 1, e, u); } try { var arity = layer.length; if (arity == 3) { layer(req, obj, deserialized); } else { layer(obj, deserialized); } } catch(e) { return done(e); } })(0); }; /** * Registers a function used to transform auth info. * * In some circumstances authorization details are contained in authentication * credentials or loaded as part of verification. * * For example, when using bearer tokens for API authentication, the tokens may * encode (either directly or indirectly in a database), details such as scope * of access or the client to which the token was issued. * * Such authorization details should be enforced separately from authentication. * Because Passport deals only with the latter, this is the responsiblity of * middleware or routes further along the chain. However, it is not optimal to * decode the same data or execute the same database query later. To avoid * this, Passport accepts optional `info` along with the authenticated `user` * in a strategy's `success()` action. This info is set at `req.authInfo`, * where said later middlware or routes can access it. * * Optionally, applications can register transforms to proccess this info, * which take effect prior to `req.authInfo` being set. This is useful, for * example, when the info contains a client ID. The transform can load the * client from the database and include the instance in the transformed info, * allowing the full set of client properties to be convieniently accessed. * * If no transforms are registered, `info` supplied by the strategy will be left * unmodified. * * Examples: * * passport.transformAuthInfo(function(info, done) { * Client.findById(info.clientID, function (err, client) { * info.client = client; * done(err, info); * }); * }); * * @api public */ Authenticator.prototype.transformAuthInfo = function(fn, req, done) { if (typeof fn === 'function') { return this._infoTransformers.push(fn); } // private implementation that traverses the chain of transformers, // attempting to transform auth info var info = fn; // For backwards compatibility if (typeof req === 'function') { done = req; req = undefined; } var stack = this._infoTransformers; (function pass(i, err, tinfo) { // transformers use 'pass' as an error to skip processing if ('pass' === err) { err = undefined; } // an error or transformed info was obtained, done if (err || tinfo) { return done(err, tinfo); } var layer = stack[i]; if (!layer) { // if no transformers are registered (or they all pass), the default // behavior is to use the un-transformed info as-is return done(null, info); } function transformed(e, t) { pass(i + 1, e, t); } try { var arity = layer.length; if (arity == 1) { // sync var t = layer(info); transformed(null, t); } else if (arity == 3) { layer(req, info, transformed); } else { layer(info, transformed); } } catch(e) { return done(e); } })(0); }; /** * Return strategy with given `name`. * * @param {String} name * @return {Strategy} * @api private */ Authenticator.prototype._strategy = function(name) { return this._strategies[name]; }; /** * Expose `Authenticator`. */ module.exports = Authenticator; passport-0.6.0/lib/errors/000077500000000000000000000000001424171417100154545ustar00rootroot00000000000000passport-0.6.0/lib/errors/authenticationerror.js000066400000000000000000000006661424171417100221130ustar00rootroot00000000000000/** * `AuthenticationError` error. * * @constructor * @api private */ function AuthenticationError(message, status) { Error.call(this); Error.captureStackTrace(this, arguments.callee); this.name = 'AuthenticationError'; this.message = message; this.status = status || 401; } // Inherit from `Error`. AuthenticationError.prototype.__proto__ = Error.prototype; // Expose constructor. module.exports = AuthenticationError; passport-0.6.0/lib/framework/000077500000000000000000000000001424171417100161355ustar00rootroot00000000000000passport-0.6.0/lib/framework/connect.js000066400000000000000000000007701424171417100201300ustar00rootroot00000000000000/** * Module dependencies. */ var initialize = require('../middleware/initialize') , authenticate = require('../middleware/authenticate'); /** * 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. * * @return {Object} * @api protected */ exports = module.exports = function() { return { initialize: initialize, authenticate: authenticate }; }; passport-0.6.0/lib/http/000077500000000000000000000000001424171417100151175ustar00rootroot00000000000000passport-0.6.0/lib/http/request.js000066400000000000000000000037321424171417100171520ustar00rootroot00000000000000var req = exports = module.exports = {}; /** * Initiate a login session for `user`. * * Options: * - `session` Save login state in session, defaults to _true_ * * Examples: * * req.logIn(user, { session: false }); * * req.logIn(user, function(err) { * if (err) { throw err; } * // session saved * }); * * @param {User} user * @param {Object} options * @param {Function} done * @api public */ req.login = req.logIn = function(user, options, done) { if (typeof options == 'function') { done = options; options = {}; } options = options || {}; var property = this._userProperty || 'user'; var session = (options.session === undefined) ? true : options.session; this[property] = user; if (session && this._sessionManager) { if (typeof done != 'function') { throw new Error('req#login requires a callback function'); } var self = this; this._sessionManager.logIn(this, user, options, function(err) { if (err) { self[property] = null; return done(err); } done(); }); } else { done && done(); } }; /** * Terminate an existing login session. * * @api public */ req.logout = req.logOut = function(options, done) { if (typeof options == 'function') { done = options; options = {}; } options = options || {}; var property = this._userProperty || 'user'; this[property] = null; if (this._sessionManager) { if (typeof done != 'function') { throw new Error('req#logout requires a callback function'); } this._sessionManager.logOut(this, options, done); } else { done && done(); } }; /** * Test if request is authenticated. * * @return {Boolean} * @api public */ req.isAuthenticated = function() { var property = this._userProperty || 'user'; return (this[property]) ? true : false; }; /** * Test if request is unauthenticated. * * @return {Boolean} * @api public */ req.isUnauthenticated = function() { return !this.isAuthenticated(); }; passport-0.6.0/lib/index.js000066400000000000000000000007321424171417100156070ustar00rootroot00000000000000/** * Module dependencies. */ var Passport = require('./authenticator') , SessionStrategy = require('./strategies/session'); /** * Export default singleton. * * @api public */ exports = module.exports = new Passport(); /** * Expose constructors. */ exports.Passport = exports.Authenticator = Passport; exports.Strategy = require('passport-strategy'); /** * Expose strategies. */ exports.strategies = {}; exports.strategies.SessionStrategy = SessionStrategy; passport-0.6.0/lib/middleware/000077500000000000000000000000001424171417100162555ustar00rootroot00000000000000passport-0.6.0/lib/middleware/authenticate.js000066400000000000000000000330261424171417100212750ustar00rootroot00000000000000/** * Module dependencies. */ var http = require('http') , IncomingMessageExt = require('../http/request') , AuthenticationError = require('../errors/authenticationerror'); /** * Authenticates requests. * * Applies the `name`ed strategy (or strategies) to the incoming request, in * order to authenticate the request. If authentication is successful, the user * will be logged in and populated at `req.user` and a session will be * established by default. If authentication fails, an unauthorized response * will be sent. * * Options: * - `session` Save login state in session, defaults to _true_ * - `successRedirect` After successful login, redirect to given URL * - `successMessage` True to store success message in * req.session.messages, or a string to use as override * message for success. * - `successFlash` True to flash success messages or a string to use as a flash * message for success (overrides any from the strategy itself). * - `failureRedirect` After failed login, redirect to given URL * - `failureMessage` True to store failure message in * req.session.messages, or a string to use as override * message for failure. * - `failureFlash` True to flash failure messages or a string to use as a flash * message for failures (overrides any from the strategy itself). * - `assignProperty` Assign the object provided by the verify callback to given property * * An optional `callback` can be supplied to allow the application to override * the default manner in which authentication attempts are handled. The * callback has the following signature, where `user` will be set to the * authenticated user on a successful authentication attempt, or `false` * otherwise. An optional `info` argument will be passed, containing additional * details provided by the strategy's verify callback - this could be information about * a successful authentication or a challenge message for a failed authentication. * An optional `status` argument will be passed when authentication fails - this could * be a HTTP response code for a remote authentication failure or similar. * * app.get('/protected', function(req, res, next) { * passport.authenticate('local', function(err, user, info, status) { * if (err) { return next(err) } * if (!user) { return res.redirect('/signin') } * res.redirect('/account'); * })(req, res, next); * }); * * Note that if a callback is supplied, it becomes the application's * responsibility to log-in the user, establish a session, and otherwise perform * the desired operations. * * Examples: * * passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }); * * passport.authenticate('basic', { session: false }); * * passport.authenticate('twitter'); * * @param {Strategy|String|Array} name * @param {Object} options * @param {Function} callback * @return {Function} * @api public */ module.exports = function authenticate(passport, name, options, callback) { if (typeof options == 'function') { callback = options; options = {}; } options = options || {}; var multi = true; // Cast `name` to an array, allowing authentication to pass through a chain of // strategies. The first strategy to succeed, redirect, or error will halt // the chain. Authentication failures will proceed through each strategy in // series, ultimately failing if all strategies fail. // // This is typically used on API endpoints to allow clients to authenticate // using their preferred choice of Basic, Digest, token-based schemes, etc. // It is not feasible to construct a chain of multiple strategies that involve // redirection (for example both Facebook and Twitter), since the first one to // redirect will halt the chain. if (!Array.isArray(name)) { name = [ name ]; multi = false; } return function authenticate(req, res, next) { req.login = req.logIn = req.logIn || IncomingMessageExt.logIn; req.logout = req.logOut = req.logOut || IncomingMessageExt.logOut; req.isAuthenticated = req.isAuthenticated || IncomingMessageExt.isAuthenticated; req.isUnauthenticated = req.isUnauthenticated || IncomingMessageExt.isUnauthenticated; req._sessionManager = passport._sm; // accumulator for failures from each strategy in the chain var failures = []; function allFailed() { if (callback) { if (!multi) { return callback(null, false, failures[0].challenge, failures[0].status); } else { var challenges = failures.map(function(f) { return f.challenge; }); var statuses = failures.map(function(f) { return f.status; }); return callback(null, false, challenges, statuses); } } // Strategies are ordered by priority. For the purpose of flashing a // message, the first failure will be displayed. var failure = failures[0] || {} , challenge = failure.challenge || {} , msg; if (options.failureFlash) { var flash = options.failureFlash; if (typeof flash == 'string') { flash = { type: 'error', message: flash }; } flash.type = flash.type || 'error'; var type = flash.type || challenge.type || 'error'; msg = flash.message || challenge.message || challenge; if (typeof msg == 'string') { req.flash(type, msg); } } if (options.failureMessage) { msg = options.failureMessage; if (typeof msg == 'boolean') { msg = challenge.message || challenge; } if (typeof msg == 'string') { req.session.messages = req.session.messages || []; req.session.messages.push(msg); } } if (options.failureRedirect) { return res.redirect(options.failureRedirect); } // When failure handling is not delegated to the application, the default // is to respond with 401 Unauthorized. Note that the WWW-Authenticate // header will be set according to the strategies in use (see // actions#fail). If multiple strategies failed, each of their challenges // will be included in the response. var rchallenge = [] , rstatus, status; for (var j = 0, len = failures.length; j < len; j++) { failure = failures[j]; challenge = failure.challenge; status = failure.status; rstatus = rstatus || status; if (typeof challenge == 'string') { rchallenge.push(challenge); } } res.statusCode = rstatus || 401; if (res.statusCode == 401 && rchallenge.length) { res.setHeader('WWW-Authenticate', rchallenge); } if (options.failWithError) { return next(new AuthenticationError(http.STATUS_CODES[res.statusCode], rstatus)); } res.end(http.STATUS_CODES[res.statusCode]); } (function attempt(i) { var layer = name[i]; // If no more strategies exist in the chain, authentication has failed. if (!layer) { return allFailed(); } // Get the strategy, which will be used as prototype from which to create // a new instance. Action functions will then be bound to the strategy // within the context of the HTTP request/response pair. var strategy, prototype; if (typeof layer.authenticate == 'function') { strategy = layer; } else { prototype = passport._strategy(layer); if (!prototype) { return next(new Error('Unknown authentication strategy "' + layer + '"')); } strategy = Object.create(prototype); } // ----- BEGIN STRATEGY AUGMENTATION ----- // Augment the new strategy instance with action functions. These action // functions are bound via closure the the request/response pair. The end // goal of the strategy is to invoke *one* of these action methods, in // order to indicate successful or failed authentication, redirect to a // third-party identity provider, etc. /** * Authenticate `user`, with optional `info`. * * Strategies should call this function to successfully authenticate a * user. `user` should be an object supplied by the application after it * has been given an opportunity to verify credentials. `info` is an * optional argument containing additional user information. This is * useful for third-party authentication strategies to pass profile * details. * * @param {Object} user * @param {Object} info * @api public */ strategy.success = function(user, info) { if (callback) { return callback(null, user, info); } info = info || {}; var msg; if (options.successFlash) { var flash = options.successFlash; if (typeof flash == 'string') { flash = { type: 'success', message: flash }; } flash.type = flash.type || 'success'; var type = flash.type || info.type || 'success'; msg = flash.message || info.message || info; if (typeof msg == 'string') { req.flash(type, msg); } } if (options.successMessage) { msg = options.successMessage; if (typeof msg == 'boolean') { msg = info.message || info; } if (typeof msg == 'string') { req.session.messages = req.session.messages || []; req.session.messages.push(msg); } } if (options.assignProperty) { req[options.assignProperty] = user; return next(); } req.logIn(user, options, function(err) { if (err) { return next(err); } function complete() { if (options.successReturnToOrRedirect) { var url = options.successReturnToOrRedirect; if (req.session && req.session.returnTo) { url = req.session.returnTo; delete req.session.returnTo; } return res.redirect(url); } if (options.successRedirect) { return res.redirect(options.successRedirect); } next(); } if (options.authInfo !== false) { passport.transformAuthInfo(info, req, function(err, tinfo) { if (err) { return next(err); } req.authInfo = tinfo; complete(); }); } else { complete(); } }); }; /** * Fail authentication, with optional `challenge` and `status`, defaulting * to 401. * * Strategies should call this function to fail an authentication attempt. * * @param {String} challenge * @param {Number} status * @api public */ strategy.fail = function(challenge, status) { if (typeof challenge == 'number') { status = challenge; challenge = undefined; } // push this failure into the accumulator and attempt authentication // using the next strategy failures.push({ challenge: challenge, status: status }); attempt(i + 1); }; /** * Redirect to `url` with optional `status`, defaulting to 302. * * Strategies should call this function to redirect the user (via their * user agent) to a third-party website for authentication. * * @param {String} url * @param {Number} status * @api public */ strategy.redirect = function(url, status) { // NOTE: Do not use `res.redirect` from Express, because it can't decide // what it wants. // // Express 2.x: res.redirect(url, status) // Express 3.x: res.redirect(status, url) -OR- res.redirect(url, status) // - as of 3.14.0, deprecated warnings are issued if res.redirect(url, status) // is used // Express 4.x: res.redirect(status, url) // - all versions (as of 4.8.7) continue to accept res.redirect(url, status) // but issue deprecated versions res.statusCode = status || 302; res.setHeader('Location', url); res.setHeader('Content-Length', '0'); res.end(); }; /** * Pass without making a success or fail decision. * * Under most circumstances, Strategies should not need to call this * function. It exists primarily to allow previous authentication state * to be restored, for example from an HTTP session. * * @api public */ strategy.pass = function() { next(); }; /** * Internal error while performing authentication. * * Strategies should call this function when an internal error occurs * during the process of performing authentication; for example, if the * user directory is not available. * * @param {Error} err * @api public */ strategy.error = function(err) { if (callback) { return callback(err); } next(err); }; // ----- END STRATEGY AUGMENTATION ----- strategy.authenticate(req, options); })(0); // attempt }; }; passport-0.6.0/lib/middleware/initialize.js000066400000000000000000000100451424171417100207540ustar00rootroot00000000000000/** * Module dependencies. */ var IncomingMessageExt = require('../http/request'); /** * Passport initialization. * * Intializes Passport for incoming requests, allowing authentication strategies * to be applied. * * If sessions are being utilized, applications must set up Passport with * functions to serialize a user into and out of a session. For example, a * common pattern is to serialize just the user ID into the session (due to the * fact that it is desirable to store the minimum amount of data in a session). * When a subsequent request arrives for the session, the full User object can * be loaded from the database by ID. * * Note that additional middleware is required to persist login state, so we * must use the `connect.session()` middleware _before_ `passport.initialize()`. * * If sessions are being used, this middleware must be in use by the * Connect/Express application for Passport to operate. If the application is * entirely stateless (not using sessions), this middleware is not necessary, * but its use will not have any adverse impact. * * Examples: * * app.use(connect.cookieParser()); * app.use(connect.session({ secret: 'keyboard cat' })); * app.use(passport.initialize()); * app.use(passport.session()); * * passport.serializeUser(function(user, done) { * done(null, user.id); * }); * * passport.deserializeUser(function(id, done) { * User.findById(id, function (err, user) { * done(err, user); * }); * }); * * @return {Function} * @api public */ module.exports = function initialize(passport, options) { options = options || {}; return function initialize(req, res, next) { req.login = req.logIn = req.logIn || IncomingMessageExt.logIn; req.logout = req.logOut = req.logOut || IncomingMessageExt.logOut; req.isAuthenticated = req.isAuthenticated || IncomingMessageExt.isAuthenticated; req.isUnauthenticated = req.isUnauthenticated || IncomingMessageExt.isUnauthenticated; req._sessionManager = passport._sm; if (options.userProperty) { req._userProperty = options.userProperty; } var compat = (options.compat === undefined) ? true : options.compat; if (compat) { // `passport@0.5.1` [removed][1] all internal use of `req._passport`. // From the standpoint of this package, this should have been a // non-breaking change. However, some strategies (such as `passport-azure-ad`) // depend directly on `passport@0.4.x` or earlier. `require`-ing earlier // versions of `passport` has the effect of monkeypatching `http.IncomingMessage` // with `logIn`, `logOut`, `isAuthenticated` and `isUnauthenticated` // functions that [expect][2] the `req._passport` property to exist. // Since pre-existing functions on `req` are given [preference][3], this // results in [issues][4]. // // The changes here restore the expected properties needed when earlier // versions of `passport` are `require`-ed. This compatibility mode is // enabled by default, and can be disabld by simply not `use`-ing `passport.initialize()` // middleware or setting `compat: false` as an option to the middleware. // // An alternative approach to addressing this issue would be to not // preferentially use pre-existing functions on `req`, but rather always // overwrite `req.logIn`, etc. with the versions of those functions shiped // with `authenticate()` middleware. This option should be reconsidered // in a future major version release. // // [1]: https://github.com/jaredhanson/passport/pull/875 // [2]: https://github.com/jaredhanson/passport/blob/v0.4.1/lib/http/request.js // [3]: https://github.com/jaredhanson/passport/blob/v0.5.1/lib/middleware/authenticate.js#L96 // [4]: https://github.com/jaredhanson/passport/issues/877 passport._userProperty = options.userProperty || 'user'; req._passport = {}; req._passport.instance = passport; } next(); }; }; passport-0.6.0/lib/sessionmanager.js000066400000000000000000000047241424171417100175230ustar00rootroot00000000000000var merge = require('utils-merge'); 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, options, cb) { if (typeof options == 'function') { cb = options; options = {}; } options = options || {}; if (!req.session) { return cb(new Error('Login sessions require session support. Did you forget to use `express-session` middleware?')); } var self = this; var prevSession = req.session; // regenerate the session, which is good practice to help // guard against forms of session fixation req.session.regenerate(function(err) { if (err) { return cb(err); } self._serializeUser(user, req, function(err, obj) { if (err) { return cb(err); } if (options.keepSessionInfo) { merge(req.session, prevSession); } if (!req.session[self._key]) { req.session[self._key] = {}; } // store user information in session, typically a user id req.session[self._key].user = obj; // save the session before redirection to ensure page // load does not happen before session is saved req.session.save(function(err) { if (err) { return cb(err); } cb(); }); }); }); } SessionManager.prototype.logOut = function(req, options, cb) { if (typeof options == 'function') { cb = options; options = {}; } options = options || {}; if (!req.session) { return cb(new Error('Login sessions require session support. Did you forget to use `express-session` middleware?')); } var self = this; // clear the user from the session object and save. // this will ensure that re-using the old session id // does not have a logged in user if (req.session[this._key]) { delete req.session[this._key].user; } var prevSession = req.session; req.session.save(function(err) { if (err) { return cb(err) } // regenerate the session, which is good practice to help // guard against forms of session fixation req.session.regenerate(function(err) { if (err) { return cb(err); } if (options.keepSessionInfo) { merge(req.session, prevSession); } cb(); }); }); } module.exports = SessionManager; passport-0.6.0/lib/strategies/000077500000000000000000000000001424171417100163125ustar00rootroot00000000000000passport-0.6.0/lib/strategies/session.js000066400000000000000000000041121424171417100203310ustar00rootroot00000000000000/** * Module dependencies. */ var pause = require('pause') , util = require('util') , Strategy = require('passport-strategy'); /** * `SessionStrategy` constructor. * * @api public */ function SessionStrategy(options, deserializeUser) { if (typeof options == 'function') { deserializeUser = options; options = undefined; } options = options || {}; Strategy.call(this); this.name = 'session'; this._key = options.key || 'passport'; this._deserializeUser = deserializeUser; } /** * Inherit from `Strategy`. */ util.inherits(SessionStrategy, Strategy); /** * Authenticate request based on the current session state. * * The session authentication strategy uses the session to restore any login * state across requests. If a login session has been established, `req.user` * will be populated with the current user. * * This strategy is registered automatically by Passport. * * @param {Object} req * @param {Object} options * @api protected */ SessionStrategy.prototype.authenticate = function(req, options) { if (!req.session) { return this.error(new Error('Login sessions require session support. Did you forget to use `express-session` middleware?')); } options = options || {}; var self = this, su; if (req.session[this._key]) { su = req.session[this._key].user; } if (su || su === 0) { // NOTE: Stream pausing is desirable in the case where later middleware is // listening for events emitted from request. For discussion on the // matter, refer to: https://github.com/jaredhanson/passport/pull/106 var paused = options.pauseStream ? pause(req) : null; this._deserializeUser(su, req, function(err, user) { if (err) { return self.error(err); } if (!user) { delete req.session[self._key].user; } else { var property = req._userProperty || 'user'; req[property] = user; } self.pass(); if (paused) { paused.resume(); } }); } else { self.pass(); } }; /** * Expose `SessionStrategy`. */ module.exports = SessionStrategy; passport-0.6.0/package.json000066400000000000000000000024071424171417100156630ustar00rootroot00000000000000{ "name": "passport", "version": "0.6.0", "description": "Simple, unobtrusive authentication for Node.js.", "keywords": [ "express", "connect", "auth", "authn", "authentication" ], "author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "https://www.jaredhanson.me/" }, "homepage": "https://www.passportjs.org/", "repository": { "type": "git", "url": "git://github.com/jaredhanson/passport.git" }, "bugs": { "url": "https://github.com/jaredhanson/passport/issues" }, "funding": { "type": "github", "url": "https://github.com/sponsors/jaredhanson" }, "license": "MIT", "licenses": [ { "type": "MIT", "url": "https://opensource.org/licenses/MIT" } ], "main": "./lib", "dependencies": { "passport-strategy": "1.x.x", "pause": "0.0.1", "utils-merge": "^1.0.1" }, "devDependencies": { "make-node": "0.3.x", "mocha": "2.x.x", "chai": "2.x.x", "chai-connect-middleware": "0.3.x", "chai-passport-strategy": "0.2.x", "proxyquire": "1.4.x" }, "engines": { "node": ">= 0.4.0" }, "scripts": { "test": "node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js" } } passport-0.6.0/sponsors/000077500000000000000000000000001424171417100152605ustar00rootroot00000000000000passport-0.6.0/sponsors/loginradius.png000066400000000000000000000252101424171417100203060ustar00rootroot00000000000000PNG  IHDR2B.sRGB pHYs  #iTXtXML:com.adobe.xmp 2 0 72 1 72 243 1 50 2020-12-11T11:12:93 Pixelmator 3.9 o%IDATx} ŝ]3 aP9 :jĿfn\k52 + D#Ѩ@\`8BaPfޛ~^ B^Mw__ZPgV!}̾$2vOH9~[H-}@>|c bIaN]_$D"{7iF44m;?Eh϶,3 NB&A2U5t1yy h?1OzV:tq Jyp Tĭ> =FثtݨSc.|l}ЦIޖ{&lXj\By h1߷Z-~%=OnpTфK0BzXj4[\U[shVsyR?XUф|_l BՊPVmvPi ʪ$r/D.@PD|ALԹ<ִ.0]FL6hCqVt(%^wRYU :Hl ٌbI4&Y5R MI -l">G| 6ב-AJG#=N!<881^9!rKvKOl6ckנ?3vy+8kO"i~#]%rFݱ|7l U%`>"cy[ s,Y4[Vt'tRRs06oSkuπ~gd@BzFFr[#N~IXLU"K s_A] dEV=Bq#[ ٘.wL¶7-M<4Ar ^~ӑŋܴ#᚝}{P XAa|; K֜>+rXq|e4/iCR$&i[_ڨT\Wj`檱 ;2ScyNQZ*j-kKÒ;TMVS x;N$g xFLM<ƞ5nô7Pc K7#剹CaѪ`iŝ婂/7KVRdKJf3 1,~}?Fuzp`cB+!1* Y9?m<ufдV K|#ZJI`& NsҞ1"{4m[8F};R_[5ܲœaY/Iɼc 3$ /?Yяg_OՃ1)Dt`2 kЀ E{$zKP5~6nOEfJ{cy2a>o΢_xdɤibY ee>H_jP *v^k'u'Tq]J;"i2b3۞5ьقiRn _Qo^G[@>[5`>&M*%S(WQo2{icXÀC>ƤnkLŖ' M7%%kYU hthΩ) ܇qa W^b @<5 8sfA_% /is SqJK[Il )X!@@'f0987f@xYzNpF1b2cѧ2 Z>,CkW5KB%'#ut4u 9X]C8<Ky) dfO7+ҏ &*$[6gb.,hSaW8K-ۤO~oãPv[ tK)j h7)d!#,4~$;KKweZs 30.U-\F*osX8ej'b.빃s8 X;մ%7չ.?>jmmYe(P2Ѝ:W? $lmLPo~جPe2ϕc-hvT㯤@d W /j}GuӰf"̝/KW]j x|8a6&N%ILJ3Hs׼3чl3kPd`gPF ?(GwT63MllyA$a*?⤅KO&,0`GKRxbhFirM/[`w-)k0hN,괥".OJ+*L)A^0"@Ը{};1Eƭȁ6`N7Ո{ f 4 8: Kn"㮸Nu@o<ϳbv+3 Y c. -p+Nb1p3"{=ٴd^|^gIbÒ-sgZhMr38\h V U Opg\Wlgǿ3 iq)s:.ĵd+97 o(iVQ'sO8yяi-~,C6ˏ=Q+D䛎bcO)i[fͅ`[:i.m&ܜkFX_2~00&\H r~!nS8/peUQ ]ButꅃcP (onj:T<vҍbH=n,K]pU%rx9/ A!NELSt Y؎a:'Z`+è>!fE. Z8|m Syю&F P.'TtoHKoY %tm#kDۥߖcI̶`R1^w=YѓStC5G/ŊsEӭjfjLWbw wvI{f[R]i N^07v x:k7d|UB;9b^A"0fj! k,,B?(*F?gIYkS{-=e٫ߥ)U7> v}rmsŻ2໔V~$xI%)(]T1eCm} %%q`FcFʏ /$WJ*O5qTiU&@~7Am?lGbn/lhu`Xb bp4@0|RHf<$CUQ/Z+1K0/ZR8QssLH'+.TrEjM[(a^Lʜ[j.,<"E:ܬx= 8p4c'X sc\JU^쀶*dgaG#B/čI^u.!Vq>%((39Re|=4XAx4q)Yqz"MhC)Ӝpc m3uR~ b#RC9L&d v :ē 3!:vp _|JXƙN,<;%([yRqh*#ʓN3X K='XZ0o)7AX{저߇bj31~IqQ: RcAUT6"`~dvjUL2ϡ-7ɯZn1&)݆>F@xe9K,%ck pJf<w(T,_vIBX ºczJg`487`'Y$j6 fHtbVo)%oGLm8VMH'&:h=+rfOm\O89?9sQvڂH'snN,,*нp=dXE f<sǽ9aW07~$\-]{ڏ3qǷBvP0 ש:)$ "Ω4jfI>! ȕRڮmM'ܟ(D~̭ GH?P9>n eM8ʮ>"./-GM oB1fZd .ROt@Ia%v[^^R ڼ~.ސ_H?  q茅1Խ3 tB5%#, >z7vjb.ϧemTޜ|t+±=ar=<HTȉ$X'v6/>WӰM視GRƹbddcOgank`' ȝ/5qu\E)|-]ֲ/w MLG !)ɀaLŶ^ t#vZyi{ᵪ&$7dV@- w= :Kt@"z[YC5 ~o†զů/2ԲQՙkYb2h+Ltw ˾2P #Dіx3G:Gѧn|,ϕP F!R* C X(3F,O[6 W{Ɔ;oh1h~'=m]f9r~٢ʁ/""* $I3E;п0]ЬlǑIi⟨לCHr(*vɐʬ"M LmWue8  ڽě+ܠcvG-+ݻ%:gY l b&>th:k4pm>X),B76$vjGA}s5=loٰ6WYHr :{4fp|L66`%|%#g9vx['cDt4Ėq|q17Ip٧[Ky_4'!5îij;}&9Tjՙ#~qc 78w% _LY/!&31Dq6ݨ6_0y-V_, u/i憛A!bTpg5.֐G㠄 !V)aɾ@h7p?fDqsJ{# DsW?) 8\qIRIYk(˕w/D$Y* 11 Q!J񭧷5bRZ[f)KTb FK!Ē@jtjaǮ,,pbV V: +pvag].*K+o;PinFRx_-8'>}'6$2Ƥq.aX~Z6~i" xt/|Q\{L|  J @ccSۼ Ӵ$1snj"Wag[ |A>M3W^Hdџ5 i>7曵!YE|6ifXOu@m~u&Hgy:ybpj_xa8: v(#̨&g G]i竌Kjp 2e ߋS -S0- j'f{2`SW\[b@(K28p8qPĶ~ڕL]gb*iYO}1*t⺋0.KU||2^VnV([{;4Z;F^"N~ }w Ӫ#`H U#^ľޅ/YƜ[5%Y@o> u: M *o<00!7[]|fendҸ&'2Z>"S Czm"-׍8Pe4N"AՃ: ~"78`勣ŐN {)bHuj>+BV,!sjr KP΂$e7g2'={Ry#G`촭e@MX)ctp] Jz4B]pLi-^\Fjk[d#Ĉ瀳~3*LD!s"~h/sИ ҆܋1hN6m{Eaq]5R/Oc[h$߻N0/4ׁ}|κlu`Zk#iǡ->>yfUW3/0r=H CR% U;cbW%/LnbOn8cbݑW'0tcnVVP"8RDQI kmXXdʐУ-A>d'f.ڟOa]L> W!4wu l5U$RVC%N?,qⴾfՓЌQF8 BPɈM)scu쁏֕lf^.X{+hY m`UŲ>& Y ò%ϣ$?z1SG,ļ!'Dqj8xgߵ1c,{reS/AIg\B~5wˆ|8OXvɇ<881sE8g"$2eyzp$S+UsAO`be!zf4?zS+E(y[@ ]p1JQ8d$M!GG]c]ck`򐝍l8<{}'Wn@Ԛ4K37iM1S 0!J|ZOn;0Oog^XA03̮[5ZX:aIoE܀=8vB<2YenB?&bfn>cIq݉316`J"+\WB&r@J+bG=!++00 ?ީ+|a2 yg8fm^{_}.qпn 59iǎagV%lmA$k8j GTA>1B?~IENDB`passport-0.6.0/sponsors/snyk.png000066400000000000000000000260251424171417100167570ustar00rootroot00000000000000PNG  IHDRZVq3 +IDATxwUeqRˏ*p! %MqB .!A$.)E!XJ#FJ =Umy)$嚢`ҥ5Vm;Ξ9'g9ZYK! D禛Y\Lך^|@1.w$=TWz].k|@CKQDܗ. e5\E\wZ BKSyw$-Z#u7I ,}KG\wG; V.w9\{YwPɎs(j\ekg);tX Ep; )]H%5wR4JmVQS֗_~ef47O>"(͠?ֱ'MS~ ϟv=xSY@&ͳZ[͈u^Dy Rb)+Ne#vHY}Ęa5^SG81T*@PXF<_֡Pj'NFT)>來30DNF '}ׇjPs_Km%y@Q;R( |=-lU@u0LFTO#zdk]DSdQ;R2`8)O:FyZ.j,yz/O7gG;PQNIU+ ^3V$f15nA zM)hU-jÈ9vڇGQժ' $TdS|IaT@ad:E(֥OU Fz܎QK% gHZSu;8m%yCL0hZt TRxH%Ux@U "YIPy}]RrPOP#, x}~]Φ!s5kތf/#G;]v#U-%t{fMc9<͗Q5`}ʰEU Su^F<'u^p:ӓT@L!څ.Ok xhGڬ<Ӛ5KU-0,|xfA;djˈ$Ny%dɒ-DUZ@@:VdiXT__h-Y&{cȪȟ%KF<'mTU- F@ytz/%K=LQuwXRUt@yd AշԶa!KWc|vZVaVRXO' 6#VOMF;׎YWy`OP:n]<ܲbӈ%K— $u3W{CCYN#[T-)F:P,ɒ-BAKu0!T[k./^~l紶cPnLu:*eN Z^ۑYYBS#Au2PВ%!Q@y-C Y-Aq;NAK5GK+òs3{ziϢ)(jU%sVՙrxSȒ% ,F~(K8,@вoR]B۞5]ҿX9u~%NUAK?t\sTq?"UeH4h}֗_~iW>q{ Y&OXX6oڝնcu:`2lIի5/麤%1- ZTT!QВ->whYyAgCƶ Mx0$yvyk|뭷K" Y9l E.8WOV{ +F@2.^GOShs">C V6&hQ Y-?mHmCOF9˔U<dTJ7d֒uXӰWXY؃ֲm7~l/is ZR*9&,$h wke*A ѠնcSȒ02?y26?j|C:lZ@rx2$h-%O:itsĚ;ҁ^ӫ%=dOw[>9Zk}z:v9n-iw oMܪ^C aN'$0d)!hzd>ӑ;YR͒5cFZ28gX[('ջY3AU$YU֤I+%`>:3-uȍ5TLb>9y/K5v'=h5@5(z܃r Zr0RT1yS- XaNXhTgmH*0O*EZ!h2sq,ե/QOl2 o2VGUEZKR3f1g=]AK*mj/LA+# =7DoC#Ad.-]Rs|:uŒZ?$,7=7pbWqbMw-七v2! (;V}Ъj,9ZI YVzZIy_zwד}hsВQBPAk-ŪY_A+!kpr\A+!K[- q,8V?AK0y%}q} A>4j;huk1+ (⠥:될%;?K\JӅAo8!/^UJk q"wCjX̙eZg k<@JQ'JD{|XAy [U2hїE hU܂Vv=@hA끕׎,V7&7 {ҺQaC9-MI>4t @\{~㗹eN?a%[ ZݯdzxcxߧKzJ"hbD& ]^o@! +v a;޿Tgₖ#/<cRMd[gxJ+lwtCjVchuQ\ 26Π%Ig`h51@TpBogsh-ݰ:%dm\!Lo N !>>\sxqW3C ZI e+Z 4&ᦑu|=g)^T.Ԡ5 YAu;{_.øV=DuNi.Ǡed>4Lf5Kza>izvn՜S NԌ}Ζ͏ - :Vh4sƚ[v hU[8kuO?[AEN~$henN! y>=Zq^^< db+:y ZqZ}; Sү \Np<#'7Z7#*[E%<^i7jWޔZъ ͵rИx:/[b+ZHpgZ֜9ؕ)S5>Xzڝ%FuVa!h] >AKJa+崤^7dy0k]T,UВURAmܜ*SZalAeJr ZPA+$@[ZкpuƂε-G[?]Z*E ZfԶpv ,hTu'e=]AƘHpjq1@AwIv[&oZؒ' Z7ҹ199gD|A+:@Agt;N?dڷ Y'.WG{v)5lj1QSM~$1<̠yϺBgjquRAK]08\u#h> 4›qE2Zpj<# Z Uz )deWA+H?v=9tUAkhp"h Z1NR7u20SZ֬jW4yYY={1dE.iܹ2$OREڗulw!+e7QܕU9ƾV? 9/KXRВ]^=q8%VAB֙[IurHTЪLxX[)-? { qLFXlA+ӯ5f9[21>hjwȺx͚5!DlOȺeI 0PMq+LJ,hkͻwb Zs\vQeL> &V`{MZaxi#ZVl*c彮+Cb qVbwwL'*25(Fd=?Mrk~]NXA+A>n.j%he >)A+dQA+%u8ܚߣPVGCV0!7;cZ@)-IAօGk[<-/mZaU.>pezM+h+ A+LZ"h]N`=BJ^3I Za?JՌ;%PAkBQXAK2*mFs ZTSzZE:G+{SUXm%7 .CVSd ZkVz*pВá Z&,,U tM6cE$\ *Vǁ(|2'L4h56l 5h]y5ytYƍY\?S:5*ˠUor=T9DŽaCf? hVd֞-z=kִš(&5hy&7R:LVaٵ{voլ -Y'w24PR!֎u["feYh蝸PrլtX@ 8CK+;-YAkUvZHLȒ'P?ixwAPК~XuAkFfӾbEwz:*nК6׏rA I O R@ЊiXuFF<̝8dl"#mgh{A *YZyn!Lx'-!K5ڡjD -%h!N2Lu3G$,yhinؒ<`*ӌFO!Ú%ʪA %P_,-5oz1w7fOA+V76/dugy]Ms{„&eЊjh)A \bnVZkgVwn*飞V~ 2iMv-s7>e7x^-*V5esoUg@As#,Dc]VTCK Zeb )g hU;e}z@A15涹kް>PZ[|ncuU&,}A 7JmemSkSLbhHU hZ?cd }( vJã6Y) 'N\f-N2]bAzw Z1^>6\SWlTO,Y➥EЂ B[J"h}|R ZTU-`jGgי[(}Y٠%+Π?*R&5BV/o:Zo>PVێZACLzvȒanCJndͷ&N\G>޲>3A ϹYe(˰ܐSՒS1[5hSB,YQ-}oR hA[[":bZ-;ܠ?Xꅮh0u聡p%,&9zφuVWǖa,5u+ԍf Ȇ4_N;r [; - T[M'q!+W_}YBңdqR [UݕQ/abBV!+=B|ْ6.]W[1h4xU,aKœ% _|Va?$-T/! @9S˶1J< mYڵ O>=yדB|YZ.cr< S 4(5ǭQ^@xn+u[ 'K^1L-ixwېmxO2TjeKwuYu7!c L-Y[ۖגY#~e:^}dF|;뇾Νe968CK]*m–1~>V8,<,.TU#,YUZCK@y-17nܰCo|84\IuuKB|4NwT=cl!S_y@J|n{n/_9tU,?!-hudl(XR_6Zd1)2Aw@YZSm:ghy} 1I!+?hMal [c x]I-ۈ^WEFBV,96X_0x\17n_h"he181;}ZU =1 [c }ZxDbD:}P}1 [c u}-YR-~zv ܭ1uvsBݵg3֗_c:[(a)MJ_w޹ֺtm6PÖrHV!kѢ~46r W__>hc@,au ? Q>5u:wm.O?}EVޭUwD\@~%6D-1|qٳXW\clH|(Sl96 Qau ěoQ>h/6 \c |hA Ö_[~B/0\R֦MB ZӦix;5l8~eAk۶ޯS\@jl4;ix\@}wZ! m%\@\;Fy3nUnX\r XQ zau ӯ9-._{44({XV=2̹ –r 4oc|-1mc ,l`l@4 1m0:rydIENDB`passport-0.6.0/sponsors/workos.png000066400000000000000000000153061424171417100173170ustar00rootroot00000000000000PNG  IHDR2zGgAMA a cHRMz&u0`:pQ<eXIfMM*JR(iZHH2Z pHYs  YiTXtXML:com.adobe.xmp 1 ^GIDATx p\Օ%YZ6`3l8ɘ0@jfA )%X`v@Ad&8 3"@ˬ’ nc؀bKꖻ%yjNw;s=ٱXD" D,Y @d" D,Y @d" D,Yζ̙nb^ފIbnC\!V4izP4F\g,6Hf=PFY`c} 뺎ȥs>;O|;s'ՅfcqWR5wv5gNXӫ/46 }pS2Q>@E''>gNۭ$3%Wn_Yw\((%Uʹ=t+_U;cM62iƙ3%u{z[fEk9xMM[ԡLW>@J5wȕ|mGڊ$5~}r&ݚF1h6a$s #!s$,̙--G:/++{-#a9|;P5XmAuu͛7jnBOeeMewwMM5wϚyZ}1aAGm'~^&nn[م_#qA:Vi!b>cCً6㳔E Ό{nǾ6$*vtL4HZrx*FhFp@;lQvppUy0_R 7 )d(9*+\)|k [LXy#vR1ɢ{NtwOcznDW.]j|}=D;**/F-/EpǣoB1 2 &?,퐥8? @ {-XW>̓6MqB X} B 91W)i'BsZoUB^0 |yՀ9dݔȢx]ݦIz-[7Tf73i%(RGDA :eɚ?GEOHOY(p?qpAJw ]' eQ ^6&GU1 |8a^CJ O]8WH? 6? oTf816e`k *w(!<~(Cj++kǸnF) 魨H3_65m6HBϼ{:~is\L2wo2cHdtqoc8='}r,8Fo_TEd􁘫@[›lX68LfLdG,XWV =a2cI# /D /+|TI?"*0J/ ,Yuu1ބu|П/wX(Gu,'g;}qM., ۄۅ˅cƤ/E8S%Xp8R7tKH uN ]"nR^ (':ҾV]. c^ j~&f[[+4Bu,wm&.e2[0:吧W\GggDeA" *EsDrlXL9(@8"NT{QRLxBť99;Nvjl4(z cViY[y9B8/WXKt$LsP *#>(/<*\%'XP#t#rp$䭭 0R|̉zk&# ~V!c+J7vAF%nR/>Л=j9/z˫x&65guY+0w,y zU`Q,\]+=\0p^(NKNAha\ƃ&z(DŽUM™Bqτ lJ1l\e=}'[КsPR φM{#,,XE)XTW{w; t#G|x/]-(ˉyt, |*0o X@rK^3;([:y.ϙtGX^N:MΟ5kX\ErM$0~]yW[ QYlI?)epsz mylQp\4OhD7A08? '}wxP PBOgR#'#"ATLp[[T?zS b 25O'*ʳzy 8I$ʸtồɩTH7#/y"/~4/ \}R}7 S9oaέ2>sXю*w!inGð#&\ Ю~ҷ8ocYcrA'BڕM:V :|:F뼟x-δ[PΕT%֎- C_64zBpp0CWMG_1/1!EPѝ[ZZ>y4MY &_q/OIJM9 /⭨yR` 2fT*PcsZI6y OnWAZ'6Y1\]p<"3oR~ @~TdVD2»N"jD+lkDd V{ $q}Vw''{nwME|#^_߾XP:٢Ca66pwm<1#y&g!pQP_`5ZdLA90 l.oe.L&+\/orpz`#2щܯ_K)qRlgaBR"A1l#`a"\Qj~<;I}? Wy@<%ql;I=](^Jxs6%P/xq?y=BIUq4.fS8@0y(`NG#A aLF#@Q8_=]\S8YS@?9FqlA0ao>Y`A(ӏ9*@_1U{0]0^u.Frrڛ˥KkoW4_'"rE󐛔sIkӱ0׷VqF?Oe, "\<:d-֊K4Dѝ(Npڡ6>4%X بo 8瘃L𘰣Iɟ Kj9G #DzG_ \(@t\-,;"9+?tFpxَ3VWS R vr's'1'"D O?KL-+},DŤ/s˒%fcAg TYŀ~  Edo6 XSQ9Cbj=,+Kj lrG ҇>8 H *|82 o+pA ;|Fh ؇#Z q'J@֯tݮ~g)|ܔ-Xa7}<-il=R|M L'&t\e]FOUt?J 5i!aWWmmmpȪA-) e!sf":WgaAgS&%F_Wf`Eh}qV_6 ĺr41>o ^/x3ac3'03A`lsHdbfxq3Pfl G O"gd Snذ3d&ɉ°flJO]Ro,oODj(O͓^':X,͙2>p(y1, l}lU7.g 89}VRdוc97Gg& #G>Fؼ--!n쀬B}ӟۃ=] lqx1cjEns09'lǔt9^O)h;}oa?Q1MWx"j' BX#Zh8mD.Zϱ` *-?rܹ԰P! (KN<#&};AN^g IDjw7WU\sQ)Ew?|a^\_GY|Tז _mEx3P-ļaơ_ʡ?e| ~/r{S@5[0؈7I[sD)=Op8}2o>-l{]o)/?I d܌nX>x}iMR>Nw.ll]a>2j~rzdkWv$Js6 A4 N7bzRy{f R6 zxʎ!xG9s6}KAf >XFp֭XG.En"l|"i\Bj1FidY`T쟌#?{vd23evF-vy)KEuŐM볁5~ݐ1o(Y`Xm$ÝDpU>[Y`:N}51 3HLS',Y @d" D,Y @d" D,Y @d#}bIENDB`passport-0.6.0/test/000077500000000000000000000000001424171417100143515ustar00rootroot00000000000000passport-0.6.0/test/authenticator.framework.test.js000066400000000000000000000030541424171417100225350ustar00rootroot00000000000000/* global describe, it, expect */ var Authenticator = require('../lib/authenticator'); describe('Authenticator', function() { describe('#framework', function() { describe('with an authenticate function used for authorization', function() { var passport = new Authenticator(); passport.framework({ initialize: function() { return function() {}; }, authenticate: function(passport, name, options) { return function() { return 'authenticate(): ' + name + ' ' + options.assignProperty; }; } }); var rv = passport.authorize('foo')(); it('should call authenticate', function() { expect(rv).to.equal('authenticate(): foo account'); }); }); describe('with an authorize function used for authorization', function() { var passport = new Authenticator(); passport.framework({ initialize: function() { return function() {}; }, authenticate: function(passport, name, options) { return function() { return 'authenticate(): ' + name + ' ' + options.assignProperty; }; }, authorize: function(passport, name, options) { return function() { return 'authorize(): ' + name + ' ' + options.assignProperty; }; } }); var rv = passport.authorize('foo')(); it('should call authorize', function() { expect(rv).to.equal('authorize(): foo account'); }); }); }); }); passport-0.6.0/test/authenticator.middleware.test.js000066400000000000000000000206531424171417100226610ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , Authenticator = require('../lib/authenticator'); describe('Authenticator', function() { describe('#initialize', function() { it('should have correct arity', function() { var passport = new Authenticator(); expect(passport.initialize).to.have.length(1); }); describe('handling a request', function() { var passport = new Authenticator(); var request, error; before(function(done) { chai.connect.use(passport.initialize()) .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 set user property on request', function() { expect(request._userProperty).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.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() { var passport = new Authenticator(); var request, error; before(function(done) { chai.connect.use(passport.initialize({ userProperty: 'currentUser' })) .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 set user property on request', function() { expect(request._userProperty).to.equal('currentUser'); }); 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'); }); }); }); describe('#authenticate', function() { it('should have correct arity', function() { var passport = new Authenticator(); expect(passport.authenticate).to.have.length(3); }); describe('handling a request', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Authenticator(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(passport.authenticate('success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); }); describe('handling a request with instantiated strategy', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Authenticator(); var request, error; before(function(done) { chai.connect.use(passport.authenticate(new Strategy())) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); }); }); describe('#authorize', function() { it('should have correct arity', function() { var passport = new Authenticator(); expect(passport.authorize).to.have.length(3); }); describe('handling a request', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Authenticator(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(passport.authorize('success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should set account', function() { expect(request.account).to.be.an('object'); expect(request.account.id).to.equal('1'); expect(request.account.username).to.equal('jaredhanson'); }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); }); describe('#session', function() { it('should have correct arity', function() { var passport = new Authenticator(); expect(passport.session).to.have.length(1); }); describe('handling a request', function() { var passport = new Authenticator(); passport.deserializeUser(function(user, done) { done(null, { id: user }); }); var request, error; before(function(done) { chai.connect.use(passport.session()) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('123456'); }); it('should maintain session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.equal('123456'); }); }); }); }); passport-0.6.0/test/authenticator.test.js000066400000000000000000000670671424171417100205570ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true, sub: true */ var Authenticator = require('../lib/authenticator'); describe('Authenticator', function() { describe('#use', function() { describe('with instance name', function() { function Strategy() { this.name = 'default'; } Strategy.prototype.authenticate = function(req) { }; var authenticator = new Authenticator(); authenticator.use(new Strategy()); it('should register strategy', function() { expect(authenticator._strategies['default']).to.be.an('object'); }); }); describe('with registered name', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { }; var authenticator = new Authenticator(); authenticator.use('foo', new Strategy()); it('should register strategy', function() { expect(authenticator._strategies['foo']).to.be.an('object'); }); }); describe('with registered name overridding instance name', function() { function Strategy() { this.name = 'default'; } Strategy.prototype.authenticate = function(req) { }; var authenticator = new Authenticator(); authenticator.use('bar', new Strategy()); it('should register strategy', function() { expect(authenticator._strategies['bar']).to.be.an('object'); expect(authenticator._strategies['default']).to.be.undefined; }); }); it('should throw if lacking a name', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { }; expect(function() { var authenticator = new Authenticator(); authenticator.use(new Strategy()); }).to.throw(Error, 'Authentication strategies must have a name'); }); }); describe('#unuse', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { }; var authenticator = new Authenticator(); authenticator.use('one', new Strategy()); authenticator.use('two', new Strategy()); expect(authenticator._strategies['one']).to.be.an('object'); expect(authenticator._strategies['two']).to.be.an('object'); authenticator.unuse('one'); it('should unregister strategy', function() { expect(authenticator._strategies['one']).to.be.undefined; expect(authenticator._strategies['two']).to.be.an('object'); }); }); describe('#serializeUser', function() { describe('without serializers', function() { var authenticator = new Authenticator(); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to serialize user into session'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with one serializer', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(null, user.id); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal('1'); }); }); describe('with one serializer that serializes to 0', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(null, 0); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal(0); }); }); describe('with one serializer that serializes to false', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(null, false); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to serialize user into session'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with one serializer that serializes to null', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(null, null); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to serialize user into session'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with one serializer that serializes to undefined', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(null, undefined); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to serialize user into session'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with one serializer that encounters an error', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done(new Error('something went wrong')); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with one serializer that throws an exception', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { throw new Error('something went horribly wrong'); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went horribly wrong'); }); it('should not serialize user', function() { expect(obj).to.be.undefined; }); }); describe('with three serializers, the first of which passes and the second of which serializes', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done('pass'); }); authenticator.serializeUser(function(user, done) { done(null, 'two'); }); authenticator.serializeUser(function(user, done) { done(null, 'three'); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal('two'); }); }); describe('with three serializers, the first of which passes and the second of which does not serialize by no argument', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done('pass'); }); authenticator.serializeUser(function(user, done) { done(null); }); authenticator.serializeUser(function(user, done) { done(null, 'three'); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal('three'); }); }); describe('with three serializers, the first of which passes and the second of which does not serialize by undefined', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(user, done) { done('pass'); }); authenticator.serializeUser(function(user, done) { done(null, undefined); }); authenticator.serializeUser(function(user, done) { done(null, 'three'); }); var error, obj; before(function(done) { authenticator.serializeUser({ id: '1', username: 'jared' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal('three'); }); }); describe('with one serializer that takes request as argument', function() { var authenticator = new Authenticator(); authenticator.serializeUser(function(req, user, done) { if (req.url !== '/foo') { return done(new Error('incorrect req argument')); } done(null, user.id); }); var error, obj; before(function(done) { var req = { url: '/foo' }; authenticator.serializeUser({ id: '1', username: 'jared' }, req, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should serialize user', function() { expect(obj).to.equal('1'); }); }); }); describe('#deserializeUser', function() { describe('without deserializers', function() { var authenticator = new Authenticator(); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to deserialize user out of session'); }); it('should not deserialize user', function() { expect(user).to.be.undefined; }); }); describe('with one deserializer', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done(null, obj.username); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should deserialize user', function() { expect(user).to.equal('jared'); }); }); describe('with one deserializer that deserializes to false', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done(null, false); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should invalidate session', function() { expect(user).to.be.false; }); }); describe('with one deserializer that deserializes to null', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done(null, null); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should invalidate session', function() { expect(user).to.be.false; }); }); describe('with one deserializer that deserializes to undefined', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done(null, undefined); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Failed to deserialize user out of session'); }); it('should not deserialize user', function() { expect(user).to.be.undefined; }); }); describe('with one deserializer that encounters an error', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done(new Error('something went wrong')); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should invalidate session', function() { expect(user).to.be.undefined; }); }); describe('with one deserializer that throws an exception', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { throw new Error('something went horribly wrong'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went horribly wrong'); }); it('should invalidate session', function() { expect(user).to.be.undefined; }); }); describe('with three deserializers, the first of which passes and the second of which deserializes', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done('pass'); }); authenticator.deserializeUser(function(obj, done) { done(null, 'two'); }); authenticator.deserializeUser(function(obj, done) { done(null, 'three'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should deserialize user', function() { expect(user).to.equal('two'); }); }); describe('with three deserializers, the first of which passes and the second of which does not deserialize by no argument', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done('pass'); }); authenticator.deserializeUser(function(obj, done) { done(null); }); authenticator.deserializeUser(function(obj, done) { done(null, 'three'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should deserialize user', function() { expect(user).to.equal('three'); }); }); describe('with three deserializers, the first of which passes and the second of which does not deserialize by undefined', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done('pass'); }); authenticator.deserializeUser(function(obj, done) { done(null, undefined); }); authenticator.deserializeUser(function(obj, done) { done(null, 'three'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should deserialize user', function() { expect(user).to.equal('three'); }); }); describe('with three deserializers, the first of which passes and the second of which invalidates session by false', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done('pass'); }); authenticator.deserializeUser(function(obj, done) { done(null, false); }); authenticator.deserializeUser(function(obj, done) { done(null, 'three'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should invalidate session', function() { expect(user).to.be.false; }); }); describe('with three deserializers, the first of which passes and the second of which invalidates session by null', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(obj, done) { done('pass'); }); authenticator.deserializeUser(function(obj, done) { done(null, null); }); authenticator.deserializeUser(function(obj, done) { done(null, 'three'); }); var error, user; before(function(done) { authenticator.deserializeUser({ id: '1', username: 'jared' }, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should invalidate session', function() { expect(user).to.be.false; }); }); describe('with one deserializer that takes request as argument', function() { var authenticator = new Authenticator(); authenticator.deserializeUser(function(req, obj, done) { if (req.url !== '/foo') { return done(new Error('incorrect req argument')); } done(null, obj.username); }); var error, user; before(function(done) { var req = { url: '/foo' }; authenticator.deserializeUser({ id: '1', username: 'jared' }, req, function(err, u) { error = err; user = u; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should deserialize user', function() { expect(user).to.equal('jared'); }); }); }); describe('#transformAuthInfo', function() { describe('without transforms', function() { var authenticator = new Authenticator(); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should not transform info', function() { expect(Object.keys(obj)).to.have.length(2); expect(obj.clientId).to.equal('1'); expect(obj.scope).to.equal('write'); }); }); describe('with one transform', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(info, done) { done(null, { clientId: info.clientId, client: { name: 'Foo' }}); }); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should not transform info', function() { expect(Object.keys(obj)).to.have.length(2); expect(obj.clientId).to.equal('1'); expect(obj.client.name).to.equal('Foo'); expect(obj.scope).to.be.undefined; }); }); describe('with one transform that encounters an error', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(info, done) { done(new Error('something went wrong')); }); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not transform info', function() { expect(obj).to.be.undefined; }); }); describe('with one transform that throws an exception', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(info, done) { throw new Error('something went horribly wrong'); }); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went horribly wrong'); }); it('should not transform info', function() { expect(obj).to.be.undefined; }); }); describe('with one sync transform', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(info) { return { clientId: info.clientId, client: { name: 'Foo' }}; }); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should not transform info', function() { expect(Object.keys(obj)).to.have.length(2); expect(obj.clientId).to.equal('1'); expect(obj.client.name).to.equal('Foo'); expect(obj.scope).to.be.undefined; }); }); describe('with three transform, the first of which passes and the second of which transforms', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(info, done) { done('pass'); }); authenticator.transformAuthInfo(function(info, done) { done(null, { clientId: info.clientId, client: { name: 'Two' }}); }); authenticator.transformAuthInfo(function(info, done) { done(null, { clientId: info.clientId, client: { name: 'Three' }}); }); var error, obj; before(function(done) { authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should not transform info', function() { expect(Object.keys(obj)).to.have.length(2); expect(obj.clientId).to.equal('1'); expect(obj.client.name).to.equal('Two'); expect(obj.scope).to.be.undefined; }); }); describe('with one transform that takes request as argument', function() { var authenticator = new Authenticator(); authenticator.transformAuthInfo(function(req, info, done) { if (req.url !== '/foo') { return done(new Error('incorrect req argument')); } done(null, { clientId: info.clientId, client: { name: 'Foo' }}); }); var error, obj; before(function(done) { var req = { url: '/foo' }; authenticator.transformAuthInfo({ clientId: '1', scope: 'write' }, req, function(err, o) { error = err; obj = o; done(); }); }); it('should not error', function() { expect(error).to.be.null; }); it('should not transform info', function() { expect(Object.keys(obj)).to.have.length(2); expect(obj.clientId).to.equal('1'); expect(obj.client.name).to.equal('Foo'); expect(obj.scope).to.be.undefined; }); }); }); }); passport-0.6.0/test/bootstrap/000077500000000000000000000000001424171417100163665ustar00rootroot00000000000000passport-0.6.0/test/bootstrap/node.js000066400000000000000000000002771424171417100176570ustar00rootroot00000000000000var chai = require('chai'); chai.use(require('chai-connect-middleware')); chai.use(require('chai-passport-strategy')); global.$require = require('proxyquire'); global.expect = chai.expect; passport-0.6.0/test/http/000077500000000000000000000000001424171417100153305ustar00rootroot00000000000000passport-0.6.0/test/http/request.test.js000066400000000000000000001033521424171417100203400ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var request = require('../../lib/http/request') , Passport = require('../..').Passport; describe('http.ServerRequest', function() { // TODO: Test that these are extended by initialize/authenticate /* describe('prototoype', function() { var req = new http.IncomingMessage(); it('should be extended with login', function() { expect(req.login).to.be.an('function'); expect(req.login).to.equal(req.logIn); }); it('should be extended with logout', function() { expect(req.logout).to.be.an('function'); expect(req.logout).to.equal(req.logOut); }); it('should be extended with isAuthenticated', function() { expect(req.isAuthenticated).to.be.an('function'); }); it('should be extended with isUnauthenticated', function() { expect(req.isUnauthenticated).to.be.an('function'); }); }); */ describe('#login', function() { describe('not establishing a session', function() { var passport = new Passport(); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req.session = {}; req.session['passport'] = {}; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, { session: false }, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); it('should not serialize user', function() { expect(req.session['passport'].user).to.be.undefined; }); }); describe('not establishing a session and setting custom user property', function() { var passport = new Passport(); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req.session = {}; req.session['passport'] = {}; req._userProperty = 'currentUser'; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, { session: false }, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should not set user', function() { expect(req.user).to.be.undefined; }); it('should set custom user', function() { expect(req.currentUser).to.be.an('object'); expect(req.currentUser.id).to.equal('1'); expect(req.currentUser.username).to.equal('root'); }); it('should not serialize user', function() { 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 Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req.session = {}; req.session['passport'] = {}; var user = { id: '1', username: 'root' }; req.login(user, { session: false }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); it('should not serialize user', function() { expect(req.session['passport'].user).to.be.undefined; }); }); describe('not establishing a session, without passport.initialize() middleware', function() { var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, { session: false }, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); }); describe('establishing a session', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session.regenerate = function(cb) { req.session = { id: '2' }; req.session.save = function(cb) { process.nextTick(cb); }; process.nextTick(cb); }; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); it('should serialize user', function() { expect(req.session['passport'].user).to.equal('1'); }); }); describe('establishing a session and not keeping previous session data', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { cart: [ '1', '2', ] }; Object.defineProperty(req.session, 'id', { value: '1' }); req.session.regenerate = function(cb) { req.session = { id: '2' }; req.session.save = function(cb) { process.nextTick(cb); }; process.nextTick(cb); }; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should not keep session data', function() { expect(req.session.cart).to.be.undefined; }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); it('should serialize user', function() { expect(req.session['passport'].user).to.equal('1'); }); }); describe('establishing a session and keeping previous session data', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { cart: [ '1', '2', ] }; Object.defineProperty(req.session, 'id', { value: '1' }); req.session.regenerate = function(cb) { req.session = { id: '2' }; req.session.save = function(cb) { process.nextTick(cb); }; process.nextTick(cb); }; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, { keepSessionInfo: true }, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should keep session data', function() { expect(req.session.cart).to.deep.equal([ '1', '2' ]); }); it('should set user', function() { expect(req.user).to.be.an('object'); expect(req.user.id).to.equal('1'); expect(req.user.username).to.equal('root'); }); it('should serialize user', function() { expect(req.session['passport'].user).to.equal('1'); }); }); describe('establishing a session and setting custom user property', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session.regenerate = function(cb) { req.session = { id: '2' }; req.session.save = function(cb) { process.nextTick(cb); }; process.nextTick(cb); } req._userProperty = 'currentUser'; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); it('should regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should not set user', function() { expect(req.user).to.be.undefined; }); it('should set custom user', function() { expect(req.currentUser).to.be.an('object'); expect(req.currentUser.id).to.equal('1'); expect(req.currentUser.username).to.equal('root'); }); it('should serialize user', function() { expect(req.session['passport'].user).to.equal('1'); }); }); describe('encountering an error when regenerating session', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session.regenerate = function(cb) { process.nextTick(function(){ cb(new Error('something went wrong')); }) } var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should not regenerate session', function() { expect(req.session.id).to.equal('1'); }); it('should not set user', function() { expect(req.user).to.be.null; }); it('should not serialize user', function() { expect(req.session['passport'].user).to.be.undefined; }); }); describe('encountering an error when serializing to session', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(new Error('something went wrong')); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); } var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should not set user', function() { expect(req.user).to.be.null; }); it('should not serialize user', function() { expect(req.session['passport']).to.be.undefined; }); }); describe('encountering an error when saving session', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session.regenerate = function(cb) { req.session = { id: '2' }; req.session.save = function(cb) { process.nextTick(function(){ cb(new Error('something went wrong')); }); }; process.nextTick(cb); } var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should not regenerate session', function() { expect(req.session.id).to.equal('2'); }); it('should not set user', function() { expect(req.user).to.be.null; }); it('should not serialize user', function() { expect(req.session['passport'].user).to.equal('1'); }); }); /* describe('establishing a session, without passport.initialize() middleware', function() { var req = new Object(); req.login = request.login; var user = { id: '1', username: 'root' }; it('should throw an exception', function() { expect(function() { req.login(user, function(err) {}); }).to.throw(Error, 'passport.initialize() middleware not in use'); }); }); */ describe('establishing a session, but not passing a callback argument', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = {}; req.session['passport'] = {}; var user = { id: '1', username: 'root' }; it('should throw an exception', function() { expect(function() { req.login(user); }).to.throw(Error, 'req#login requires a callback function'); }); }); describe('establishing a session without session support', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.login = request.login; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; var error; before(function(done) { var user = { id: '1', username: 'root' }; req.login(user, function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Login sessions require session support. Did you forget to use `express-session` middleware?'); }); }); }); describe('#logout', function() { describe('existing session', function() { var passport = new Passport(); 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._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(cb); }; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport']).to.be.undefined; }); }); describe('existing session and not keeping session data', function() { var passport = new Passport(); 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._sessionManager = passport._sm; req.session = { cart: [ '1', '2', ] }; Object.defineProperty(req.session, 'id', { value: '1' }); req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(cb); }; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport']).to.be.undefined; }); it('should keep session data', function() { expect(req.session.cart).to.be.undefined; }); }); describe('existing session and keeping session data', function() { var passport = new Passport(); 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._sessionManager = passport._sm; req.session = { cart: [ '1', '2', ] }; Object.defineProperty(req.session, 'id', { value: '1' }); req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(cb); }; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); }; var error; before(function(done) { req.logout({ keepSessionInfo: true }, function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport'].user).to.be.undefined; }); it('should keep session data', function() { expect(req.session.cart).to.deep.equal([ '1', '2' ]); }); }); describe('existing session and clearing custom user property', function() { var passport = new Passport(); 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._userProperty = 'currentUser'; req._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(cb); }; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.currentUser).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport']).to.be.undefined; }); }); describe('existing session, without passport.initialize() middleware', function() { var req = new Object(); req.logout = request.logout; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req.user = { id: '1', username: 'root' }; req.logout(); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); }); describe('existing session, without passport.initialize() middleware, and invoked with a callback', function() { var req = new Object(); req.logout = request.logout; req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req.user = { id: '1', username: 'root' }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); }); describe('encountering an error saving existing session', function() { var passport = new Passport(); 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._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(function() { cb(new Error('something went wrong')); }); }; req.session.regenerate = function(cb) { req.session = { id: '2' }; process.nextTick(cb); }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport'].user).to.be.undefined; }); }); describe('encountering an error regenerating session', function() { var passport = new Passport(); 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._sessionManager = passport._sm; req.session = { id: '1' }; req.session['passport'] = {}; req.session['passport'].user = '1'; req.session.save = function(cb) { expect(req.session['passport'].user).to.be.undefined; process.nextTick(cb); }; req.session.regenerate = function(cb) { process.nextTick(function() { cb(new Error('something went wrong')); }); }; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); it('should clear user', function() { expect(req.user).to.be.null; }); it('should clear serialized user', function() { expect(req.session['passport'].user).to.be.undefined; }); }); describe('existing session, but not passing a callback argument', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.logout = request.logout; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '1'; it('should throw an exception', function() { expect(function() { req.logout(); }).to.throw(Error, 'req#logout requires a callback function'); }); }); describe('without session support', function() { var passport = new Passport(); passport.serializeUser(function(user, done) { done(null, user.id); }); var req = new Object(); req.logout = request.logout; req._passport = {}; req._passport.instance = passport; req._sessionManager = passport._sm; var error; before(function(done) { req.logout(function(err) { error = err; done(); }); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Login sessions require session support. Did you forget to use `express-session` middleware?'); }); }); }); describe('#isAuthenticated', function() { describe('with a user', function() { var req = new Object(); req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req.user = { id: '1', username: 'root' }; it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); }); describe('with a user set on custom property', function() { var req = new Object(); req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req.currentUser = { id: '1', username: 'root' }; req._passport = {}; req._passport.instance = {}; req._userProperty = 'currentUser'; it('should be authenticated', function() { expect(req.isAuthenticated()).to.be.true; expect(req.isUnauthenticated()).to.be.false; }); }); describe('without a user', function() { var req = new Object(); req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); }); describe('with a null user', function() { var req = new Object(); req.isAuthenticated = request.isAuthenticated; req.isUnauthenticated = request.isUnauthenticated; req.user = null; it('should not be authenticated', function() { expect(req.isAuthenticated()).to.be.false; expect(req.isUnauthenticated()).to.be.true; }); }); }); }); passport-0.6.0/test/middleware/000077500000000000000000000000001424171417100164665ustar00rootroot00000000000000passport-0.6.0/test/middleware/authenticate.error.callback.test.js000066400000000000000000000044371424171417100253530ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('error with callback', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.error(new Error('something is wrong')); }; var passport = new Passport(); passport.use('error', new Strategy()); var request, error, user; before(function(done) { function callback(e, u) { error = e; user = u; done(); } chai.connect.use(authenticate(passport, 'error', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should pass error to callback', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something is wrong'); }); it('should pass user as undefined to callback', function() { expect(request.user).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('error with callback and options passed to middleware', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.error(new Error('something is wrong')); }; var passport = new Passport(); passport.use('error', new Strategy()); var request, error, user; before(function(done) { function callback(e, u) { error = e; user = u; done(); } chai.connect.use(authenticate(passport, 'error', { foo: 'bar' }, callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should pass error to callback', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something is wrong'); }); it('should pass user as undefined to callback', function() { expect(request.user).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.error.test.js000066400000000000000000000020621424171417100236100ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('error', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.error(new Error('something is wrong')); }; var passport = new Passport(); passport.use('error', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'error')) .req(function(req) { request = req; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something is wrong'); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.fail.callback.test.js000066400000000000000000000177041424171417100251360ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('fail with callback', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user; before(function(done) { function callback(e, u) { error = e; user = u; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback, passing info', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user, info, status; before(function(done) { function callback(e, u, i, s) { error = e; user = u; info = i; status = s; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass info to callback', function() { expect(info).to.be.an('object'); expect(info.message).to.equal('Invalid password'); }); it('should pass status to callback', function() { expect(status).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback, passing info and status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }, 403); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user, info, status; before(function(done) { function callback(e, u, i, s) { error = e; user = u; info = i; status = s; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass info to callback', function() { expect(info).to.be.an('object'); expect(info.message).to.equal('Invalid password'); }); it('should pass status to callback', function() { expect(status).to.equal(403); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback, passing challenge', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Bearer challenge'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenge to callback', function() { expect(challenge).to.equal('Bearer challenge'); }); it('should pass status to callback', function() { expect(status).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback, passing challenge and status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Bearer challenge', 403); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenge to callback', function() { expect(challenge).to.equal('Bearer challenge'); }); it('should pass status to callback', function() { expect(status).to.equal(403); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback, passing status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(402); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, 'fail', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenge to callback', function() { expect(challenge).to.be.undefined; }); it('should pass status to callback', function() { expect(status).to.equal(402); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('fail with callback and options passed to middleware', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, error, user; before(function(done) { function callback(e, u) { error = e; user = u; done(); } chai.connect.use(authenticate(passport, 'fail', { foo: 'bar' }, callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.fail.flash.test.js000066400000000000000000000712421424171417100244740ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('using strategy that specifies message', function() { describe('fail with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Invalid password'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'info' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Invalid password'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'notice', message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); }); describe('using strategy that specifies message and type', function() { describe('fail with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ type: 'notice', message: 'Invite required' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Invite required'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ type: 'notice', message: 'Invite required' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'info' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Invite required'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ type: 'notice', message: 'Invite required' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ type: 'notice', message: 'Invite required' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ type: 'notice', message: 'Invite required' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'info', message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); }); describe('using strategy that specifies message as string', function() { describe('fail with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Access denied'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Access denied'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Access denied'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'info' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Access denied'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Access denied'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Access denied'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Access denied'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'notice', message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); }); describe('using strategy that does not specify message', function() { describe('fail with flash message left up to strategy', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not flash message', function() { expect(request.message).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message left up to strategy using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'info' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not flash message', function() { expect(request.message).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message specified by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message specified by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with flash message specified by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureFlash: { type: 'notice', message: 'Try again' }, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Try again'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); }); }); passport-0.6.0/test/middleware/authenticate.fail.message.test.js000066400000000000000000000124621424171417100250220ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('fail with message set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureMessage: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with message set by route that is added to messages', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureMessage: 'Wrong credentials', failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; req.session.messages = [ 'I exist!' ]; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should add message to session', function() { expect(request.session.messages).to.have.length(2); expect(request.session.messages[0]).to.equal('I exist!'); expect(request.session.messages[1]).to.equal('Wrong credentials'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with message set by strategy', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureMessage: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Invalid password'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with message set by strategy with extra info', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid password', scope: 'read' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureMessage: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.session = {}; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Invalid password'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); }); passport-0.6.0/test/middleware/authenticate.fail.multi.test.js000066400000000000000000000245421424171417100245320ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('with multiple strategies, all of which fail, and responding with unauthorized status', function() { function BasicStrategy() { } BasicStrategy.prototype.authenticate = function(req) { this.fail('BASIC challenge'); }; function DigestStrategy() { } DigestStrategy.prototype.authenticate = function(req) { this.fail('DIGEST challenge'); }; function NoChallengeStrategy() { } NoChallengeStrategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('basic', new BasicStrategy()); passport.use('digest', new DigestStrategy()); passport.use('no-challenge', new NoChallengeStrategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, ['basic', 'no-challenge', 'digest'])) .req(function(req) { request = req; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(401); expect(response.body).to.equal('Unauthorized'); }); it('should set authenticate header on response', function() { var val = response.getHeader('WWW-Authenticate'); expect(val).to.be.an('array'); expect(val).to.have.length(2); expect(val[0]).to.equal('BASIC challenge'); expect(val[1]).to.equal('DIGEST challenge'); }); }); describe('with multiple strategies, all of which fail, and responding with specified status', function() { function BasicStrategy() { } BasicStrategy.prototype.authenticate = function(req) { this.fail('BASIC challenge', 400); }; function BearerStrategy() { } BearerStrategy.prototype.authenticate = function(req) { this.fail('BEARER challenge', 403); }; function NoChallengeStrategy() { } NoChallengeStrategy.prototype.authenticate = function(req) { this.fail(402); }; var passport = new Passport(); passport.use('basic', new BasicStrategy()); passport.use('bearer', new BearerStrategy()); passport.use('no-challenge', new NoChallengeStrategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, ['basic', 'no-challenge', 'bearer'])) .req(function(req) { request = req; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(400); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.equal('Bad Request'); }); }); describe('with multiple strategies, all of which fail, and flashing message', function() { function StrategyA() { } StrategyA.prototype.authenticate = function(req) { this.fail('A message'); }; function StrategyB() { } StrategyB.prototype.authenticate = function(req) { this.fail('B message'); }; var passport = new Passport(); passport.use('a', new StrategyA()); passport.use('b', new StrategyB()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, ['a', 'b'], { failureFlash: true, failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should flash message', function() { expect(request.message.type).to.equal('error'); expect(request.message.msg).to.equal('A message'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('with multiple strategies, all of which fail with unauthorized status, and invoking callback', function() { function BasicStrategy() { } BasicStrategy.prototype.authenticate = function(req) { this.fail('BASIC challenge'); }; function DigestStrategy() { } DigestStrategy.prototype.authenticate = function(req) { this.fail('DIGEST challenge'); }; function NoChallengeStrategy() { } NoChallengeStrategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('basic', new BasicStrategy()); passport.use('digest', new DigestStrategy()); passport.use('no-challenge', new NoChallengeStrategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, ['basic', 'no-challenge', 'digest'], callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenges to callback', function() { expect(challenge).to.be.an('array'); expect(challenge).to.have.length(3); expect(challenge[0]).to.equal('BASIC challenge'); expect(challenge[1]).to.be.undefined; expect(challenge[2]).to.equal('DIGEST challenge'); }); it('should pass statuses to callback', function() { expect(status).to.be.an('array'); expect(status).to.have.length(3); expect(status[0]).to.be.undefined; expect(status[1]).to.be.undefined; expect(status[2]).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('with multiple strategies, all of which fail with specific status, and invoking callback', function() { function BasicStrategy() { } BasicStrategy.prototype.authenticate = function(req) { this.fail('BASIC challenge', 400); }; function BearerStrategy() { } BearerStrategy.prototype.authenticate = function(req) { this.fail('BEARER challenge', 403); }; function NoChallengeStrategy() { } NoChallengeStrategy.prototype.authenticate = function(req) { this.fail(402); }; var passport = new Passport(); passport.use('basic', new BasicStrategy()); passport.use('bearer', new BearerStrategy()); passport.use('no-challenge', new NoChallengeStrategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, ['basic', 'no-challenge', 'bearer'], callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenges to callback', function() { expect(challenge).to.be.an('array'); expect(challenge).to.have.length(3); expect(challenge[0]).to.equal('BASIC challenge'); expect(challenge[1]).to.be.undefined; expect(challenge[2]).to.equal('BEARER challenge'); }); it('should pass statuses to callback', function() { expect(status).to.be.an('array'); expect(status).to.have.length(3); expect(status[0]).to.equal(400); expect(status[1]).to.equal(402); expect(status[2]).to.equal(403); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); describe('with single strategy in list, which fails with unauthorized status, and invoking callback', function() { function BasicStrategy() { } BasicStrategy.prototype.authenticate = function(req) { this.fail('BASIC challenge'); }; var passport = new Passport(); passport.use('basic', new BasicStrategy()); var request, error, user, challenge, status; before(function(done) { function callback(e, u, c, s) { error = e; user = u; challenge = c; status = s; done(); } chai.connect.use(authenticate(passport, ['basic'], callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass false to callback', function() { expect(user).to.equal(false); }); it('should pass challenges to callback', function() { expect(challenge).to.be.an('array'); expect(challenge).to.have.length(1); expect(challenge[0]).to.equal('BASIC challenge'); }); it('should pass statuses to callback', function() { expect(status).to.be.an('array'); expect(status).to.have.length(1); expect(status[0]).to.be.undefined; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.fail.test.js000066400000000000000000000307301424171417100233750ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'fail')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(401); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.equal('Unauthorized'); }); }); describe('fail with redirect', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failureRedirect: 'http://www.example.com/login' })) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/login'); }); }); describe('fail with challenge', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('MOCK challenge'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'fail')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(401); expect(response.body).to.equal('Unauthorized'); }); it('should set authenticate header on response', function() { var val = response.getHeader('WWW-Authenticate'); expect(val).to.be.an('array'); expect(val).to.have.length(1); expect(val[0]).to.equal('MOCK challenge'); }); }); describe('fail with challenge and status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('MOCK challenge', 403); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'fail')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(403); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.equal('Forbidden'); }); }); describe('fail with status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(400); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'fail')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should respond', function() { expect(response.statusCode).to.equal(400); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.equal('Bad Request'); }); }); describe('fail with error', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Unauthorized'); expect(error.status).to.equal(401); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(401); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.be.undefined; }); }); describe('fail with error, passing info to fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Invalid credentials' }); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Unauthorized'); expect(error.status).to.equal(401); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(401); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.be.undefined; }); }); describe('fail with error, passing info and status to fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail({ message: 'Multiple credentials' }, 400); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Bad Request'); expect(error.status).to.equal(400); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(400); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.be.undefined; }); }); describe('fail with error, passing challenge to fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Bearer challenge'); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Unauthorized'); expect(error.status).to.equal(401); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(401); expect(response.body).to.be.undefined; }); it('should set authenticate header on response', function() { var val = response.getHeader('WWW-Authenticate'); expect(val).to.be.an('array'); expect(val).to.have.length(1); expect(val[0]).to.equal('Bearer challenge'); }); }); describe('fail with error, passing challenge and status to fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail('Bearer challenge', 403); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Forbidden'); expect(error.status).to.equal(403); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(403); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.be.undefined; }); }); describe('fail with error, passing status to fail', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.fail(402); }; var passport = new Passport(); passport.use('fail', new Strategy()); var request, response, error; before(function(done) { chai.connect.use('express', authenticate(passport, 'fail', { failWithError: true })) .req(function(req) { request = req; }) .res(function(res) { response = res; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.constructor.name).to.equal('AuthenticationError'); expect(error.message).to.equal('Payment Required'); expect(error.status).to.equal(402); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set body of response', function() { expect(response.statusCode).to.equal(402); expect(response.getHeader('WWW-Authenticate')).to.be.undefined; expect(response.body).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.pass.test.js000066400000000000000000000017141424171417100234300ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('pass', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.pass(); }; var passport = new Passport(); passport.use('pass', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'pass')) .req(function(req) { request = req; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not set user', function() { expect(request.user).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.redirect.test.js000066400000000000000000000075341424171417100242710ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('redirect', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.redirect('http://www.example.com/idp'); }; var passport = new Passport(); passport.use('redirect', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'redirect')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/idp'); expect(response.getHeader('Content-Length')).to.equal('0'); }); }); describe('redirect with status', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.redirect('http://www.example.com/idp', 303); }; var passport = new Passport(); passport.use('redirect', new Strategy()); var request, response; before(function(done) { chai.connect.use(authenticate(passport, 'redirect')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(303); expect(response.getHeader('Location')).to.equal('http://www.example.com/idp'); expect(response.getHeader('Content-Length')).to.equal('0'); }); }); describe('redirect using framework function', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.redirect('http://www.example.com/idp'); }; var passport = new Passport(); passport.use('redirect', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'redirect')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/idp'); }); }); describe('redirect with status using framework function', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { this.redirect('http://www.example.com/idp', 303); }; var passport = new Passport(); passport.use('redirect', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'redirect')) .req(function(req) { request = req; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(303); expect(response.getHeader('Location')).to.equal('http://www.example.com/idp'); }); }); }); passport-0.6.0/test/middleware/authenticate.success.callback.test.js000066400000000000000000000057011424171417100256650ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('success with callback', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error, user, info; before(function(done) { function callback(e, u, i) { error = e; user = u; info = i; done(); } chai.connect.use(authenticate(passport, 'success', callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass user to callback', function() { expect(user).to.be.an('object'); expect(user.id).to.equal('1'); expect(user.username).to.equal('jaredhanson'); }); it('should pass info to callback', function() { expect(info).to.be.an('object'); expect(info.message).to.equal('Hello'); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); it('should not set authInfo on request', function() { expect(request.authInfo).to.be.undefined; }); }); describe('success with callback and options passed to middleware', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error, user, info; before(function(done) { function callback(e, u, i) { error = e; user = u; info = i; done(); } chai.connect.use(authenticate(passport, 'success', { foo: 'bar' }, callback)) .req(function(req) { request = req; }) .dispatch(); }); it('should not error', function() { expect(error).to.be.null; }); it('should pass user to callback', function() { expect(user).to.be.an('object'); expect(user.id).to.equal('1'); expect(user.username).to.equal('jaredhanson'); }); it('should pass info to callback', function() { expect(info).to.be.an('object'); expect(info.message).to.equal('Hello'); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); it('should not set authInfo on request', function() { expect(request.authInfo).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.success.flash.test.js000066400000000000000000001055361424171417100252350ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('using strategy that specifies message', function() { describe('success with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Welcome!'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'info' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Welcome!'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: 'Login complete', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Login complete'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { message: 'OK' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('OK'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'notice', message: 'Last login was yesterday' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Last login was yesterday'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); }); describe('using strategy that specifies message and type', function() { describe('success with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { type: 'info', message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Hello'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { type: 'info', message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'ok' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('ok'); expect(request.message.msg).to.equal('Hello'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { type: 'info', message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: 'Success!', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Success!'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { type: 'info', message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { message: 'Okay' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Okay'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { type: 'info', message: 'Hello' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'warn', message: 'Last login from far away place' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('warn'); expect(request.message.msg).to.equal('Last login from far away place'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); }); describe('using strategy that specifies message as string', function() { describe('success with flash message', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, 'Greetings'); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Greetings'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, 'Greetings'); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'info' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('info'); expect(request.message.msg).to.equal('Greetings'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, 'Greetings'); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: 'Login complete', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Login complete'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, 'Greetings'); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { message: 'OK' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('OK'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message overridden by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, 'Greetings'); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'notice', message: 'Last login was yesterday' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Last login was yesterday'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); }); describe('using strategy that does not specify message', function() { describe('success with flash message left up to strategy', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should not flash message', function() { expect(request.message).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message left up to strategy using type set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'info' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should not flash message', function() { expect(request.message).to.be.undefined; }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message specified by route as string', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: 'Login complete', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('Login complete'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message specified by route using options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { message: 'OK' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('success'); expect(request.message.msg).to.equal('OK'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with flash message specified by route using options with type', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successFlash: { type: 'notice', message: 'Last login was yesterday' }, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; req.flash = function(type, msg) { this.message = { type: type, msg: msg }; }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should flash message', function() { expect(request.message.type).to.equal('notice'); expect(request.message.msg).to.equal('Last login was yesterday'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); }); }); passport-0.6.0/test/middleware/authenticate.success.info.test.js000066400000000000000000000132451424171417100250660ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('success with info', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { clientId: '123', scope: 'read' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(2); expect(request.authInfo.clientId).to.equal('123'); expect(request.authInfo.scope).to.equal('read'); }); }); describe('success with info that is transformed', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { clientId: '123', scope: 'read' }); }; var passport = new Passport(); passport.use('success', new Strategy()); passport.transformAuthInfo(function(info, done) { done(null, { clientId: info.clientId, client: { name: 'Foo' }, scope: info.scope }); }); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(3); expect(request.authInfo.clientId).to.equal('123'); expect(request.authInfo.client.name).to.equal('Foo'); expect(request.authInfo.scope).to.equal('read'); }); }); describe('success with info, but transform that encounters an error', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { clientId: '123', scope: 'read' }); }; var passport = new Passport(); passport.use('success', new Strategy()); passport.transformAuthInfo(function(info, done) { done(new Error('something went wrong')); }); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); describe('success with info, but option that disables info', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { clientId: '123', scope: 'read' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success', { authInfo: false })) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.success.message.test.js000066400000000000000000000147521424171417100255630ustar00rootroot00000000000000/* global describe, it, expect, before */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('success with message set by route', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successMessage: 'Login complete', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Login complete'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with message set by route that is added to messages', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successMessage: 'Login complete', successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.session.messages = [ 'I exist!' ]; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should add message to session', function() { expect(request.session.messages).to.have.length(2); expect(request.session.messages[0]).to.equal('I exist!'); expect(request.session.messages[1]).to.equal('Login complete'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with message set by strategy', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successMessage: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Welcome!'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with message set by strategy with extra info', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user, { message: 'Welcome!', scope: 'read' }); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successMessage: true, successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.session = {}; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should add message to session', function() { expect(request.session.messages).to.have.length(1); expect(request.session.messages[0]).to.equal('Welcome!'); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); }); passport-0.6.0/test/middleware/authenticate.success.multi.test.js000066400000000000000000000050001424171417100252530ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('with multiple strategies, the first of which succeeds', function() { function StrategyA() { } StrategyA.prototype.authenticate = function(req) { this.success({ username: 'bob-a' }); }; function StrategyB() { } StrategyB.prototype.authenticate = function(req) { this.success({ username: 'bob-b' }); }; var passport = new Passport(); passport.use('a', new StrategyA()); passport.use('b', new StrategyB()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, ['a', 'b'])) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.username).to.equal('bob-a'); }); }); describe('with multiple strategies, the second of which succeeds', function() { function StrategyA() { } StrategyA.prototype.authenticate = function(req) { this.fail('A challenge'); }; function StrategyB() { } StrategyB.prototype.authenticate = function(req) { this.success({ username: 'bob-b' }); }; var passport = new Passport(); passport.use('a', new StrategyA()); passport.use('b', new StrategyB()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, ['a', 'b'])) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.username).to.equal('bob-b'); }); }); }); passport-0.6.0/test/middleware/authenticate.success.test.js000066400000000000000000000230621424171417100241320ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { describe('success', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); }); describe('success that assigns a specific property', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success', { assignProperty: 'account' })) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should set account', function() { expect(request.account).to.be.an('object'); expect(request.account.id).to.equal('1'); expect(request.account.username).to.equal('jaredhanson'); }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); describe('success with strategy-specific options', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; if (options.scope == 'email') { user.email = 'jaredhanson@example.com'; } this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success', { scope: 'email' })) .req(function(req) { request = req; req.logIn = function(user, options, done) { if (options.scope != 'email') { return done(new Error('invalid options')); } this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); expect(request.user.email).to.equal('jaredhanson@example.com'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); }); describe('success with redirect', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successRedirect: 'http://www.example.com/account' })) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/account'); }); }); describe('success with return to previous location', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successReturnToOrRedirect: 'http://www.example.com/default' })) .req(function(req) { request = req; req.session = { returnTo: 'http://www.example.com/return' }; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/return'); }); it('should move return to from session', function() { expect(request.session.returnTo).to.be.undefined; }); }); describe('success with return to default location', function() { function Strategy() { } Strategy.prototype.authenticate = function(req, options) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, response; before(function(done) { chai.connect.use('express', authenticate(passport, 'success', { successReturnToOrRedirect: 'http://www.example.com/default' })) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .end(function(res) { response = res; done(); }) .dispatch(); }); it('should set user', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('1'); expect(request.user.username).to.equal('jaredhanson'); }); it('should set authInfo', function() { expect(request.authInfo).to.be.an('object'); expect(Object.keys(request.authInfo)).to.have.length(0); }); it('should redirect', function() { expect(response.statusCode).to.equal(302); expect(response.getHeader('Location')).to.equal('http://www.example.com/default'); }); }); describe('success, but login that encounters an error', function() { function Strategy() { } Strategy.prototype.authenticate = function(req) { var user = { id: '1', username: 'jaredhanson' }; this.success(user); }; var passport = new Passport(); passport.use('success', new Strategy()); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'success')) .req(function(req) { request = req; req.logIn = function(user, options, done) { done(new Error('something went wrong')); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/authenticate.test.js000066400000000000000000000023641424171417100224650ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , authenticate = require('../../lib/middleware/authenticate') , Passport = require('../..').Passport; describe('middleware/authenticate', function() { it('should be named authenticate', function() { expect(authenticate().name).to.equal('authenticate'); }); describe('with unknown strategy', function() { var passport = new Passport(); var request, error; before(function(done) { chai.connect.use(authenticate(passport, 'foo')) .req(function(req) { request = req; req.logIn = function(user, options, done) { this.user = user; done(); }; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Unknown authentication strategy "foo"'); }); it('should not set user', function() { expect(request.user).to.be.undefined; }); it('should not set authInfo', function() { expect(request.authInfo).to.be.undefined; }); }); }); passport-0.6.0/test/middleware/initialize.test.js000066400000000000000000000132651424171417100221520ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , initialize = require('../../lib/middleware/initialize') , Passport = require('../..').Passport; describe('middleware/initialize', function() { it('should be named initialize', function() { expect(initialize().name).to.equal('initialize'); }); describe('handling a request without a session', function() { var passport = new Passport(); var request, error; before(function(done) { chai.connect.use(initialize(passport)) .req(function(req) { request = req; }) .next(function(err) { error = err; done(); }) .dispatch(); }); 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() { var passport = new Passport(); var request, error; before(function(done) { chai.connect.use(initialize(passport)) .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.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() { var passport = new Passport(); var request, error; before(function(done) { chai.connect.use(initialize(passport)) .req(function(req) { request = req; req.session = {}; req.session.passport = {}; req.session.passport.user = '123456'; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should maintain data within session', function() { expect(request.session.passport).to.be.an('object'); 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() { var passport = new Passport(); passport._key = 'authentication'; var request, error; before(function(done) { chai.connect.use(initialize(passport)) .req(function(req) { request = req; req.session = {}; req.session.authentication = {}; req.session.authentication.user = '123456'; }) .next(function(err) { error = err; done(); }) .dispatch(); }); it('should not error', function() { expect(error).to.be.undefined; }); it('should maintain data within session', function() { expect(request.session.authentication).to.be.an('object'); 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; }); }); }); passport-0.6.0/test/package.test.js000066400000000000000000000011721424171417100172610ustar00rootroot00000000000000/* global describe, it, expect */ var passport = require('..'); describe('passport', function() { it('should expose singleton authenticator', function() { expect(passport).to.be.an('object'); expect(passport).to.be.an.instanceOf(passport.Authenticator); }); it('should export constructors', function() { expect(passport.Authenticator).to.equal(passport.Passport); expect(passport.Authenticator).to.be.a('function'); expect(passport.Strategy).to.be.a('function'); }); it('should export strategies', function() { expect(passport.strategies.SessionStrategy).to.be.a('function'); }); }); passport-0.6.0/test/strategies/000077500000000000000000000000001424171417100165235ustar00rootroot00000000000000passport-0.6.0/test/strategies/session.pause.test.js000066400000000000000000000072201424171417100226370ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai'); describe('SessionStrategy', function() { describe('handling a request with a login session, pausing for deserialization', function() { var spy = []; var pause = function() { spy.push({ context: this, args: [].slice.call(arguments) }); return { resume: function() { spy.push({ context: this, args: [].slice.call(arguments) }); } }; } var SessionStrategy = $require('../../lib/strategies/session', { pause: pause }); var strategy = new SessionStrategy(function(user, req, done) { done(null, { id: user }); }); var request, pass = false; before(function(done) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate({ pauseStream: true }); }); it('should spy correctly', function() { expect(spy).to.have.length(2); }); it('should pass', function() { expect(pass).to.be.true; }); it('should set user on request', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('123456'); }); it('should maintain session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.equal('123456'); }); it('should pause request', function() { var s0 = spy[0]; expect(s0.args[0]).to.equal(request); }); it('should resume request', function() { var s1 = spy[1]; expect(s1.args[0]).to.equal(undefined); }); }); describe('handling a request with a login session that has been invalidated, pausing for deserialization', function() { var spy = []; var pause = function() { spy.push({ context: this, args: [].slice.call(arguments) }); return { resume: function() { spy.push({ context: this, args: [].slice.call(arguments) }); } }; } var SessionStrategy = $require('../../lib/strategies/session', { pause: pause }); var strategy = new SessionStrategy(function(user, req, done) { done(null, false); }); var request, pass = false; before(function(done) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate({ pauseStream: true }); }); it('should spy correctly', function() { expect(spy).to.have.length(2); }); it('should pass', function() { expect(pass).to.be.true; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); it('should remove user from session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.be.undefined; }); it('should pause request', function() { var s0 = spy[0]; expect(s0.args[0]).to.equal(request); }); it('should resume request', function() { var s1 = spy[1]; expect(s1.args[0]).to.equal(undefined); }); }); }); passport-0.6.0/test/strategies/session.test.js000066400000000000000000000160441424171417100215270ustar00rootroot00000000000000/* global describe, it, expect, before */ /* jshint expr: true */ var chai = require('chai') , SessionStrategy = require('../../lib/strategies/session'); describe('SessionStrategy', function() { var strategy = new SessionStrategy(); it('should be named session', function() { expect(strategy.name).to.equal('session'); }); describe('handling a request without a login session', function() { var request, pass = false; before(function(done) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req.session = {}; req.session['passport'] = {}; }) .authenticate(); }); it('should pass', function() { expect(pass).to.be.true; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); 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) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate(); }); it('should pass', function() { expect(pass).to.be.true; }); it('should set user on request', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal('123456'); }); it('should maintain session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.equal('123456'); }); }); 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) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = 0; }) .authenticate(); }); it('should pass', function() { expect(pass).to.be.true; }); it('should set user on request', function() { expect(request.user).to.be.an('object'); expect(request.user.id).to.equal(0); }); it('should maintain session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.equal(0); }); }); 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) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate(); }); it('should pass', function() { expect(pass).to.be.true; }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); it('should remove user from session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.be.undefined; }); }); 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) { chai.passport.use(strategy) .pass(function() { pass = true; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req._userProperty = 'currentUser'; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate(); }); it('should pass', function() { expect(pass).to.be.true; }); it('should not set "user" on request', function() { expect(request.user).to.be.undefined; }); it('should set "currentUser" on request', function() { expect(request.currentUser).to.be.an('object'); expect(request.currentUser.id).to.equal('123456'); }); }); 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) { chai.passport.use(strategy) .error(function(err) { error = err; done(); }) .req(function(req) { request = req; req._passport = {}; req._passport.instance = {}; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '123456'; }) .authenticate(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('something went wrong'); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); it('should maintain session', function() { expect(request.session['passport']).to.be.an('object'); expect(request.session['passport'].user).to.equal('123456'); }); }); describe('handling a request that lacks an authenticator', function() { var request, error; before(function(done) { chai.passport.use(strategy) .error(function(err) { error = err; done(); }) .req(function(req) { request = req; }) .authenticate(); }); it('should error', function() { expect(error).to.be.an.instanceOf(Error); expect(error.message).to.equal('Login sessions require session support. Did you forget to use `express-session` middleware?'); }); it('should not set user on request', function() { expect(request.user).to.be.undefined; }); }); });