Skip to content

Commit

Permalink
Introduced 'AuthContext' to simplify/future-proof external interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranj committed Aug 21, 2011
1 parent 9b306d4 commit d6273ac
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/authExecutionScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright(c) 2010 Ciaran Jessup <ciaranj@gmai.com>
* MIT Licensed
*/
var AuthExecutionScope= module.exports = function( scope, trace ) {
var AuthExecutionScope= module.exports = function( authContext ) {
this.executionResult= { authenticated: undefined };
this.scope= scope;
this._trace= trace;
this.authContext= authContext;
this._trace= authContext.request.getAuthDetails().trace;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/auth_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = function(optionsOrStrategy) {

// Assign a tracer so if needed routes can trace.
req.getAuthDetails().trace= function( message, scope, linePrefix ) {
traceFunction( message, req, res, scope, linePrefix );
traceFunction( message, {scope:scope, request:req, response:res}, linePrefix );
};

// Now we've added our requisite methods to the request, call the next part of the middleware chain
Expand Down
2 changes: 1 addition & 1 deletion lib/requestMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports.authenticate= function(strategy, opts, callback, strategyExecutor
callback(null, true);
}
else {
strategyExecutor.authenticate(strategy, scope, trace, req, res, function (error, executionResult) {
strategyExecutor.authenticate(strategy, {scope:scope, request:req, response:res}, function (error, executionResult) {
//TODO: This needs tidying up, the HTTP strategies have bled...
if( executionResult) {
req.getAuthDetails().errorResponse= executionResult.errorResponse;
Expand Down
16 changes: 8 additions & 8 deletions lib/strategyExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports= function(strategies) {
}
};

module.exports.prototype.authenticate= function(strategies, scope, trace, request, response, callback) {
var executionScope= new AuthExecutionScope( scope, trace );
module.exports.prototype.authenticate= function(strategies, authContext, callback) {
var executionScope= new AuthExecutionScope( authContext );
if( !this.strategies || this.strategies.length ==0 ) {
trace( "Unable to find a strategy to authenticate with", scope, "###");
executionScope.trace( "Unable to find a strategy to authenticate with", "###");
callback( null, executionScope.executionResult );
}
else {
Expand All @@ -32,12 +32,12 @@ module.exports.prototype.authenticate= function(strategies, scope, trace, reques
carryOn= true;
}
catch(e) {
trace( "Error choosing strategy: "+ e, scope, "###");
executionScope.trace( "Error choosing strategy: "+ e, "###");
callback(e);
}
if( carryOn ) {
if( strategiesToTest.length == 0 ) {
trace( "Tested all strategies :" + util.inspect(executionScope.executionResult), scope, "###");
executionScope.trace( "Tested all strategies :" + util.inspect(executionScope.executionResult), "###");
callback( null, executionScope.executionResult );
}
else {
Expand All @@ -49,15 +49,15 @@ module.exports.prototype.authenticate= function(strategies, scope, trace, reques
//todo: scope!
;(function next(e) {
if (executionScope.executionResult.halted || e || complete === total) {
trace( "Tested all strategies", scope, "###");
executionScope.trace( "Tested all strategies", "###");
callback(e, executionScope.executionResult)
}
else {
strategy= strategiesToTest[complete++];
if( strategy.isValid === undefined || strategy.isValid() ) {
trace( "Attempting authentication with: " + strategy.name, scope, "###" );
executionScope.executionResult.currentStrategy= strategy.name;
strategy.authenticate.call(executionScope, request, response, next);
executionScope.trace( "Attempting authentication with: " + strategy.name, "###" );
strategy.authenticate.call(executionScope, authContext.request, authContext.response, next);
}
else {
next();
Expand Down
12 changes: 6 additions & 6 deletions lib/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ module.exports.nullTrace= function() {}
* Viewing these lines offer useful diagnostics to determine why authentication is failing (the session id within the square brackets
* and the provided url giving the greatest clues!)
*/
module.exports.standardTrace= function( message, req, res, scope, linePrefix ) {
module.exports.standardTrace= function( message, authContext, linePrefix ) {
var d= new Date();
var id;
if( req.sessionID ) {
id= req.sessionID.substring(0,6);
if( authContext.request.sessionID ) {
id= authContext.request.sessionID.substring(0,6);
} else {
id= req.socket.remoteAddress;
id= authContext.request.socket.remoteAddress;
}
scope= (scope? " (" + scope +")" : "");
linePrefix= (linePrefix? " " + linePrefix : "");
var scope= (authContext.scope? " (" + authContext.scope +")" : "");
var linePrefix= (linePrefix? " " + linePrefix : "");
message= message.replace(/\n/g, "\n " + linePrefix);
console.log( pad(d.getHours(),2) + ":"+ pad(d.getMinutes(),2) + ':' + pad(d.getSeconds(),2) + '-' + pad(d.getMilliseconds(),3) + " ["+id+"]" + scope + linePrefix+ " "+ message);
}

0 comments on commit d6273ac

Please sign in to comment.