diff --git a/.eslintignore b/.eslintignore index 1fda7392..4e3ef646 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ .git coverage/ -node_modules/ \ No newline at end of file +node_modules/ +var/ diff --git a/.eslintrc.js b/.eslintrc.js index b69e83d6..b743aac9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,24 +1,36 @@ module.exports = { env: { - // jest: true, - mocha: true, node: true, }, extends: [ 'airbnb-base', ], + overrides: [ + { + files: ['test/**'], + env: { + // jest: true, + mocha: true + }, + globals: { + expect: 'readonly' + }, + rules: { + // 'jest/no-disabled-tests': [2], + // 'jest/no-focused-tests': [2], + // 'jest/no-identical-title': [2], + // 'jest/prefer-to-have-length': [2], + // 'jest/valid-expect': [2], + } + } + ], plugins: [ // 'jest' ], rules: { - "comma-dangle": 0, - "no-underscore-dangle": 0, - "no-param-reassign": 0, - "prefer-destructuring": 0, - // 'jest/no-disabled-tests': [2], - // 'jest/no-focused-tests': [2], - // 'jest/no-identical-title': [2], - // 'jest/prefer-to-have-length': [2], - // 'jest/valid-expect': [2], + 'comma-dangle': 0, + 'no-underscore-dangle': 0, + 'no-param-reassign': 0, + 'prefer-destructuring': 0, } - }; \ No newline at end of file + }; diff --git a/.gitignore b/.gitignore index e705ba7d..0cf4fbc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ docs/ reports/ +var/ # Mac OS X .DS_Store diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index f1149ee7..00000000 --- a/.jshintrc +++ /dev/null @@ -1,18 +0,0 @@ -{ - "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 -} diff --git a/.npmignore b/.npmignore index aec28b93..8e3eed16 100644 --- a/.npmignore +++ b/.npmignore @@ -5,8 +5,8 @@ docs/ examples/ reports/ test/ +var/ .github/ -.jshintrc .travis.yml .gitlab-ci.yml diff --git a/Makefile b/Makefile index d148666d..d096498a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ view-docs: open ./docs/index.html view-cov: - open ./reports/coverage/lcov-report/index.html + open ./var/cov/index.html clean: clean-docs clean-cov -rm -r $(REPORTSDIR) diff --git a/README.md b/README.md index 45b91bc4..6fe2d5fa 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Passport Next aims to: * Follow [Semantic Versioning](https://semver.org/) * Keep an up to date CHANGELOG.md -**Passport Next does not aim to be backwards compatible with the upstream repositories. +**Passport Next does not aim to be backwards compatible with the upstream repositories. The changes required to keep up to date and functioning prohibit that so if you're migrating from the upstream modules please test your code thouroughly!** @@ -63,7 +63,7 @@ application must be configured. ```javascript passport.use(new LocalStrategy( function(username, password, done) { - User.findOne({ username: username }, function (err, user) { + User.findOne({ username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (!user.verifyPassword(password)) { return done(null, false); } @@ -108,7 +108,7 @@ persistent login sessions (recommended, but not required), `passport.session()` middleware must also be used. ```javascript -var app = express(); +const app = express(); app.use(require('serve-static')(__dirname + '/../../public')); app.use(require('cookie-parser')()); app.use(require('body-parser').urlencoded({ extended: true })); @@ -123,7 +123,7 @@ Passport provides an `authenticate()` function, which is used as route middleware to authenticate requests. ```javascript -app.post('/login', +app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); @@ -132,12 +132,12 @@ app.post('/login', #### Protect Routes When Using Sessions -Passport provides an `isAuthenticated()` function on the request object, which -is used to determine if the user has been authenticated and stored in the +Passport provides an `isAuthenticated()` function on the request object, which +is used to determine if the user has been authenticated and stored in the session. ```javascript -app.post('/some/protected/route', +app.post('/some/protected/route', function(req, res, next) { if(req.isAuthenticated()){ next(); @@ -147,7 +147,7 @@ app.post('/some/protected/route', }); ``` -For a more complete solution to handling unauthenticated users, see +For a more complete solution to handling unauthenticated users, see [connect-ensure-login](https://github.com/jaredhanson/connect-ensure-login), a middleware to ensure login sessions. diff --git a/lib/authenticator.js b/lib/authenticator.js index 913df9a9..96c70118 100644 --- a/lib/authenticator.js +++ b/lib/authenticator.js @@ -2,8 +2,6 @@ * Module dependencies. */ -/* eslint-disable no-underscore-dangle, camelcase, no-proto, no-shadow */ - const SessionStrategy = require('./strategies/session'); const SessionManager = require('./sessionmanager'); const connect = require('./framework/connect'); @@ -14,472 +12,473 @@ const connect = require('./framework/connect'); * * @api public */ -function Authenticator() { - this._key = 'passport'; - this._strategies = {}; - this._serializers = []; - this._deserializers = []; - this._infoTransformers = []; - this._framework = null; - this._userProperty = 'user'; - - this.init(); -} - -/** - * Initialize authenticator. - * - * @api protected - */ -Authenticator.prototype.init = function init() { - this.framework(connect()); - this.use(new SessionStrategy(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 use(name, strategy) { - if (!strategy) { - strategy = name; - name = strategy.name; +class Authenticator { + constructor() { + this._key = 'passport'; + this._strategies = {}; + this._serializers = []; + this._deserializers = []; + this._infoTransformers = []; + this._framework = null; + this._userProperty = 'user'; + + this.init(); } - 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 unuse(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 framework(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 initialize(options) { - options = options || {}; - this._userProperty = options.userProperty || 'user'; - - 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 authenticate(strategy, options, callback) { - return this._framework.authenticate(this, strategy, options, callback); -}; + /** + * Initialize authenticator. + * + * @api protected + */ + init() { + this.framework(connect()); + this.use(new SessionStrategy(this.deserializeUser.bind(this))); + this._sm = new SessionManager({ key: this._key }, this.serializeUser.bind(this)); + } -/** - * 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 authorize(strategy, options, callback) { - options = options || {}; - options.assignProperty = 'account'; + /** + * 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 + */ + use(name, strategy) { + if (!strategy) { + strategy = name; + name = strategy.name; + } + if (!name) { throw new Error('Authentication strategies must have a name'); } - const fn = this._framework.authorize || this._framework.authenticate; - return fn(this, strategy, options, callback); -}; + this._strategies[name] = strategy; + return this; + } -/** - * 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()); - * - * @param {Object} options - * @return {Function} middleware - * @api public - */ -Authenticator.prototype.session = function session(options) { - return this.authenticate('session', options); -}; + /** + * 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 + */ + unuse(name) { + delete this._strategies[name]; + return this; + } -/** - * Sets a custom SessionManager - * - * Examples: - * - * passport.sessionManager = new CustomSessionManager(); - * - * @api public - */ + /** + * 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 + */ + framework(fw) { + this._framework = fw; + return this; + } -Authenticator.prototype.sessionManager = function sessionManager(mgr) { - this._sm = mgr; - 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 + */ + initialize(options) { + options = options || {}; + this._userProperty = options.userProperty || 'user'; + + return this._framework.initialize(this, options); + } -/** - * Registers a function used to serialize user objects into the session. - * - * Examples: - * - * passport.serializeUser(function(user, done) { - * done(null, user.id); - * }); - * - * @api public - */ + /** + * 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 + */ + authenticate(strategy, options, callback) { + return this._framework.authenticate(this, strategy, options, callback); + } -// eslint-disable-next-line consistent-return -Authenticator.prototype.serializeUser = function serializeUser(fn, req, done) { - if (typeof fn === 'function') { - return this._serializers.push(fn); + /** + * 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 + */ + authorize(strategy, options, callback) { + options = options || {}; + options.assignProperty = 'account'; + + const fn = this._framework.authorize || this._framework.authenticate; + return fn(this, strategy, options, callback); } - // private implementation that traverses the chain of serializers, attempting - // to serialize a user - const user = fn; + /** + * 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()); + * + * @param {Object} options + * @return {Function} middleware + * @api public + */ + session(options) { + return this.authenticate('session', options); + } - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; + /** + * Sets a custom SessionManager + * + * Examples: + * + * passport.sessionManager = new CustomSessionManager(); + * + * @api public + */ + + sessionManager(mgr) { + this._sm = mgr; + return this; } - const stack = this._serializers; - // eslint-disable-next-line consistent-return - (function pass(i, err, obj) { - // serializers use 'pass' as an error to skip processing - if (err === 'pass') { - err = undefined; - } - // an error or serialized object was obtained, done - if (err || obj || obj === 0) { return done(err, obj); } + /** + * Registers a function used to serialize user objects into the session. + * + * Examples: + * + * passport.serializeUser(function(user, done) { + * done(null, user.id); + * }); + * + * @api public + */ - const layer = stack[i]; - if (!layer) { - return done(new Error('Failed to serialize user into session')); + // eslint-disable-next-line consistent-return + serializeUser(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 + const user = fn; - function serialized(e, o) { - pass(i + 1, e, o); + // For backwards compatibility + if (typeof req === 'function') { + done = req; + req = undefined; } - try { - const arity = layer.length; - if (arity === 3) { - layer(req, user, serialized); - } else { - layer(user, serialized); + const stack = this._serializers; + // eslint-disable-next-line consistent-return + (function pass(i, err, obj) { + // serializers use 'pass' as an error to skip processing + if (err === 'pass') { + err = undefined; } - } catch (e) { - return done(e); - } - }(0)); -}; + // an error or serialized object was obtained, done + if (err || obj || obj === 0) { return done(err, obj); } -/** - * 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 - */ + const layer = stack[i]; + if (!layer) { + return done(new Error('Failed to serialize user into session')); + } -// eslint-disable-next-line consistent-return -Authenticator.prototype.deserializeUser = function deserializeUser(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 - const obj = fn; + function serialized(e, o) { + pass(i + 1, e, o); + } - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; + try { + const arity = layer.length; + if (arity === 3) { + layer(req, user, serialized); + } else { + layer(user, serialized); + } + } catch (e) { + return done(e); + } + }(0)); } - const stack = this._deserializers; + /** + * 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 + */ + // eslint-disable-next-line consistent-return - (function pass(i, err, user) { - // deserializers use 'pass' as an error to skip processing - if (err === 'pass') { - 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); } - - const layer = stack[i]; - if (!layer) { - return done(new Error('Failed to deserialize user out of session')); + deserializeUser(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 + const obj = fn; - function deserialized(e, u) { - pass(i + 1, e, u); + // For backwards compatibility + if (typeof req === 'function') { + done = req; + req = undefined; } - try { - const arity = layer.length; - if (arity === 3) { - layer(req, obj, deserialized); - } else { - layer(obj, deserialized); + const stack = this._deserializers; + // eslint-disable-next-line consistent-return + (function pass(i, err, user) { + // deserializers use 'pass' as an error to skip processing + if (err === 'pass') { + 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); } + + const layer = stack[i]; + if (!layer) { + return done(new Error('Failed to deserialize user out of session')); } - } 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 - */ -// eslint-disable-next-line consistent-return -Authenticator.prototype.transformAuthInfo = function transformAuthInfo(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 - const info = fn; + function deserialized(e, u) { + pass(i + 1, e, u); + } - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; + try { + const arity = layer.length; + if (arity === 3) { + layer(req, obj, deserialized); + } else { + layer(obj, deserialized); + } + } catch (e) { + return done(e); + } + }(0)); } - const stack = this._infoTransformers; + /** + * 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 + */ + // eslint-disable-next-line consistent-return - (function pass(i, err, tinfo) { - // transformers use 'pass' as an error to skip processing - if (err === 'pass') { - err = undefined; - } - // an error or transformed info was obtained, done - if (err || tinfo) { return done(err, tinfo); } - - const 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); + transformAuthInfo(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 + const info = fn; - function transformed(e, t) { - pass(i + 1, e, t); + // For backwards compatibility + if (typeof req === 'function') { + done = req; + req = undefined; } - try { - const arity = layer.length; - if (arity === 1) { - // sync - const t = layer(info); - transformed(null, t); - } else if (arity === 3) { - layer(req, info, transformed); - } else { - layer(info, transformed); + const stack = this._infoTransformers; + // eslint-disable-next-line consistent-return + (function pass(i, err, tinfo) { + // transformers use 'pass' as an error to skip processing + if (err === 'pass') { + err = undefined; + } + // an error or transformed info was obtained, done + if (err || tinfo) { return done(err, tinfo); } + + const 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); } - } catch (e) { - return done(e); - } - }(0)); -}; -/** - * Return strategy with given `name`. - * - * @param {String} name - * @return {Strategy} - * @api private - */ -Authenticator.prototype._strategy = function _strategy(name) { - return this._strategies[name]; -}; + function transformed(e, t) { + pass(i + 1, e, t); + } + + try { + const arity = layer.length; + if (arity === 1) { + // sync + const 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 + */ + _strategy(name) { + return this._strategies[name]; + } +} /** * Expose `Authenticator`. diff --git a/lib/errors/authenticationerror.js b/lib/errors/authenticationerror.js index 68cf923e..cba0b8c1 100644 --- a/lib/errors/authenticationerror.js +++ b/lib/errors/authenticationerror.js @@ -1,23 +1,17 @@ /** * `AuthenticationError` error. * - * @constructor * @api private */ - -/* eslint-disable no-proto, no-proto, no-caller, no-restricted-properties */ - -function AuthenticationError(message, status) { - Error.call(this); - Error.captureStackTrace(this, arguments.callee); - this.name = 'AuthenticationError'; - this.message = message; - this.status = status || 401; +class AuthenticationError extends Error { + constructor(message, status) { + super(); + Error.captureStackTrace(this, AuthenticationError); + this.name = 'AuthenticationError'; + this.message = message; + this.status = status || 401; + } } -// Inherit from `Error`. -AuthenticationError.prototype.__proto__ = Error.prototype; - - // Expose constructor. module.exports = AuthenticationError; diff --git a/lib/framework/connect.js b/lib/framework/connect.js index cd625249..c8485461 100644 --- a/lib/framework/connect.js +++ b/lib/framework/connect.js @@ -1,7 +1,6 @@ /** * Module dependencies. */ -/* eslint-disable camelcase, no-proto, no-shadow */ const initialize = require('../middleware/initialize'); const authenticate = require('../middleware/authenticate'); diff --git a/lib/http/request.js b/lib/http/request.js index ccd46eef..6d3a0e70 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -2,10 +2,10 @@ * Module dependencies. */ -// var http = require('http') +// const http = require('http') // , req = http.IncomingMessage.prototype; -/* eslint-disable no-multi-assign, camelcase, no-proto, no-shadow */ +/* eslint-disable no-multi-assign */ const req = exports = module.exports = {}; diff --git a/lib/middleware/authenticate.js b/lib/middleware/authenticate.js index 84dcaaa8..33f43fba 100644 --- a/lib/middleware/authenticate.js +++ b/lib/middleware/authenticate.js @@ -2,7 +2,7 @@ * Module dependencies. */ -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const http = require('http'); const AuthenticationError = require('../errors/authenticationerror'); @@ -116,9 +116,8 @@ module.exports = function authenticate(passport, name, options, callback) { // Strategies are ordered by priority. For the purpose of flashing a // message, the first failure will be displayed. - let failure = failures[0] || {}; - let challenge = failure.challenge || {}; - let msg; + const failure = failures[0] || {}; + const challenge = failure.challenge || {}; if (options.failureFlash) { let flash = options.failureFlash; @@ -128,13 +127,13 @@ module.exports = function authenticate(passport, name, options, callback) { flash.type = flash.type || 'error'; const type = flash.type || challenge.type || 'error'; - msg = flash.message || challenge.message || challenge; + const msg = flash.message || challenge.message || challenge; if (typeof msg === 'string') { req.flash(type, msg); } } if (options.failureMessage) { - msg = options.failureMessage; + let msg = options.failureMessage; if (typeof msg === 'boolean') { msg = challenge.message || challenge; } @@ -154,18 +153,12 @@ module.exports = function authenticate(passport, name, options, callback) { // will be included in the response. const rchallenge = []; let rstatus; - let status; - // eslint-disable-next-line no-plusplus - for (let j = 0, len = failures.length; j < len; j++) { - failure = failures[j]; - challenge = failure.challenge; - status = failure.status; - + failures.forEach(({ challenge, status }) => { rstatus = rstatus || status; if (typeof challenge === 'string') { rchallenge.push(challenge); } - } + }); res.statusCode = rstatus || 401; if (res.statusCode === 401 && rchallenge.length) { diff --git a/lib/middleware/initialize.js b/lib/middleware/initialize.js index 17e159b4..dd87f893 100644 --- a/lib/middleware/initialize.js +++ b/lib/middleware/initialize.js @@ -42,7 +42,7 @@ const IncomingMessageExt = require('../http/request'); -/* eslint-disable no-proto, no-shadow */ +/* eslint-disable no-shadow */ module.exports = function initialize(passport) { return function initialize(req, res, next) { req._passport = {}; diff --git a/lib/sessionmanager.js b/lib/sessionmanager.js index e7cbea66..2b560e5a 100644 --- a/lib/sessionmanager.js +++ b/lib/sessionmanager.js @@ -1,42 +1,42 @@ -/* eslint-disable camelcase, no-proto, no-shadow */ +class SessionManager { + constructor(options, serializeUser) { + if (typeof options === 'function') { + serializeUser = options; + options = undefined; + } + options = options || {}; -function SessionManager(options, serializeUser) { - if (typeof options === 'function') { - serializeUser = options; - options = undefined; + this._key = options.key || 'passport'; + this._serializeUser = serializeUser; } - options = options || {}; - this._key = options.key || 'passport'; - this._serializeUser = serializeUser; -} + logIn(req, user, cb) { + const self = this; + // eslint-disable-next-line consistent-return + this._serializeUser(user, req, (err, obj) => { + if (err) { + return cb(err); + } + if (!req._passport.session) { + req._passport.session = {}; + } + req._passport.session.user = obj; + if (!req.session) { + req.session = {}; + } + req.session[self._key] = req._passport.session; + cb(); + }); + } -SessionManager.prototype.logIn = function logIn(req, user, cb) { - const self = this; - // eslint-disable-next-line consistent-return - this._serializeUser(user, req, (err, obj) => { - if (err) { - return cb(err); - } - if (!req._passport.session) { - req._passport.session = {}; + // eslint-disable-next-line class-methods-use-this + logOut(req, cb) { + if (req._passport && req._passport.session) { + delete req._passport.session.user; } - req._passport.session.user = obj; - if (!req.session) { - req.session = {}; - } - req.session[self._key] = req._passport.session; - cb(); - }); -}; - -SessionManager.prototype.logOut = function logOut(req, cb) { - if (req._passport && req._passport.session) { - delete req._passport.session.user; + // eslint-disable-next-line no-unused-expressions + cb && cb(); } - // eslint-disable-next-line no-unused-expressions - cb && cb(); -}; - +} module.exports = SessionManager; diff --git a/lib/strategies/session.js b/lib/strategies/session.js index 9d16b472..bf5922b7 100644 --- a/lib/strategies/session.js +++ b/lib/strategies/session.js @@ -2,77 +2,70 @@ * Module dependencies. */ -/* eslint-disable camelcase, no-proto, no-shadow */ - -const util = require('util'); const Strategy = require('@passport-next/passport-strategy'); - /** * `SessionStrategy` constructor. * * @api public */ -function SessionStrategy(options, deserializeUser) { - if (typeof options === 'function') { - deserializeUser = options; - options = undefined; +class SessionStrategy extends Strategy { + constructor(options, deserializeUser) { + if (typeof options === 'function') { + deserializeUser = options; + options = undefined; + } + options = options || {}; + + super(); + this.name = 'session'; + this._deserializeUser = deserializeUser; } - options = options || {}; - Strategy.call(this); - this.name = 'session'; - this._deserializeUser = deserializeUser; -} + /** + * 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 + */ -/** - * Inherit from `Strategy`. - */ -util.inherits(SessionStrategy, Strategy); + // eslint-disable-next-line consistent-return, no-unused-vars + authenticate(req, options) { + if (!req._passport) { return this.error(new Error('passport.initialize() middleware not in use')); } + options = options || {}; -/** - * 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 - */ - -// eslint-disable-next-line consistent-return, no-unused-vars -SessionStrategy.prototype.authenticate = function authenticate(req, options) { - if (!req._passport) { return this.error(new Error('passport.initialize() middleware not in use')); } - options = options || {}; + const self = this; + let su; - const self = this; - let su; + if (req._passport.session) { + su = req._passport.session.user; + } - if (req._passport.session) { - su = req._passport.session.user; - } - - if (su || su === 0) { - // eslint-disable-next-line consistent-return - this._deserializeUser(su, req, (err, user) => { - if (err) { return self.error(err); } - if (!user) { - delete req._passport.session.user; - } else { - // TODO: Remove instance access - const property = req._passport.instance._userProperty || 'user'; - req[property] = user; - } + if (su || su === 0) { + // eslint-disable-next-line consistent-return + this._deserializeUser(su, req, (err, user) => { + if (err) { return self.error(err); } + if (!user) { + delete req._passport.session.user; + } else { + // TODO: Remove instance access + const property = req._passport.instance._userProperty || 'user'; + req[property] = user; + } + self.pass(); + }); + } else { self.pass(); - }); - } else { - self.pass(); + } } -}; +} /** diff --git a/package.json b/package.json index 18c7da2a..9ed827f8 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "name": "Rowan Wookey", "email": "admin@rwky.net" }, + "contributors": [ + "Brett Zamir" + ], "repository": { "type": "git", "url": "git://github.com/passport-next/passport.git" @@ -20,6 +23,7 @@ "bugs": { "url": "http://github.com/passport-next/passport/issues" }, + "homepage": "https://github.com/passport-next/passport", "license": "MIT", "licenses": [ { diff --git a/test/authenticator.framework.test.js b/test/authenticator.framework.test.js index afb05aaa..fde410a5 100644 --- a/test/authenticator.framework.test.js +++ b/test/authenticator.framework.test.js @@ -1,6 +1,4 @@ -/* global describe, it, expect */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const Authenticator = require('../lib/authenticator'); diff --git a/test/authenticator.middleware.test.js b/test/authenticator.middleware.test.js index ef23b9e2..c741c99a 100644 --- a/test/authenticator.middleware.test.js +++ b/test/authenticator.middleware.test.js @@ -1,6 +1,4 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ -/* eslint-disable no-underscore-dangle, camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const Authenticator = require('../lib/authenticator'); @@ -110,12 +108,12 @@ describe('Authenticator', () => { }); describe('handling a request', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Authenticator(); passport.use('success', new Strategy()); @@ -166,12 +164,12 @@ describe('Authenticator', () => { }); describe('handling a request', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Authenticator(); passport.use('success', new Strategy()); diff --git a/test/authenticator.test.js b/test/authenticator.test.js index adb2e9a7..d813651c 100644 --- a/test/authenticator.test.js +++ b/test/authenticator.test.js @@ -1,8 +1,3 @@ -/* global describe, it, expect, before */ -/* jshint expr: true, sub: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const Authenticator = require('../lib/authenticator'); @@ -19,10 +14,14 @@ describe('Authenticator', () => { describe('#use', () => { describe('with instance name', () => { - function Strategy() { - this.name = 'default'; + class Strategy { + constructor() { + this.name = 'default'; + } + + // eslint-disable-next-line class-methods-use-this + authenticate() {} } - Strategy.prototype.authenticate = function authenticate() {}; const authenticator = new Authenticator(); authenticator.use(new Strategy()); @@ -46,10 +45,14 @@ describe('Authenticator', () => { }); describe('with registered name overridding instance name', () => { - function Strategy() { - this.name = 'default'; + class Strategy { + constructor() { + this.name = 'default'; + } + + // eslint-disable-next-line class-methods-use-this + authenticate() {} } - Strategy.prototype.authenticate = function authenticate() {}; const authenticator = new Authenticator(); authenticator.use('bar', new Strategy()); diff --git a/test/http/request.test.js b/test/http/request.test.js index ee6e33af..c8bf0d2e 100644 --- a/test/http/request.test.js +++ b/test/http/request.test.js @@ -1,10 +1,5 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const http = require('http'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); const initialize = require('../../lib/middleware/initialize'); function setup() { diff --git a/test/middleware/authenticate.error.callback.test.js b/test/middleware/authenticate.error.callback.test.js index 3295e554..7ccbb0a2 100644 --- a/test/middleware/authenticate.error.callback.test.js +++ b/test/middleware/authenticate.error.callback.test.js @@ -1,20 +1,14 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - - const chai = require('chai'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); const authenticate = require('../../lib/middleware/authenticate'); describe('middleware/authenticate', () => { describe('error with callback', () => { - function Strategy() { + class Strategy { + authenticate() { + this.error(new Error('something is wrong')); + } } - Strategy.prototype.authenticate = function authenticate() { - this.error(new Error('something is wrong')); - }; const passport = new Passport(); passport.use('error', new Strategy()); @@ -52,11 +46,11 @@ describe('middleware/authenticate', () => { }); describe('error with callback and options passed to middleware', () => { - function Strategy() { + class Strategy { + authenticate() { + this.error(new Error('something is wrong')); + } } - Strategy.prototype.authenticate = function authenticate() { - this.error(new Error('something is wrong')); - }; const passport = new Passport(); passport.use('error', new Strategy()); diff --git a/test/middleware/authenticate.error.test.js b/test/middleware/authenticate.error.test.js index c201d660..cf7dc644 100644 --- a/test/middleware/authenticate.error.test.js +++ b/test/middleware/authenticate.error.test.js @@ -1,20 +1,15 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('error', () => { - function Strategy() { + class Strategy { + authenticate() { + this.error(new Error('something is wrong')); + } } - Strategy.prototype.authenticate = function authenticate() { - this.error(new Error('something is wrong')); - }; const passport = new Passport(); passport.use('error', new Strategy()); diff --git a/test/middleware/authenticate.fail.callback.test.js b/test/middleware/authenticate.fail.callback.test.js index 73bba9d4..ff2f2a49 100644 --- a/test/middleware/authenticate.fail.callback.test.js +++ b/test/middleware/authenticate.fail.callback.test.js @@ -1,19 +1,14 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('fail with callback', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -52,11 +47,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback, passing info', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -109,11 +104,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback, passing info and status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }, 403); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }, 403); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -165,11 +160,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback, passing challenge', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Bearer challenge'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Bearer challenge'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -221,11 +216,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback, passing challenge and status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Bearer challenge', 403); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Bearer challenge', 403); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -276,11 +271,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback, passing status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(402); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(402); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -332,11 +327,11 @@ describe('middleware/authenticate', () => { }); describe('fail with callback and options passed to middleware', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); diff --git a/test/middleware/authenticate.fail.flash.test.js b/test/middleware/authenticate.fail.flash.test.js index 8d5398e3..d47c2fee 100644 --- a/test/middleware/authenticate.fail.flash.test.js +++ b/test/middleware/authenticate.fail.flash.test.js @@ -1,21 +1,16 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('using strategy that specifies message', () => { describe('fail with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -60,11 +55,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -109,11 +104,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -158,11 +153,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -207,11 +202,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -259,11 +254,11 @@ describe('middleware/authenticate', () => { describe('using strategy that specifies message and type', () => { describe('fail with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ type: 'notice', message: 'Invite required' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ type: 'notice', message: 'Invite required' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -308,11 +303,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ type: 'notice', message: 'Invite required' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ type: 'notice', message: 'Invite required' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -357,11 +352,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ type: 'notice', message: 'Invite required' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ type: 'notice', message: 'Invite required' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -406,11 +401,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ type: 'notice', message: 'Invite required' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ type: 'notice', message: 'Invite required' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -455,11 +450,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ type: 'notice', message: 'Invite required' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ type: 'notice', message: 'Invite required' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -507,11 +502,11 @@ describe('middleware/authenticate', () => { describe('using strategy that specifies message as string', () => { describe('fail with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Access denied'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Access denied'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -556,11 +551,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Access denied'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Access denied'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -605,11 +600,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Access denied'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Access denied'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -654,11 +649,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Access denied'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Access denied'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -703,11 +698,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Access denied'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Access denied'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -755,11 +750,11 @@ describe('middleware/authenticate', () => { describe('using strategy that does not specify message', () => { describe('fail with flash message left up to strategy', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -804,11 +799,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message left up to strategy using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -853,11 +848,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message specified by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -902,11 +897,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message specified by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -951,11 +946,11 @@ describe('middleware/authenticate', () => { }); describe('fail with flash message specified by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); diff --git a/test/middleware/authenticate.fail.message.test.js b/test/middleware/authenticate.fail.message.test.js index 17c74831..07230746 100644 --- a/test/middleware/authenticate.fail.message.test.js +++ b/test/middleware/authenticate.fail.message.test.js @@ -1,20 +1,15 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('fail with message set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -55,11 +50,11 @@ describe('middleware/authenticate', () => { }); describe('fail with message set by route that is added to messages', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -102,11 +97,11 @@ describe('middleware/authenticate', () => { }); describe('fail with message set by strategy', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -147,11 +142,11 @@ describe('middleware/authenticate', () => { }); describe('fail with message set by strategy with extra info', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid password', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid password', scope: 'read' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); diff --git a/test/middleware/authenticate.fail.multi.test.js b/test/middleware/authenticate.fail.multi.test.js index 60deb858..63d81223 100644 --- a/test/middleware/authenticate.fail.multi.test.js +++ b/test/middleware/authenticate.fail.multi.test.js @@ -1,32 +1,27 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('with multiple strategies, all of which fail, and responding with unauthorized status', () => { - function BasicStrategy() { + class BasicStrategy { + authenticate() { + this.fail('BASIC challenge'); + } } - BasicStrategy.prototype.authenticate = function authenticate() { - this.fail('BASIC challenge'); - }; - function DigestStrategy() { + class DigestStrategy { + authenticate() { + this.fail('DIGEST challenge'); + } } - DigestStrategy.prototype.authenticate = function authenticate() { - this.fail('DIGEST challenge'); - }; - function NoChallengeStrategy() { + class NoChallengeStrategy { + authenticate() { + this.fail(); + } } - NoChallengeStrategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('basic', new BasicStrategy()); @@ -73,23 +68,23 @@ describe('middleware/authenticate', () => { }); describe('with multiple strategies, all of which fail, and responding with specified status', () => { - function BasicStrategy() { + class BasicStrategy { + authenticate() { + this.fail('BASIC challenge', 400); + } } - BasicStrategy.prototype.authenticate = function authenticate() { - this.fail('BASIC challenge', 400); - }; - function BearerStrategy() { + class BearerStrategy { + authenticate() { + this.fail('BEARER challenge', 403); + } } - BearerStrategy.prototype.authenticate = function authenticate() { - this.fail('BEARER challenge', 403); - }; - function NoChallengeStrategy() { + class NoChallengeStrategy { + authenticate() { + this.fail(402); + } } - NoChallengeStrategy.prototype.authenticate = function authenticate() { - this.fail(402); - }; const passport = new Passport(); passport.use('basic', new BasicStrategy()); @@ -129,17 +124,17 @@ describe('middleware/authenticate', () => { }); describe('with multiple strategies, all of which fail, and flashing message', () => { - function StrategyA() { + class StrategyA { + authenticate() { + this.fail('A message'); + } } - StrategyA.prototype.authenticate = function authenticate() { - this.fail('A message'); - }; - function StrategyB() { + class StrategyB { + authenticate() { + this.fail('B message'); + } } - StrategyB.prototype.authenticate = function authenticate() { - this.fail('B message'); - }; const passport = new Passport(); passport.use('a', new StrategyA()); @@ -184,23 +179,23 @@ describe('middleware/authenticate', () => { }); describe('with multiple strategies, all of which fail with unauthorized status, and invoking callback', () => { - function BasicStrategy() { + class BasicStrategy { + authenticate() { + this.fail('BASIC challenge'); + } } - BasicStrategy.prototype.authenticate = function authenticate() { - this.fail('BASIC challenge'); - }; - function DigestStrategy() { + class DigestStrategy { + authenticate() { + this.fail('DIGEST challenge'); + } } - DigestStrategy.prototype.authenticate = function authenticate() { - this.fail('DIGEST challenge'); - }; - function NoChallengeStrategy() { + class NoChallengeStrategy { + authenticate() { + this.fail(); + } } - NoChallengeStrategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('basic', new BasicStrategy()); @@ -265,23 +260,23 @@ describe('middleware/authenticate', () => { }); describe('with multiple strategies, all of which fail with specific status, and invoking callback', () => { - function BasicStrategy() { + class BasicStrategy { + authenticate() { + this.fail('BASIC challenge', 400); + } } - BasicStrategy.prototype.authenticate = function authenticate() { - this.fail('BASIC challenge', 400); - }; - function BearerStrategy() { + class BearerStrategy { + authenticate() { + this.fail('BEARER challenge', 403); + } } - BearerStrategy.prototype.authenticate = function authenticate() { - this.fail('BEARER challenge', 403); - }; - function NoChallengeStrategy() { + class NoChallengeStrategy { + authenticate() { + this.fail(402); + } } - NoChallengeStrategy.prototype.authenticate = function authenticate() { - this.fail(402); - }; const passport = new Passport(); passport.use('basic', new BasicStrategy()); @@ -343,11 +338,11 @@ describe('middleware/authenticate', () => { }); describe('with single strategy in list, which fails with unauthorized status, and invoking callback', () => { - function BasicStrategy() { + class BasicStrategy { + authenticate() { + this.fail('BASIC challenge'); + } } - BasicStrategy.prototype.authenticate = function authenticate() { - this.fail('BASIC challenge'); - }; const passport = new Passport(); passport.use('basic', new BasicStrategy()); diff --git a/test/middleware/authenticate.fail.test.js b/test/middleware/authenticate.fail.test.js index ac45b55b..f467e436 100644 --- a/test/middleware/authenticate.fail.test.js +++ b/test/middleware/authenticate.fail.test.js @@ -1,20 +1,15 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -48,11 +43,11 @@ describe('middleware/authenticate', () => { }); describe('fail with redirect', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -84,11 +79,11 @@ describe('middleware/authenticate', () => { }); describe('fail with challenge', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('MOCK challenge'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('MOCK challenge'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -128,11 +123,11 @@ describe('middleware/authenticate', () => { }); describe('fail with challenge and status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('MOCK challenge', 403); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('MOCK challenge', 403); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -166,11 +161,11 @@ describe('middleware/authenticate', () => { }); describe('fail with status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(400); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(400); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -204,11 +199,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -254,11 +249,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error, passing info to fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Invalid credentials' }); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Invalid credentials' }); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -304,11 +299,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error, passing info and status to fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail({ message: 'Multiple credentials' }, 400); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail({ message: 'Multiple credentials' }, 400); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -354,11 +349,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error, passing challenge to fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Bearer challenge'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Bearer challenge'); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -410,11 +405,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error, passing challenge and status to fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail('Bearer challenge', 403); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail('Bearer challenge', 403); - }; const passport = new Passport(); passport.use('fail', new Strategy()); @@ -460,11 +455,11 @@ describe('middleware/authenticate', () => { }); describe('fail with error, passing status to fail', () => { - function Strategy() { + class Strategy { + authenticate() { + this.fail(402); + } } - Strategy.prototype.authenticate = function authenticate() { - this.fail(402); - }; const passport = new Passport(); passport.use('fail', new Strategy()); diff --git a/test/middleware/authenticate.pass.test.js b/test/middleware/authenticate.pass.test.js index 49a35bf2..54cab400 100644 --- a/test/middleware/authenticate.pass.test.js +++ b/test/middleware/authenticate.pass.test.js @@ -1,20 +1,15 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('pass', () => { - function Strategy() { + class Strategy { + authenticate() { + this.pass(); + } } - Strategy.prototype.authenticate = function authenticate() { - this.pass(); - }; const passport = new Passport(); passport.use('pass', new Strategy()); diff --git a/test/middleware/authenticate.redirect.test.js b/test/middleware/authenticate.redirect.test.js index b7195f2a..5da2e079 100644 --- a/test/middleware/authenticate.redirect.test.js +++ b/test/middleware/authenticate.redirect.test.js @@ -1,20 +1,17 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('redirect', () => { - function Strategy() { + class Strategy { + authenticate() { + this.redirect('http://www.example.com/idp'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.redirect('http://www.example.com/idp'); - }; const passport = new Passport(); passport.use('redirect', new Strategy()); @@ -47,12 +44,12 @@ describe('middleware/authenticate', () => { }); describe('redirect with session', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'idurotola' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'idurotola' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -97,11 +94,11 @@ describe('middleware/authenticate', () => { }); describe('redirect with status', () => { - function Strategy() { + class Strategy { + authenticate() { + this.redirect('http://www.example.com/idp', 303); + } } - Strategy.prototype.authenticate = function authenticate() { - this.redirect('http://www.example.com/idp', 303); - }; const passport = new Passport(); passport.use('redirect', new Strategy()); @@ -134,11 +131,11 @@ describe('middleware/authenticate', () => { }); describe('redirect using framework function', () => { - function Strategy() { + class Strategy { + authenticate() { + this.redirect('http://www.example.com/idp'); + } } - Strategy.prototype.authenticate = function authenticate() { - this.redirect('http://www.example.com/idp'); - }; const passport = new Passport(); passport.use('redirect', new Strategy()); @@ -170,11 +167,11 @@ describe('middleware/authenticate', () => { }); describe('redirect with status using framework function', () => { - function Strategy() { + class Strategy { + authenticate() { + this.redirect('http://www.example.com/idp', 303); + } } - Strategy.prototype.authenticate = function authenticate() { - this.redirect('http://www.example.com/idp', 303); - }; const passport = new Passport(); passport.use('redirect', new Strategy()); diff --git a/test/middleware/authenticate.success.callback.test.js b/test/middleware/authenticate.success.callback.test.js index 97cd161f..08373eb7 100644 --- a/test/middleware/authenticate.success.callback.test.js +++ b/test/middleware/authenticate.success.callback.test.js @@ -1,21 +1,16 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('success with callback', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -68,13 +63,12 @@ describe('middleware/authenticate', () => { }); describe('success with callback and options passed to middleware', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Hello' }); + } } - // eslint-disable-next-line consistent-return - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); diff --git a/test/middleware/authenticate.success.flash.test.js b/test/middleware/authenticate.success.flash.test.js index 6adab984..165d5f56 100644 --- a/test/middleware/authenticate.success.flash.test.js +++ b/test/middleware/authenticate.success.flash.test.js @@ -1,22 +1,19 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('using strategy that specifies message', () => { describe('success with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -66,12 +63,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -121,12 +118,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -176,12 +173,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -231,12 +228,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -289,12 +286,12 @@ describe('middleware/authenticate', () => { describe('using strategy that specifies message and type', () => { describe('success with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { type: 'info', message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { type: 'info', message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -344,12 +341,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { type: 'info', message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { type: 'info', message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -399,12 +396,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { type: 'info', message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { type: 'info', message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -454,12 +451,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { type: 'info', message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { type: 'info', message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -509,12 +506,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { type: 'info', message: 'Hello' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { type: 'info', message: 'Hello' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -567,12 +564,12 @@ describe('middleware/authenticate', () => { describe('using strategy that specifies message as string', () => { describe('success with flash message', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, 'Greetings'); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, 'Greetings'); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -622,12 +619,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, 'Greetings'); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, 'Greetings'); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -677,12 +674,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, 'Greetings'); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, 'Greetings'); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -732,12 +729,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, 'Greetings'); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, 'Greetings'); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -787,12 +784,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message overridden by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, 'Greetings'); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, 'Greetings'); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -845,12 +842,12 @@ describe('middleware/authenticate', () => { describe('using strategy that does not specify message', () => { describe('success with flash message left up to strategy', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -900,12 +897,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message left up to strategy using type set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -955,12 +952,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message specified by route as string', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -1010,12 +1007,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message specified by route using options', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -1065,12 +1062,12 @@ describe('middleware/authenticate', () => { }); describe('success with flash message specified by route using options with type', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); diff --git a/test/middleware/authenticate.success.info.test.js b/test/middleware/authenticate.success.info.test.js index bf8d8fb7..fa5143aa 100644 --- a/test/middleware/authenticate.success.info.test.js +++ b/test/middleware/authenticate.success.info.test.js @@ -1,21 +1,18 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('success with info', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { clientId: '123', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { clientId: '123', scope: 'read' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -60,12 +57,12 @@ describe('middleware/authenticate', () => { }); describe('success with info that is transformed', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { clientId: '123', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { clientId: '123', scope: 'read' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -114,12 +111,12 @@ describe('middleware/authenticate', () => { }); describe('success with info, but transform that encounters an error', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { clientId: '123', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { clientId: '123', scope: 'read' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -165,12 +162,12 @@ describe('middleware/authenticate', () => { }); describe('success with info, but option that disables info', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { clientId: '123', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { clientId: '123', scope: 'read' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); diff --git a/test/middleware/authenticate.success.message.test.js b/test/middleware/authenticate.success.message.test.js index d9d84258..420117d1 100644 --- a/test/middleware/authenticate.success.message.test.js +++ b/test/middleware/authenticate.success.message.test.js @@ -1,20 +1,18 @@ -/* global describe, it, expect, before */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('success with message set by route', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -61,12 +59,12 @@ describe('middleware/authenticate', () => { }); describe('success with message set by route that is added to messages', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -115,12 +113,12 @@ describe('middleware/authenticate', () => { }); describe('success with message set by strategy', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -167,12 +165,12 @@ describe('middleware/authenticate', () => { }); describe('success with message set by strategy with extra info', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user, { message: 'Welcome!', scope: 'read' }); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user, { message: 'Welcome!', scope: 'read' }); - }; const passport = new Passport(); passport.use('success', new Strategy()); diff --git a/test/middleware/authenticate.success.multi.test.js b/test/middleware/authenticate.success.multi.test.js index 5811f602..03215550 100644 --- a/test/middleware/authenticate.success.multi.test.js +++ b/test/middleware/authenticate.success.multi.test.js @@ -1,26 +1,23 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('with multiple strategies, the first of which succeeds', () => { - function StrategyA() { + class StrategyA { + authenticate() { + this.success({ username: 'bob-a' }); + } } - StrategyA.prototype.authenticate = function authenticate() { - this.success({ username: 'bob-a' }); - }; - function StrategyB() { + class StrategyB { + authenticate() { + this.success({ username: 'bob-b' }); + } } - StrategyB.prototype.authenticate = function authenticate() { - this.success({ username: 'bob-b' }); - }; const passport = new Passport(); passport.use('a', new StrategyA()); @@ -58,17 +55,17 @@ describe('middleware/authenticate', () => { }); describe('with multiple strategies, the second of which succeeds', () => { - function StrategyA() { + class StrategyA { + authenticate() { + this.fail('A challenge'); + } } - StrategyA.prototype.authenticate = function authenticate() { - this.fail('A challenge'); - }; - function StrategyB() { + class StrategyB { + authenticate() { + this.success({ username: 'bob-b' }); + } } - StrategyB.prototype.authenticate = function authenticate() { - this.success({ username: 'bob-b' }); - }; const passport = new Passport(); passport.use('a', new StrategyA()); diff --git a/test/middleware/authenticate.success.test.js b/test/middleware/authenticate.success.test.js index d86a66df..d8d367da 100644 --- a/test/middleware/authenticate.success.test.js +++ b/test/middleware/authenticate.success.test.js @@ -1,22 +1,18 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { describe('success', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -59,12 +55,12 @@ describe('middleware/authenticate', () => { }); describe('success that assigns a specific property', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -112,15 +108,15 @@ describe('middleware/authenticate', () => { }); describe('success with strategy-specific options', () => { - function Strategy() { - } - Strategy.prototype.authenticate = function authenticate(req, options) { - const user = { id: '1', username: 'jaredhanson' }; - if (options.scope === 'email') { - user.email = 'jaredhanson@example.com'; + class Strategy { + authenticate(req, options) { + const user = { id: '1', username: 'jaredhanson' }; + if (options.scope === 'email') { + user.email = 'jaredhanson@example.com'; + } + this.success(user); } - this.success(user); - }; + } const passport = new Passport(); passport.use('success', new Strategy()); @@ -166,12 +162,12 @@ describe('middleware/authenticate', () => { }); describe('success with redirect', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -214,12 +210,12 @@ describe('middleware/authenticate', () => { }); describe('success with return to previous location', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -268,12 +264,12 @@ describe('middleware/authenticate', () => { }); describe('success with return to default location', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); @@ -316,12 +312,12 @@ describe('middleware/authenticate', () => { }); describe('success, but login that encounters an error', () => { - function Strategy() { + class Strategy { + authenticate() { + const user = { id: '1', username: 'jaredhanson' }; + this.success(user); + } } - Strategy.prototype.authenticate = function authenticate() { - const user = { id: '1', username: 'jaredhanson' }; - this.success(user); - }; const passport = new Passport(); passport.use('success', new Strategy()); diff --git a/test/middleware/authenticate.test.js b/test/middleware/authenticate.test.js index 8b393adf..7e0d6740 100644 --- a/test/middleware/authenticate.test.js +++ b/test/middleware/authenticate.test.js @@ -1,11 +1,8 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const authenticate = require('../../lib/middleware/authenticate'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); describe('middleware/authenticate', () => { diff --git a/test/middleware/initialize.test.js b/test/middleware/initialize.test.js index a2eca710..4bba1377 100644 --- a/test/middleware/initialize.test.js +++ b/test/middleware/initialize.test.js @@ -1,10 +1,5 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ - const chai = require('chai'); -const Passport = require('../..').Passport; +const { Passport } = require('../..'); const initialize = require('../../lib/middleware/initialize'); describe('middleware/initialize', () => { diff --git a/test/package.test.js b/test/package.test.js index ea7dfd5e..2eb8bf81 100644 --- a/test/package.test.js +++ b/test/package.test.js @@ -1,5 +1,3 @@ -/* global describe, it, expect */ - const passport = require('..'); describe('passport', () => { diff --git a/test/strategies/session.test.js b/test/strategies/session.test.js index 2a210972..ec2eabf3 100644 --- a/test/strategies/session.test.js +++ b/test/strategies/session.test.js @@ -1,7 +1,4 @@ -/* global describe, it, expect, before */ -/* jshint expr: true */ - -/* eslint-disable camelcase, no-proto, no-shadow */ +/* eslint-disable no-shadow */ const chai = require('chai'); const SessionStrategy = require('../../lib/strategies/session');