<commit_before>var http = require('follow-redirects').http
, https = require('follow-redirects').https
, MMXManager = require('./MMXManager')
, _ = require('underscore')
, magnetId = require('node-uuid');

var WPOAuthClient = function(){};

WPOAuthClient.prototype.getAuthCodeUrl = function(){
    return (ENV_CONFIG.WPOAuth.ssl ? 'https://' : 'http://') + ENV_CONFIG.WPOAuth.host+'/oauth/authorize' +
        '?response_type=code&client_id=' + ENV_CONFIG.WPOAuth.clientId +
        '&redirect_uri=' + ENV_CONFIG.App.appUrl + '/oauth-login';
};

WPOAuthClient.prototype.getAccessToken = function(user, code, cb) {
    if(!code) return cb('invalid-code');
    var me = this;
    me.request('POST', '/oauth/token', 'grant_type=authorization_code&code='+code+'&redirect_uri='+ENV_CONFIG.App.appUrl + '/oauth-login', function(e, oauthSession){
        if(e) return cb(e);
        me.getUserInfo(user, oauthSession.access_token, function(e, fullUser){
            if(e) return cb(e);
            cb(null, fullUser);
        });
    });
};

WPOAuthClient.prototype.typeMap = function(capability){
    capability = capability || {};
    if(capability.customer || capability.magnetemployee || capability.developer || capability.builder)
        return 'developer';
    if(capability.administrator)
        return 'admin';
    //if(capability.subscriber)
    return 'developer';
};

WPOAuthClient.prototype.getUserInfo = function(user, access_token, cb) {
    var me = this;
    if(!access_token) return cb('invalid-token');
    me.request('GET', '/oauth/me?access_token='+access_token, null, function(e, data){
        if(e) return cb(e);
        user.email = data.user_email;
        user.magnetId = data.user_email;
        user.userType = me.typeMap(data.wp_capabilities);
        user.hasMMXApp = true;
        user.activated = true;
        var name = data.display_name.split(' ');
        if(name.length > 0){
            user.firstName = name[0];
        }
        name.shift();
        if(name.length > 0){
            user.lastName = name.join(' ');
        }
        cb(null, user);
    });
};

WPOAuthClient.prototype.request = function(method, path, data, cb){
    var protocol = ENV_CONFIG.WPOAuth.ssl === true ? https : http;
    var req = {
        host               : ENV_CONFIG.WPOAuth.host,
        port               : ENV_CONFIG.WPOAuth.ssl === true ? 443 : 80,
        path               : path,
        method             : method,
        rejectUnauthorized : false,
        requestCert        : false,
        headers : {
            'Content-Type'  : 'application/x-www-form-urlencoded',
            'Authorization' : 'Basic ' + new Buffer(ENV_CONFIG.WPOAuth.clientId + ':' + ENV_CONFIG.WPOAuth.clientSecret).toString('base64')
        }
    };
    winston.silly('OAuth: remote call - ', req);
    var call = protocol.request(req, function(res){
        var data = '';
        res.setEncoding('utf8');
        res.on('data', function(chunk){
            data += chunk;
        }).on('end', function(){
            try{
                data = JSON.parse(data);
            }catch(e){}
            if(!isSuccess(res.statusCode)){
                winston.verbose('OAuth: failed to make remote call to '+req.host+':'+req.port+req.path+': code - '+res.statusCode+' response: ', data);
            }
            if(typeof cb === typeof Function){
                if(isSuccess(res.statusCode)){
                    cb(null, data, res.statusCode);
                }else{
                    cb((typeof data == 'object' && data.message) ? data.message : 'request-failed', data, res.statusCode);
                }<commit_msg>fix content-length problem for wpengine<commit_after>var http = require('follow-redirects').http
, https = require('follow-redirects').https
, MMXManager = require('./MMXManager')
, _ = require('underscore')
, magnetId = require('node-uuid');

var WPOAuthClient = function(){};

WPOAuthClient.prototype.getAuthCodeUrl = function(){
    return (ENV_CONFIG.WPOAuth.ssl ? 'https://' : 'http://') + ENV_CONFIG.WPOAuth.host+'/oauth/authorize' +
        '?response_type=code&client_id=' + ENV_CONFIG.WPOAuth.clientId +
        '&redirect_uri=' + ENV_CONFIG.App.appUrl + '/oauth-login';
};

WPOAuthClient.prototype.getAccessToken = function(user, code, cb) {
    if(!code) return cb('invalid-code');
    var me = this;
    me.request('POST', '/oauth/token', 'grant_type=authorization_code&code='+code+'&redirect_uri='+ENV_CONFIG.App.appUrl + '/oauth-login', function(e, oauthSession){
        if(e) return cb(e);
        me.getUserInfo(user, oauthSession.access_token, function(e, fullUser){
            if(e) return cb(e);
            cb(null, fullUser);
        });
    });
};

WPOAuthClient.prototype.typeMap = function(capability){
    capability = capability || {};
    if(capability.customer || capability.magnetemployee || capability.developer || capability.builder)
        return 'developer';
    if(capability.administrator)
        return 'admin';
    //if(capability.subscriber)
    return 'developer';
};

WPOAuthClient.prototype.getUserInfo = function(user, access_token, cb) {
    var me = this;
    if(!access_token) return cb('invalid-token');
    me.request('GET', '/oauth/me?access_token='+access_token, null, function(e, data){
        if(e) return cb(e);
        user.email = data.user_email;
        user.magnetId = data.user_email;
        user.userType = me.typeMap(data.wp_capabilities);
        user.hasMMXApp = true;
        user.activated = true;
        var name = data.display_name.split(' ');
        if(name.length > 0){
            user.firstName = name[0];
        }
        name.shift();
        if(name.length > 0){
            user.lastName = name.join(' ');
        }
        cb(null, user);
    });
};

WPOAuthClient.prototype.request = function(method, path, data, cb){
    var protocol = ENV_CONFIG.WPOAuth.ssl === true ? https : http;
    var req = {
        host               : ENV_CONFIG.WPOAuth.host,
        port               : ENV_CONFIG.WPOAuth.ssl === true ? 443 : 80,
        path               : path,
        method             : method,
        rejectUnauthorized : false,
        requestCert        : false,
        headers : {
            'Content-Length' : data ? Buffer.byteLength(data, 'utf8') : 0,
            'Content-Type'   : 'application/x-www-form-urlencoded',
            'Authorization'  : 'Basic ' + new Buffer(ENV_CONFIG.WPOAuth.clientId + ':' + ENV_CONFIG.WPOAuth.clientSecret).toString('base64')
        }
    };
    winston.silly('OAuth: remote call - ', req);
    var call = protocol.request(req, function(res){
        var data = '';
        res.setEncoding('utf8');
        res.on('data', function(chunk){
            data += chunk;
        }).on('end', function(){
            try{
                data = JSON.parse(data);
            }catch(e){}
            if(!isSuccess(res.statusCode)){
                winston.verbose('OAuth: failed to make remote call to '+req.host+':'+req.port+req.path+': code - '+res.statusCode+' response: ', data);
            }
            if(typeof cb === typeof Function){
                if(isSuccess(res.statusCode)){
                    cb(null, data, res.statusCode);
                }else{
                    cb((typeof data == 'object' && data.message) ? data.message : 'request-failed', data, res.statusCode);
                }
{'fraction_non_alphanumeric': 0.15225946532772425, 'fraction_numerical': 0.003256886958881802, 'mean_word_length': 2.4057301293900184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3265375', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2237, 'n_words': 405}