Skip to content

Commit

Permalink
Remove extraneous context and delegate objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jan 11, 2014
1 parent 24de619 commit dfabb21
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 384 deletions.
93 changes: 0 additions & 93 deletions lib/passport/context/http/actions.js

This file was deleted.

17 changes: 0 additions & 17 deletions lib/passport/context/http/context.js

This file was deleted.

129 changes: 101 additions & 28 deletions lib/passport/middleware/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* Module dependencies.
*/
var http = require('http')
, util = require('util')
, actions = require('../context/http/actions')
, Context = require('../context/http/context')
var http = require('http');


/**
Expand Down Expand Up @@ -159,8 +156,41 @@ module.exports = function authenticate(name, options, callback) {
}

(function attempt(i) {
var delegate = {};
delegate.success = function(user, info) {
var layer = name[i];
// If no more strategies exist in the chain, authentication has failed.
if (!layer) { return allFailed(); }

// Get the strategy, which will be used as prototype from which to create
// a new instance. Action functions will then be bound to the strategy
// within the context of the HTTP request/response pair.
var prototype = passport._strategy(layer);
if (!prototype) { return next(new Error('no strategy registered under name: ' + layer)); }

var strategy = Object.create(prototype);


// ----- BEGIN STRATEGY AUGMENTATION -----
// Augment the new strategy instance with action functions. These action
// functions are bound via closure the the request/response pair. The end
// goal of the strategy is to invoke *one* of these action methods, in
// order to indicate successful or failed authentication, redirect to a
// third-party identity provider, etc.

/**
* Authenticate `user`, with optional `info`.
*
* Strategies should call this function to successfully authenticate a
* user. `user` should be an object supplied by the application after it
* has been given an opportunity to verify credentials. `info` is an
* optional argument containing additional user information. This is
* useful for third-party authentication strategies to pass profile
* details.
*
* @param {Object} user
* @param {Object} info
* @api public
*/
strategy.success = function(user, info) {
if (callback) {
return callback(null, user, info);
}
Expand Down Expand Up @@ -223,35 +253,78 @@ module.exports = function authenticate(name, options, callback) {
}
});
}
delegate.fail = function(challenge, status) {

/**
* Fail authentication, with optional `challenge` and `status`, defaulting
* to 401.
*
* Strategies should call this function to fail an authentication attempt.
*
* @param {String} challenge
* @param {Number} status
* @api public
*/
strategy.fail = function(challenge, status) {
// push this failure into the accumulator and attempt authentication
// using the next strategy
failures.push({ challenge: challenge, status: status });
attempt(i + 1);
}

var layer = name[i];
// If no more strategies exist in the chain, authentication has failed.
if (!layer) { return allFailed(); }

// Get the strategy, which will be used as prototype from which to create
// a new instance. Action functions will then be bound to the strategy
// within the context of the HTTP request/response pair.
var 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);

/**
* Redirect to `url` with optional `status`, defaulting to 302.
*
* Strategies should call this function to redirect the user (via their
* user agent) to a third-party website for authentication.
*
* @param {String} url
* @param {Number} status
* @api public
*/
strategy.redirect = function(url, status) {
if (typeof res.redirect == 'function') {
// If possible use redirect method on the response
// Assume Express API, optional status is last
res.redirect(url, status || 302);
} else {
// Otherwise fall back to native methods
res.statusCode = status || 302;
res.setHeader('Location', url);
res.setHeader('Content-Length', '0');
res.end();
}
}

/**
* Pass without making a success or fail decision.
*
* Under most circumstances, Strategies should not need to call this
* function. It exists primarily to allow previous authentication state
* to be restored, for example from an HTTP session.
*
* @api public
*/
strategy.pass = function() {
next();
}

/**
* Internal error while performing authentication.
*
* Strategies should call this function when an internal error occurs
* during the process of performing authentication; for example, if the
* user directory is not available.
*
* @param {Error} err
* @api public
*/
strategy.error = function(err) {
next(err);
}

// ----- END STRATEGY AUGMENTATION -----

strategy.authenticate(req, options);
})(0); // attempt
}
}


function augment(strategy, actions, ctx) {
for (var method in actions) {
strategy[method] = actions[method].bind(ctx);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"node": ">= 0.4.0"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js test/**/*-test.js test/context/http/*-test.js"
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js test/**/*-test.js"
}
}
Loading

0 comments on commit dfabb21

Please sign in to comment.