<commit_before>/*!
 * Copyright(c) 2010 <NAME> <<EMAIL>>
 * MIT Licensed
 */

/** 
 * Module dependencies.
 */
var connect= require('connect');
var fs= require('fs');
var StrategyExecutor= require('./strategyExecutor');

/**
 * Construct the authentication middleware.
 * Construction can take 2 forms:
 *    auth(<Strategy>()/[<Strategy>()])  -  A single configured strategy, or array of strategies.
 *    auth({strategies:<Strategy>()/[<Strategy>()...]})   - More powerful variant that allows for passing in other configuration options, none yet defined.
 */
Auth= module.exports = function(optionsOrStrategy) {

  var strategies;
  var options;
  if( !optionsOrStrategy ) throw new Error("You must specify at least one strategy to use the authentication middleware, even if it is anonymous.");
  // Constructor form 1
  if(  Array.isArray(optionsOrStrategy) || ( optionsOrStrategy.authenticate !== undefined  && 
      optionsOrStrategy.strategies === undefined  ) ) {
    strategies= Array.isArray(optionsOrStrategy) ? optionsOrStrategy : [optionsOrStrategy];
    options= {};
  }
  else {
    options= optionsOrStrategy
    strategies= Array.isArray(optionsOrStrategy.strategies) ? optionsOrStrategy.strategies : [optionsOrStrategy.strategies];
  }
  var isAuthenticated = function( request, scope ) {
    if( scope === undefined ) {
      return (request.getAuthDetails().user) ? true : false;
    }
    else {
      return (request.getAuthDetails().scopedUsers[scope] && request.getAuthDetails().scopedUsers[scope].user) ? true : false;
    }
  };     
  
  var defaultGetPasswordForUser= function(username, password, callback) {
    callback( new Error("No mechanism specified for retrieving user passwords.") );
  }; 
  
  function createAuthDetails(req) {
    var auth= { scopedUsers: {} };  
    req._connect_auth= auth;
    if( req.session ) {
      req.session.auth= auth; 
    }
  };
  
  
  var server=  connect.createServer(  
    function auth(req, res, next) {
      // Setup the utility methods
      req.authenticate = function(strategy, opts, callback) {
        var strategy, opts, callback;
        var scope;

        //ughhh pull this rubbish somewhere tidy...
        if( strategy && opts && callback ) {
          var type= typeof strategy;
          if(  strategy.constructor != Array ) {
            strategy= [strategy]
          }
          scope= opts.scope;
        }
        else if( strategy && opts ) {
          callback= opts;
          var type= typeof strategy;
          if( strategy.constructor == Array ) {
            // do nothing
          }
          else if( type == 'string' ) {
            strategy= [strategy];
          }
          else if( type == 'object') {
            scope= strategy.scope
            strategy= undefined;
          }
        }
        else if( strategy ) {
          callback= strategy;
          strategy= undefined;
          //TODO: DEFINE DEFAULT STRATEGY(IES)
        }
        if( isAuthenticated(req, scope) ) callback(null, true);
        else strategyExecutor.authenticate(strategy, scope, req, res, function (error, executionResult) {
          req.getAuthDetails().errorResponse= executionResult.errorResponse;
          if(error) callback(error); // I really wish the plugins code logged errors.. 
          else {
            if( !executionResult.user ) callback(null,false)
            else {
              if( scope === undefined) {
               req.getAuthDetails().user= executionResult.user;
              }
              else {
                if( req.getAuthDetails().scopedUsers[scope] === undefined ) {
                 req.getAuthDetails().scopedUsers[scope] = {};
                }
                req.getAuthDetails().scopedUsers[scope].user=  executionResult.user;
              }              
              callback(null, true)
            }<commit_msg>Fixed bug introduced in a7811e259152125451afd87eecb24a4d5f90bbbb<commit_after>/*!
 * Copyright(c) 2010 <NAME> <<EMAIL>>
 * MIT Licensed
 */

/** 
 * Module dependencies.
 */
var connect= require('connect');
var fs= require('fs');
var StrategyExecutor= require('./strategyExecutor');

/**
 * Construct the authentication middleware.
 * Construction can take 2 forms:
 *    auth(<Strategy>()/[<Strategy>()])  -  A single configured strategy, or array of strategies.
 *    auth({strategies:<Strategy>()/[<Strategy>()...]})   - More powerful variant that allows for passing in other configuration options, none yet defined.
 */
Auth= module.exports = function(optionsOrStrategy) {

  var strategies;
  var options;
  if( !optionsOrStrategy ) throw new Error("You must specify at least one strategy to use the authentication middleware, even if it is anonymous.");
  // Constructor form 1
  if(  Array.isArray(optionsOrStrategy) || ( optionsOrStrategy.authenticate !== undefined  && 
      optionsOrStrategy.strategies === undefined  ) ) {
    strategies= Array.isArray(optionsOrStrategy) ? optionsOrStrategy : [optionsOrStrategy];
    options= {};
  }
  else {
    options= optionsOrStrategy
    strategies= Array.isArray(optionsOrStrategy.strategies) ? optionsOrStrategy.strategies : [optionsOrStrategy.strategies];
  }
  var isAuthenticated = function( request, scope ) {
    if( scope === undefined ) {
      return (request.getAuthDetails().user) ? true : false;
    }
    else {
      return (request.getAuthDetails().scopedUsers[scope] && request.getAuthDetails().scopedUsers[scope].user) ? true : false;
    }
  };     
  
  var defaultGetPasswordForUser= function(username, password, callback) {
    callback( new Error("No mechanism specified for retrieving user passwords.") );
  }; 
  
  function createAuthDetails(req) {
    var auth= { scopedUsers: {} };  
    req._connect_auth= auth;
    if( req.session ) {
      req.session.auth= auth; 
    }
  };
  
  
  var server=  connect.createServer(  
    function auth(req, res, next) {
      // Setup the utility methods
      req.authenticate = function(strategy, opts, callback) {
        var strategy, opts, callback;
        var scope;

        //ughhh pull this rubbish somewhere tidy...
        if( strategy && opts && callback ) {
          var type= typeof strategy;
          if(  strategy.constructor != Array ) {
            strategy= [strategy]
          }
          scope= opts.scope;
        }
        else if( strategy && opts ) {
          callback= opts;
          var type= typeof strategy;
          if( strategy.constructor == Array ) {
            // do nothing
          }
          else if( type == 'string' ) {
            strategy= [strategy];
          }
          else if( type == 'object') {
            scope= strategy.scope
            strategy= undefined;
          }
        }
        else if( strategy ) {
          callback= strategy;
          strategy= undefined;
          //TODO: DEFINE DEFAULT STRATEGY(IES)
        }
        if( isAuthenticated(req, scope) ) callback(null, true);
        else strategyExecutor.authenticate(strategy, scope, req, res, function (error, executionResult) {
          if( executionResult) { req.getAuthDetails().errorResponse= executionResult.errorResponse; }
          if(error) callback(error); // I really wish the plugins code logged errors.. 
          else {
            if( !executionResult.user ) callback(null,false)
            else {
              if( scope === undefined) {
               req.getAuthDetails().user= executionResult.user;
              }
              else {
                if( req.getAuthDetails().scopedUsers[scope] === undefined ) {
                 req.getAuthDetails().scopedUsers[scope] = {};
                }
                req.getAuthDetails().scopedUsers[scope].user=  executionResult.user;
              }              
              callback(null, true)
            }
{'fraction_non_alphanumeric': 0.11601642710472279, 'fraction_numerical': 0.004620123203285421, 'mean_word_length': 2.33461703038083, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6383027', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2058, 'n_words': 544}