<commit_before>var util = require('util'),
    OAuth2Strategy = require('passport-oauth').OAuth2Strategy,
    InternalOAuthError = require('passport-oauth').InternalOAuthError;

util.inherits(YahooTokenStrategy, OAuth2Strategy);

/**
 * `YahooTokenStrategy` constructor.
 * The Yahoo authentication strategy authenticates requests by delegating to Yahoo using the OAuth 2.0 protocol.
 * Applications must supply a `verify` callback which accepts an accessToken, refreshToken, profile, and callback.
 * Callback supplying a `user`, which should be set to `false` if the credentials are not valid.
 * If an exception occurred, `error` should be set.
 *
 * Options:
 * - clientID          your Yahoo application's App ID
 * - clientSecret      your Yahoo application's App Secret
 * - passReqToCallback If need, pass req to verify callback
 *
 * Example:
 *     passport.use(new YahooTokenStrategy({
 *           clientID: '123-456-789',
 *           clientSecret: 'shhh-its-a-secret',
 *           passReqToCallback: true
 *       }, function(req, accessToken, refreshToken, profile, next) {
 *              User.findOrCreate(..., function (error, user) {
 *                  next(error, user);
 *              });
 *          }
 *       ));
 *
 * @param {Object} options
 * @param {Function} verify
 * @constructor
 */
function YahooTokenStrategy(options, verify) {
    options = options || {};
    options.authorizationURL = options.authorizationURL || 'https://api.login.yahoo.com/oauth2/request_auth';
    options.tokenURL = options.tokenURL || 'https://api.login.yahoo.com/oauth2/get_token';

    OAuth2Strategy.call(this, options, verify);

    this.name = 'yahoo-token';
    this._passReqToCallback = options.passReqToCallback;
    this._oauth2._useAuthorizationHeaderForGET = true;
}

/**
 * Authenticate method
 * @param {Object} req Request stream
 * @param {Object} options Options object
 * @returns {*}
 */
YahooTokenStrategy.prototype.authenticate = function (req, options) {
    var self = this,
        accessToken = (req.body && req.body.access_token) || req.query.access_token || req.headers.access_token,
        refreshToken = (req.body && req.body.refresh_token) || req.query.refresh_token || req.headers.refresh_token,
        xoauthYahooGuid = (req.body && req.body.xoauth_yahoo_guid) || req.query.xoauth_yahoo_guid || req.headers.xoauth_yahoo_guid;

    if (!(accessToken && xoauthYahooGuid)) {
        return this.fail({message: 'You should provide access_token and xoauth_yahoo_guid'});
    }

    self._loadUserProfile({
        accessToken: accessToken,
        xoauthYahooGuid: xoauthYahooGuid
    }, function (error, profile) {
        if (error) return self.error(error);

        function verified(error, user, info) {
            if (error) return self.error(error);
            if (!user) return self.fail(info);

            return self.success(user, info);
        }

        if (self._passReqToCallback) {
            self._verify(req, accessToken, refreshToken, profile, verified);
        } else {
            self._verify(accessToken, refreshToken, profile, verified);
        }
    });
};

/**
 * Get user profile from Yahoo
 * @param {String} accessToken Yahoo access token
 * @param {String} xoauthYahooGuid Yahoo GUID
 * @param {Function} done
 */
YahooTokenStrategy.prototype.userProfile = function (accessToken, xoauthYahooGuid, done) {
    this._oauth2.get('https://social.yahooapis.com/v1/user/' + xoauthYahooGuid + '/profile?format=json', accessToken, function (error, body, res) {
        if (error) return done(new InternalOAuthError('Failed to fetch user profile', error));

        try {
            var json = JSON.parse(body),
                profile = {
                    provider: 'yahoo',
                    id: json.profile.guid,
                    displayName: [json.profile.givenName || '', json.profile.familyName || ''].join(' '),
                    name: {
                        familyName: json.profile.familyName || '',
                        givenName: json.profile.givenName || ''
                    },
                    emails: [],
                    photos: [{
                        value: (json.profile.image && json.profile.image.imageUrl) || ''
                    }],
                    _raw: body,
                    _json: json
                };

            done(null, profile);
        } catch (e) {
            done(e);
        }
    });
};

/**
 * Load user profile
 * @param {Object} params
 * @param {Function} done
 * @private
 */
YahooTokenStrategy.prototype._loadUserProfile = function (params, done) {
    var self = this;

    function loadIt() {
        return self.userProfile(params.accessToken, params.xoauthYahooGuid, done);
    }

    function skipIt() {
        return done(null);
    }

    if (typeof this._skipUserProfile == 'function' && this._skipUserProfile.length > 1) {
        this._skipUserProfile(params.accessToken, function (error, skip) {
            if (error) {
                return done(error);
            }

            if (!skip) {
                return loadIt();
            }

            return skipIt();
        });
    } else {
        var skip = (typeof this._skipUserProfile == 'function') ? this._skipUserProfile() : this._skipUserProfile;

        if (!skip) {
            return loadIt();
        }

        return skipIt();
    }
};

