Socket
Socket
Sign inDemoInstall

passport

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

passport - npm Package Compare versions

Comparing version 0.1.11 to 0.1.12

207

lib/passport/middleware/authenticate.js

@@ -64,67 +64,38 @@ /**

// TODO: Implement support for authenticting with multiple strategies.
// Cast `name` to an array, allowing authentication to pass through a chain of
// strategies. The first strategy to succeed, redirect, or error will halt
// the chain. Authentication failures will proceed through each strategy in
// series, ultimately failing if all strategies fail.
//
// This is typically used on API endpoints to allow clients to authenticate
// using their preferred choice of Basic, Digest, token-based schemes, etc.
// It is not feasible to construct a chain of multiple strategies that involve
// redirection (for example both Facebook and Twitter), since the first one to
// redirect will halt the chain.
if (!Array.isArray(name)) {
name = [ name ];
}
return function authenticate(req, res, next) {
var passport = this;
var delegate = {};
delegate.success = function(user, info) {
// accumulator for failures from each strategy in the chain
var failures = [];
function allFailed() {
if (callback) {
return callback(null, user, info);
}
info = info || {}
if (options.successFlash) {
var flash = options.successFlash;
if (typeof flash == 'string') {
flash = { type: 'success', message: flash };
}
flash.type = flash.type || 'success';
var type = flash.type || info.type || 'success';
var msg = flash.message || info.message || info;
if (typeof msg == 'string') {
req.flash(type, msg);
}
}
if (options.assignProperty) {
req[options.assignProperty] = user;
return next();
}
req.logIn(user, options, function(err) {
if (err) { return next(err); }
if (options.authInfo || options.authInfo === undefined) {
passport.transformAuthInfo(info, function(err, tinfo) {
if (err) { return next(err); }
req.authInfo = tinfo;
complete();
});
if (failures.length == 1) {
return callback(null, false, failures[0].challenge, failures[0].status);
} else {
complete();
var challenges = failures.map(function(f) { return f.challenge; });
var statuses = failures.map(function(f) { return f.status; })
return callback(null, false, challenges, statuses);
}
function complete() {
if (options.successReturnToOrRedirect) {
var url = options.successReturnToOrRedirect;
if (req.session && req.session.returnTo) {
url = req.session.returnTo;
delete req.session.returnTo;
}
return res.redirect(url);
}
if (options.successRedirect) {
return res.redirect(options.successRedirect);
}
next();
}
});
}
delegate.fail = function(challenge, status) {
if (callback) {
return callback(null, false, challenge, status);
}
challenge = challenge || {}
// Strategies are ordered by priority. For the purpose of flashing a
// message, the first failure will be displayed.
var failure = failures[0] || {}
, challenge = failure.challenge || {};
if (options.failureFlash) {

@@ -136,3 +107,3 @@ var flash = options.failureFlash;

flash.type = flash.type || 'error';
var type = flash.type || challenge.type || 'error';

@@ -147,31 +118,111 @@ var msg = flash.message || challenge.message || challenge;

}
if (typeof challenge == 'number') {
status = challenge;
challenge = null;
}
// When failure handling is not delegated to the application, the default
// is to respond with 401 Unauthorized. Note that the WWW-Authenticate
// header will be set according to the strategies in use (see
// actions#fail).
res.statusCode = status || 401;
if (typeof challenge == 'string') {
this.res.setHeader('WWW-Authenticate', challenge);
// actions#fail). If multiple strategies failed, each of their challenges
// will be included in the response.
var rchallenge = []
, rstatus;
for (var j = 0, len = failures.length; j < len; j++) {
var failure = failures[j]
, challenge = failure.challenge || {}
, status = failure.status;
if (typeof challenge == 'number') {
status = challenge;
challenge = null;
}
rstatus = rstatus || status;
if (typeof challenge == 'string') {
rchallenge.push(challenge)
}
}
res.statusCode = rstatus || 401;
if (rchallenge.length) {
res.setHeader('WWW-Authenticate', rchallenge);
}
res.end('Unauthorized');
}
// Get the strategy, which will be used as prototype from which to create
// a new instance. Action functions will then be bound to the strategy
// within the context of the HTTP request/response pair.
var prototype = passport._strategy(name);
if (!prototype) { return next(new Error('no strategy registered under name: ' + name)); }
(function attempt(i) {
var delegate = {};
delegate.success = function(user, info) {
if (callback) {
return callback(null, user, info);
}
info = info || {}
if (options.successFlash) {
var flash = options.successFlash;
if (typeof flash == 'string') {
flash = { type: 'success', message: flash };
}
flash.type = flash.type || 'success';
var type = flash.type || info.type || 'success';
var msg = flash.message || info.message || info;
if (typeof msg == 'string') {
req.flash(type, msg);
}
}
if (options.assignProperty) {
req[options.assignProperty] = user;
return next();
}
req.logIn(user, options, function(err) {
if (err) { return next(err); }
if (options.authInfo || options.authInfo === undefined) {
passport.transformAuthInfo(info, function(err, tinfo) {
if (err) { return next(err); }
req.authInfo = tinfo;
complete();
});
} else {
complete();
}
function complete() {
if (options.successReturnToOrRedirect) {
var url = options.successReturnToOrRedirect;
if (req.session && req.session.returnTo) {
url = req.session.returnTo;
delete req.session.returnTo;
}
return res.redirect(url);
}
if (options.successRedirect) {
return res.redirect(options.successRedirect);
}
next();
}
});
}
delegate.fail = function(challenge, status) {
// push this failure into the accumulator and attempt authentication
// using the next strategy
failures.push({ challenge: challenge, status: status });
attempt(i + 1);
}
var strategy = Object.create(prototype);
var context = new Context(delegate, req, res, next);
augment(strategy, actions, context);
var layer = name[i];
// If no more strategies exist in the chain, authentication has failed.
if (!layer) { return allFailed(); }
strategy.authenticate(req, options);
// Get the strategy, which will be used as prototype from which to create
// a new instance. Action functions will then be bound to the strategy
// within the context of the HTTP request/response pair.
var prototype = passport._strategy(layer);
if (!prototype) { return next(new Error('no strategy registered under name: ' + layer)); }
var strategy = Object.create(prototype);
var context = new Context(delegate, req, res, next);
augment(strategy, actions, context);
strategy.authenticate(req, options);
})(0); // attempt
}

@@ -178,0 +229,0 @@ }

{
"name": "passport",
"version": "0.1.11",
"version": "0.1.12",
"description": "Simple, unobtrusive authentication for Node.js.",

@@ -5,0 +5,0 @@ "author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc