<commit_before>/**
 * Module dependencies.
 */
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy
  , util = require('util')
  , GooglePlusProfile = require('./profile/googleplus')
  , InternalOAuthError = require('passport-oauth').InternalOAuthError;

var DEPRECATED_SCOPES = {
  'https://www.googleapis.com/auth/userinfo.profile': 'profile',
  'https://www.googleapis.com/auth/userinfo.email': 'email',
};

/**
 * `Strategy` constructor.
 *
 * The Google authentication strategy authenticates requests by delegating to
 * Google using the OAuth 2.0 protocol.
 *
 * Applications must supply a `verify` callback which accepts an `accessToken`,
 * `refreshToken` and service-specific `profile`, and then calls the `done`
 * callback supplying a `user`, which should be set to `false` if the
 * credentials are not valid.  If an exception occured, `err` should be set.
 *
 * Options:
 *   - `clientID`      your Google application's client id
 *   - `clientSecret`  your Google application's client secret
 *   - `callbackURL`   URL to which Google will redirect the user after granting authorization
 *
 * Examples:
 *
 *     passport.use(new GoogleStrategy({
 *         clientID: '123-456-789',
 *         clientSecret: 'shhh-its-a-secret'
 *         callbackURL: 'https://www.example.net/auth/google/callback'
 *       },
 *       function(accessToken, refreshToken, profile, done) {
 *         User.findOrCreate(..., function (err, user) {
 *           done(err, user);
 *         });
 *       }
 *     ));
 *
 * @param {Object} options
 * @param {Function} verify
 * @api public
 */
function Strategy(options, verify) {
  options = options || {};
  options.authorizationURL = options.authorizationURL || 'https://accounts.google.com/o/oauth2/v2/auth';
  options.tokenURL = options.tokenURL || 'https://www.googleapis.com/oauth2/v4/token';

  OAuth2Strategy.call(this, options, verify);
  this.name = 'google';
  this._userProfileURL = options.userProfileURL || 'https://www.googleapis.com/plus/v1/people/me';
  
  //warn deprecated scopes
  if (this._scope) {
    var scopes = Array.isArray(this._scope) ? this._scope : [ this._scope ];
    scopes.forEach(function(scope) {
      var alt = DEPRECATED_SCOPES[scope];
      if (!alt) return;
      console.warn(scope + ' is deprecated. Switch to ' + alt);
    });
  }
}

/**
 * Inherit from `OAuth2Strategy`.
 */
util.inherits(Strategy, OAuth2Strategy);


/**
 * Retrieve user profile from Google.
 *
 * This function constructs a normalized profile, with the following properties:
 *
 *   - `provider`         always set to `google`
 *   - `id`
 *   - `username`
 *   - `displayName`
 *
 * @param {String} accessToken
 * @param {Function} done
 * @api protected
 */
Strategy.prototype.userProfile = function(accessToken, done) {
  // TODO: Switch to new URL:
  // https://www.googleapis.com/oauth2/v3/userinfo
  this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
    if (err) { return done(new InternalOAuthError('Failed to fetch user profile', err)); }

    try {
      var json = JSON.parse(body)
        , i, len;

      var profile = GooglePlusProfile.parse(json);
      profile.provider = 'google';
      profile._raw = body;
      profile._json = json;

      done(null, profile);
    } catch(e) {
      return done(new InternalOAuthError('Failed to parse user profile', err));
    }
  });
}
<commit_msg>Prepare to handle errors from profile endpoint.<commit_after>/**
 * Module dependencies.
 */
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy
  , util = require('util')
  , GooglePlusProfile = require('./profile/googleplus')
  , InternalOAuthError = require('passport-oauth').InternalOAuthError;

var DEPRECATED_SCOPES = {
  'https://www.googleapis.com/auth/userinfo.profile': 'profile',
  'https://www.googleapis.com/auth/userinfo.email': 'email',
};

/**
 * `Strategy` constructor.
 *
 * The Google authentication strategy authenticates requests by delegating to
 * Google using the OAuth 2.0 protocol.
 *
 * Applications must supply a `verify` callback which accepts an `accessToken`,
 * `refreshToken` and service-specific `profile`, and then calls the `done`
 * callback supplying a `user`, which should be set to `false` if the
 * credentials are not valid.  If an exception occured, `err` should be set.
 *
 * Options:
 *   - `clientID`      your Google application's client id
 *   - `clientSecret`  your Google application's client secret
 *   - `callbackURL`   URL to which Google will redirect the user after granting authorization
 *
 * Examples:
 *
 *     passport.use(new GoogleStrategy({
 *         clientID: '123-456-789',
 *         clientSecret: 'sh<PASSWORD>'
 *         callbackURL: 'https://www.example.net/auth/google/callback'
 *       },
 *       function(accessToken, refreshToken, profile, done) {
 *         User.findOrCreate(..., function (err, user) {
 *           done(err, user);
 *         });
 *       }
 *     ));
 *
 * @param {Object} options
 * @param {Function} verify
 * @api public
 */
function Strategy(options, verify) {
  options = options || {};
  options.authorizationURL = options.authorizationURL || 'https://accounts.google.com/o/oauth2/v2/auth';
  options.tokenURL = options.tokenURL || 'https://www.googleapis.com/oauth2/v4/token';

  OAuth2Strategy.call(this, options, verify);
  this.name = 'google';
  this._userProfileURL = options.userProfileURL || 'https://www.googleapis.com/plus/v1/people/me';
  
  //warn deprecated scopes
  if (this._scope) {
    var scopes = Array.isArray(this._scope) ? this._scope : [ this._scope ];
    scopes.forEach(function(scope) {
      var alt = DEPRECATED_SCOPES[scope];
      if (!alt) return;
      console.warn(scope + ' is deprecated. Switch to ' + alt);
    });
  }
}

/**
 * Inherit from `OAuth2Strategy`.
 */
util.inherits(Strategy, OAuth2Strategy);


/**
 * Retrieve user profile from Google.
 *
 * This function constructs a normalized profile, with the following properties:
 *
 *   - `provider`         always set to `google`
 *   - `id`
 *   - `username`
 *   - `displayName`
 *
 * @param {String} accessToken
 * @param {Function} done
 * @api protected
 */
Strategy.prototype.userProfile = function(accessToken, done) {
  // TODO: Switch to new URL:
  // https://www.googleapis.com/oauth2/v3/userinfo
  this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
    var json;
    
    if (err) {
      // TODO
      /*
      if (err.data) {
        try {
          json = JSON.parse(err.data);
        } catch (_) {}
      }
        
      if (json && json.error) {
        return done(new LiveConnectAPIError(json.error.message, json.error.code));
      }
      */
      return done(new InternalOAuthError('Failed to fetch user profile', err));
    }
    
    try {
      json = JSON.parse(body);
    } catch (ex) {
      return done(new Error('Failed to parse user profile'));
    }
    
    var profile = GooglePlusProfile.parse(json);
    profile.provider  = 'google';
    profile._raw = body;
    profile._json = json;
    
    done(null, profile);
  });
}

{'fraction_non_alphanumeric': 0.13891236968160045, 'fraction_numerical': 0.006762468300929839, 'mean_word_length': 3.049629207073588, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 14, 'lorem ipsum': 0, 'www.': 12, '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': '1491284', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2148, 'n_words': 537}