module.exports = YahooTokenStrategy;<commit_msg>Simplify _loadUserProfile<commit_after>var util = require('util'),
    OAuth2Strategy = require('passport-oauth').OAuth2Strategy,
    InternalOAuthError = require('passport-oauth').InternalOAuthError;

util.inherits(YahooTokenStrategy, OAuth2Strategy);

/**
 * `YahooTokenStrategy` constructor.
 * The Yahoo authentication strategy authenticates requests by delegating to Yahoo using the OAuth 2.0 protocol.
 * Applications must supply a `verify` callback which accepts an accessToken, refreshToken, profile, and callback.
 * Callback supplying a `user`, which should be set to `false` if the credentials are not valid.
 * If an exception occurred, `error` should be set.
 *
 * Options:
 * - clientID          your Yahoo application's App ID
 * - clientSecret      your Yahoo application's App Secret
 * - passReqToCallback If need, pass req to verify callback
 *
 * Example:
 *     passport.use(new YahooTokenStrategy({
 *           clientID: '123-456-789',
 *           clientSecret: 'shhh-<PASSWORD>',
 *           passReqToCallback: true
 *       }, function(req, accessToken, refreshToken, profile, next) {
 *              User.findOrCreate(..., function (error, user) {
 *                  next(error, user);
 *              });
 *          }
 *       ));
 *
 * @param {Object} options
 * @param {Function} verify
 * @constructor
 */
function YahooTokenStrategy(options, verify) {
    options = options || {};
    options.authorizationURL = options.authorizationURL || 'https://api.login.yahoo.com/oauth2/request_auth';
    options.tokenURL = options.tokenURL || 'https://api.login.yahoo.com/oauth2/get_token';

    OAuth2Strategy.call(this, options, verify);

    this.name = 'yahoo-token';
    this._passReqToCallback = options.passReqToCallback;
    this._oauth2._useAuthorizationHeaderForGET = true;
}

/**
 * Authenticate method
 * @param {Object} req Request stream
 * @param {Object} options Options object
 * @returns {*}
 */
YahooTokenStrategy.prototype.authenticate = function (req, options) {
    var self = this,
        accessToken = (req.body && req.body.access_token) || req.query.access_token || req.headers.access_token,
        refreshToken = (req.body && req.body.refresh_token) || req.query.refresh_token || req.headers.refresh_token,
        xoauthYahooGuid = (req.body && req.body.xoauth_yahoo_guid) || req.query.xoauth_yahoo_guid || req.headers.xoauth_yahoo_guid;

    if (!(accessToken && xoauthYahooGuid)) {
        return this.fail({message: 'You should provide access_token and xoauth_yahoo_guid'});
    }

    self._loadUserProfile({
        accessToken: accessToken,
        xoauthYahooGuid: xoauthYahooGuid
    }, function (error, profile) {
        if (error) return self.error(error);

        function verified(error, user, info) {
            if (error) return self.error(error);
            if (!user) return self.fail(info);

            return self.success(user, info);
        }

        if (self._passReqToCallback) {
            self._verify(req, accessToken, refreshToken, profile, verified);
        } else {
            self._verify(accessToken, refreshToken, profile, verified);
        }
    });
};

/**
 * Get user profile from Yahoo
 * @param {String} accessToken Yahoo access token
 * @param {String} xoauthYahooGuid Yahoo GUID
 * @param {Function} done
 */
YahooTokenStrategy.prototype.userProfile = function (accessToken, xoauthYahooGuid, done) {
    this._oauth2.get('https://social.yahooapis.com/v1/user/' + xoauthYahooGuid + '/profile?format=json', accessToken, function (error, body, res) {
        if (error) return done(new InternalOAuthError('Failed to fetch user profile', error));

        try {
            var json = JSON.parse(body),
                profile = {
                    provider: 'yahoo',
                    id: json.profile.guid,
                    displayName: [json.profile.givenName || '', json.profile.familyName || ''].join(' '),
                    name: {
                        familyName: json.profile.familyName || '',
                        givenName: json.profile.givenName || ''
                    },
                    emails: [],
                    photos: [{
                        value: (json.profile.image && json.profile.image.imageUrl) || ''
                    }],
                    _raw: body,
                    _json: json
                };

            done(null, profile);
        } catch (e) {
            done(e);
        }
    });
};

/**
 * Load user profile
 * @param {Object} params
 * @param {Function} done
 * @private
 */
YahooTokenStrategy.prototype._loadUserProfile = function (params, done) {
    return this.userProfile(params.accessToken, params.xoauthYahooGuid, done);
};

module.exports = YahooTokenStrategy;
{'fraction_non_alphanumeric': 0.121545874717278, 'fraction_numerical': 0.004031861539974432, 'mean_word_length': 2.4451219512195124, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '5766426', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2790, 'n_words': 665}