<commit_before>/*!
 * Copyright(c) 2010 <NAME> <<EMAIL>>
 * MIT Licensed
 */
var OAuth= require("oauth").OAuth,
    url = require("url"),
    connect = require("connect"),
    http = require('http'),

Google = module.exports = function(options, server) {
  options = options || {};
  var that = {};
  var my = {};

  // Construct the internal OAuth client
  my._oAuth = new OAuth("https://www.google.com/accounts/OAuthGetRequestToken",
                        "https://www.google.com/accounts/OAuthGetAccessToken",
                        options.consumerKey, options.consumerSecret,
                        "1.0", options.callback, "HMAC-SHA1");
  
  // Give the strategy a name
  that.name = options.name || "google";

  // Build the authentication routes required 
  that.setupRoutes = function(server) {
    server.use('/', connect.router(function routes(app){
      app.get('/auth/google_callback', function(req, res){
        req.authenticate([that.name], function(error, authenticated) {
          res.writeHead(303, { 'Location': req.session.google_redirect_url });
          res.end('');
        });
      });
    }));
  };

  // Declare the method that actually does the authentication
  that.authenticate = function(request, response, callback) {
    //todo: if multiple connect middlewares were doing this, it would be more efficient to do it in the stack??
    var parsedUrl = url.parse(request.url, true);
    //todo: makw the call timeout ....
    var self= this;

    // get oauth access token
    if( parsedUrl.query &&
        parsedUrl.query.oauth_token &&
        parsedUrl.query.oauth_verifier &&
        request.session.auth["google_oauth_token_secret"] &&
        parsedUrl.query.oauth_token == request.session.auth['google_oauth_token'] ) {

      my._oAuth.getOAuthAccessToken(
        parsedUrl.query.oauth_token,
        request.session.auth["google_oauth_token_secret"],
        parsedUrl.query.oauth_verifier,
        function(error, oauth_token, oauth_token_secret, additionalParameters) {
          if( error ) callback(null);
          else {
            my._oAuth.getProtectedResource(
              "https://www.google.com/m8/feeds/contacts/default/full?alt=json",
              "GET", oauth_token, oauth_token_secret,
              function(error, data){
                if( error ) callback(null);
                var profile = { 'username': JSON.parse(data).feed.id.$t };
                request.session.auth["google_oauth_token_secret"] = oauth_token_secret;
                request.session.auth["google_oauth_token"] = oauth_token;
                self.success(profile, callback);
              });
          }
        });
    }
    // get oauth request token
    else {
      var scope = "https://www.google.com/m8/feeds/ " + options.scope;
      my._oAuth.getOAuthRequestToken(
        { "scope": scope },
        function(error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters ) {
          if(error) {
            callback(null); // Ignore the error upstream, treat as validation failure.
          } else {
            request.session['google_redirect_url'] = request.url;
            request.session.auth["google_oauth_token_secret"] = oauth_token_secret;
            request.session.auth["google_oauth_token"] = oauth_token;
            var authTokenURL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=" + oauth_token;
            if (options.hd) authTokenURL += '&hd=' + options.hd;
            self.redirect(response, authTokenURL, callback);
          }
        });
    }

  };
  return that;
};
<commit_msg>Apply fix to Google Strategy to stop infinite retry when authentication fails<commit_after>/*!
 * Copyright(c) 2010 <NAME> <<EMAIL>>
 * MIT Licensed
 */
var OAuth= require("oauth").OAuth,
    url = require("url"),
    connect = require("connect"),
    http = require('http'),

Google = module.exports = function(options, server) {
  options = options || {};
  var that = {};
  var my = {};

  // Construct the internal OAuth client
  my._oAuth = new OAuth("https://www.google.com/accounts/OAuthGetRequestToken",
                        "https://www.google.com/accounts/OAuthGetAccessToken",
                        options.consumerKey, options.consumerSecret,
                        "1.0", options.callback, "HMAC-SHA1");
  
  // Give the strategy a name
  that.name = options.name || "google";

  // Build the authentication routes required 
  that.setupRoutes = function(server) {
    server.use('/', connect.router(function routes(app){
      app.get('/auth/google_callback', function(req, res){
        req.authenticate([that.name], function(error, authenticated) {
          res.writeHead(303, { 'Location': req.session.google_redirect_url });
          res.end('');
        });
      });
    }));
  };

  // Declare the method that actually does the authentication
  that.authenticate = function(request, response, callback) {
    //todo: if multiple connect middlewares were doing this, it would be more efficient to do it in the stack??
    var parsedUrl = url.parse(request.url, true);
    //todo: makw the call timeout ....
    var self= this;
    if( request.getAuthDetails()['github_login_attempt_failed'] === true ) {
      // Because we bounce through authentication calls across multiple requests
      // we use this to keep track of the fact we *Really* have failed to authenticate
      // so that we don't keep re-trying to authenticate forever.
      // (To clarify this infinite retry that we're stopping here would only
      //  occur when the attempt has failed, not when it has succeeded!!!)
      delete request.getAuthDetails()['github_login_attempt_failed'];
      self.fail( callback );
    }
    else {
      // get oauth access token
      if( parsedUrl.query &&
          parsedUrl.query.oauth_token &&
          parsedUrl.query.oauth_verifier &&
          request.session.auth["google_oauth_token_secret"] &&
          parsedUrl.query.oauth_token == request.session.auth['google_oauth_token'] ) {

        my._oAuth.getOAuthAccessToken(
          parsedUrl.query.oauth_token,
          request.session.auth["google_oauth_token_secret"],
          parsedUrl.query.oauth_verifier,
          function(error, oauth_token, oauth_token_secret, additionalParameters) {
            if( error ) {
              request.getAuthDetails()['github_login_attempt_failed'] = true;
              self.fail(callback);
            }
            else {
              my._oAuth.getProtectedResource(
                "https://www.google.com/m8/feeds/contacts/default/full?alt=json",
                "GET", oauth_token, oauth_token_secret,
                function(error, data){
                  if( error ) callback(null);
                  var profile = { 'username': JSON.parse(data).feed.id.$t };
                  request.session.auth["google_oauth_token_secret"] = oauth_token_secret;
                  request.session.auth["google_oauth_token"] = oauth_token;
                  self.success(profile, callback);
                });
            }
          });
      }
      // get oauth request token
      else {
        var scope = "https://www.google.com/m8/feeds/ " + options.scope;
        my._oAuth.getOAuthRequestToken(
          { "scope": scope },
          function(error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters ) {
            if(error) {
              callback(null); // Ignore the error upstream, treat as validation failure.
            } else {
              request.session['google_redirect_url'] = request.url;
              request.session.auth["google_oauth_token_secret"] = oauth_token_secret;
              request.session.auth["google_oauth_token"] = oauth_token;
              var authTokenURL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=" + oauth_token;
              if (options.hd) authTokenURL += '&hd=' + options.hd;
              self.redirect(response, authTokenURL, callback);
            }
          });
      }
    }
  };
  return that;
};

{'fraction_non_alphanumeric': 0.12242364042711697, 'fraction_numerical': 0.0029798857710454435, 'mean_word_length': 2.395868465430017, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 10, 'lorem ipsum': 0, 'www.': 10, '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': '7194716', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2259, 'n_words': 480}