<commit_before>/**
 * Passport Configuration
 */

var _                = require('lodash')
  , cwd              = process.cwd()
  , env              = process.env.NODE_ENV || 'development'
  , path             = require('path')
  , util             = require('util')
  , config           = require(path.join(cwd, 'config.' + env + '.json'))
  , LocalStrategy    = require('passport-local').Strategy
  , OAuth2Strategy   = require('../lib/strategies/OAuth2')
  , TwitterStrategy  = require('passport-twitter').Strategy
  , base64url        = require('base64url')
  , User             = require('../models/User')
  ;


module.exports = function (passport) {


  /**
   * Sessions
   */

  passport.serializeUser(function (user, done) {
    done(null, user._id);
  });

  passport.deserializeUser(function (id, done) {
    User.get(id, function (err, user) {
      done(err, user);
    });
  });


  /**
   * Local Strategy
   */

  passport.use(new LocalStrategy(
    { usernameField: 'email',  passReqToCallback: 'true' },
    function (req, email, password, done) {
      User.authenticate(email, password, function (err, user, info) {
        if (user) {
          // throw password value away so isn't included in URLs/logged
          delete req.connectParams.password;
          delete req.connectParams.email;
        }

        done(err, user, info);
      });
    }
  ));


  /**
   * OAuth Strategies
   */

  var providers = config.providers;

  if (providers) {

    /**
     * Cut the verbosity of the config files
     */

    Object.keys(providers).forEach(function (name) {
      var callbackUrl = config.issuer + '/connect/' + name + '/callback';
      providers[name].callbackURL = callbackUrl;
      providers[name].passReqToCallback = true;
    });


    /**
     * Twitter
     */

    if (typeof providers.twitter === 'object') {
      passport.use(new TwitterStrategy(
        providers.twitter,
        function (request, token, secret, profile, done) {
          User.connect({
            provider: 'twitter',
            user:      request.user,
            token:     token,
            profile:   profile._json
          }, function (err, user) {
            if (err) { return done(err); }
            done(null, user);
          });
        }
      ));
    }

  }



  /**
   * OAuth 2.0 Strategies
   */

  var providers = require('../lib/providers')

  function verifier (request, response, profile, done) {
    User.connect({
      provider: profile.provider,
      user:     request.user,
      token:    response.access_token,
      profile:  profile
    }, function (err, user) {
      if (err) { return done(err); }
      done(null, user);
    });
  }

  Object.keys(providers).forEach(function (name) {
    var prov = providers.name;

    if (prov && prov.protocol === 'OAuth 2.0') {
      passport.use(new OAuth2Strategy(prov.name, prov, verifier));
    }
  });


};<commit_msg>guard for no config.providers case<commit_after>/**
 * Passport Configuration
 */

var _                = require('lodash')
  , cwd              = process.cwd()
  , env              = process.env.NODE_ENV || 'development'
  , path             = require('path')
  , util             = require('util')
  , config           = require(path.join(cwd, 'config.' + env + '.json'))
  , LocalStrategy    = require('passport-local').Strategy
  , OAuth2Strategy   = require('../lib/strategies/OAuth2')
  , TwitterStrategy  = require('passport-twitter').Strategy
  , base64url        = require('base64url')
  , User             = require('../models/User')
  ;


module.exports = function (passport) {


  /**
   * Sessions
   */

  passport.serializeUser(function (user, done) {
    done(null, user._id);
  });

  passport.deserializeUser(function (id, done) {
    User.get(id, function (err, user) {
      done(err, user);
    });
  });


  /**
   * Local Strategy
   */

  passport.use(new LocalStrategy(
    { usernameField: 'email',  passReqToCallback: 'true' },
    function (req, email, password, done) {
      User.authenticate(email, password, function (err, user, info) {
        if (user) {
          // throw password value away so isn't included in URLs/logged
          delete req.connectParams.password;
          delete req.connectParams.email;
        }

        done(err, user, info);
      });
    }
  ));


  /**
   * OAuth Strategies
   */

  var providers = config.providers;

  if (providers) {

    /**
     * Cut the verbosity of the config files
     */

    Object.keys(providers).forEach(function (name) {
      var callbackUrl = config.issuer + '/connect/' + name + '/callback';
      providers[name].callbackURL = callbackUrl;
      providers[name].passReqToCallback = true;
    });


    /**
     * Twitter
     */

    if (typeof providers.twitter === 'object') {
      passport.use(new TwitterStrategy(
        providers.twitter,
        function (request, token, secret, profile, done) {
          User.connect({
            provider: 'twitter',
            user:      request.user,
            token:     token,
            profile:   profile._json
          }, function (err, user) {
            if (err) { return done(err); }
            done(null, user);
          });
        }
      ));
    }

  }



  /**
   * OAuth 2.0 Strategies
   */

  var providers = require('../lib/providers')

  function verifier (request, response, profile, done) {
    User.connect({
      provider: profile.provider,
      user:     request.user,
      token:    response.access_token,
      profile:  profile
    }, function (err, user) {
      if (err) { return done(err); }
      done(null, user);
    });
  }

if (config.providers) {
  Object.keys(config.providers).forEach(function (name) {
    var prov = providers[name]
      , conf = config.providers[name]
      ;

    if (prov && prov.protocol === 'OAuth 2.0') {
      passport.use(new OAuth2Strategy(prov, conf, verifier));
    }
    });
}


};
{'fraction_non_alphanumeric': 0.14374894550362746, 'fraction_numerical': 0.0037118272313143245, 'mean_word_length': 2.0368852459016393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6673769', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1762, 'n_words': 380}