<commit_before>var config = require('./config').config
  , redis = require("redis")
  , _ = require('underscore')._
  , neuron = require('neuron')
  , winston = require('./logging').winston
  , oauth = require('oauth').OAuth
;
  
require('date-utils');

/*
	Connect to redis
*/
var client = redis.createClient(config.redis.port, config.redis.address);
if (config.redis.auth) {
	client.auth(config.redis.auth, function (err) {
		if (err) { 
			// handle err; 
		}
	});
}

var manager = new neuron.JobManager();

manager.addJob('finishAuth', {
	work: function( tout, cb, authParams ) {
		cb(tout, authParams);
	}
});

manager.addJob('getTemporaryCredentials', {
	work: function( api, bid, key, cb ) {
		
		var oaData = config.authentication[api.auth.provider];
		var oa = new oauth(oaData.requestTemporaryCredentials,
              oaData.requestAccessToken,
              oaData.oauth_consumer_key,
              oaData.client_secret,
              oaData.version,
              oaData.authorize,
              oaData.encryption),
              self = this;
              
        oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
            if(error) {
	            manager.enqueue('finishAuth', false, cb);
	            self.finished = true;
	        } else { 
		        api.credentials = {
		        	'oauth_token': oauth_token,
		        	'oauth_token_secret': oauth_token_secret,
		        	'type': 'oauth',
		        	'provider': api.auth.provider
		        };
		        client.set(bid+key+'oauth', JSON.stringify(api.credentials));
            	var tout = {
	  				expires: new Date(),
	  				redirect: oaData.authorize+
	  					"?oauth_token="+api.credentials.oauth_token+"&oauth_nonce="+bid+","+key,
	  				err: {errnum:1, errtxt:"Authentication provider requires code."},
	  				cname: key
	  			};
	  			manager.enqueue('finishAuth', tout, cb);
  				self.finished = true;		        				   
			}
		});
	}
});

manager.addJob('getAccessToken', {
	work: function(api, bid, key, oauth_token, cb) {
		
		var oaData = config.authentication[api.auth.provider],
			self = this;
		
		var oa = new oauth(oaData.requestTemporaryCredentials,
              oaData.requestAccessToken,
              oaData.oauth_consumer_key,
              oaData.client_secret,
              oaData.version,
              oaData.authorize,
              oaData.encryption);

        oa.getOAuthAccessToken(oauth_token, api.credentials.oauth_token_secret, function(error, oauth_access_token, oauth_access_token_secret, results2) {
	    
	    	if(error) {
		        // handle error
		        manager.enqueue('finishAuth', false, cb);
		        self.finsihed = true;
	    	} else { 
	        	api.credentials.oauth_access_token = oauth_access_token;
	        	api.credentials.oauth_token_secret = oauth_access_token_secret;
	        	api.credentials.oauth_consumer_key = oaData.oauth_consumer_key;
	        	api.credentials.authConfig = oaData;
	        	client.set(bid+key+'oauth', JSON.stringify(api.credentials));
	        	var tout = {
	  				expires: new Date(),
	  				redirect: config.url+'/bundle/'+bid,
	  				err: {errnum:1, errtxt:"Authentication provider requires code."},
	  				cname: key
	  			};
	  			manager.enqueue('finishAuth', tout, cb, {
					"oauth_consumer_key": api.credentials.oauth_consumer_key, 
					//"oauth_nonce": bid+","+key,
					//"oauth_signature": xxx,
					"oauth_signature_method": oaData.encryption,
					"oauth_timestamp": Math.floor( (new Date()).getTime() / 1000 ),
					"oauth_token": api.credentials.oauth_access_token
				});
	  			self.finsihed = true;
	  		}
	   });
	}
})

exports.authorize = function( api, bid, key, cb ) {
	<commit_msg>Handle missing authentication provider details<commit_after>var config = require('./config').config
  , redis = require("redis")
  , _ = require('underscore')._
  , neuron = require('neuron')
  , winston = require('./logging').winston
  , oauth = require('oauth').OAuth
;
  
require('date-utils');

/*
	Connect to redis
*/
var client = redis.createClient(config.redis.port, config.redis.address);
if (config.redis.auth) {
	client.auth(config.redis.auth, function (err) {
		if (err) { 
			// handle err; 
		}
	});
}

var manager = new neuron.JobManager();

manager.addJob('finishAuth', {
	work: function( tout, cb, authParams ) {
		cb(tout, authParams);
	}
});

manager.addJob('getTemporaryCredentials', {
	work: function( api, bid, key, cb ) {
		
		var oaData = config.authentication[api.auth.provider];
		var oa = new oauth(oaData.requestTemporaryCredentials,
              oaData.requestAccessToken,
              oaData.oauth_consumer_key,
              oaData.client_secret,
              oaData.version,
              oaData.authorize,
              oaData.encryption),
              self = this;
              
        oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
            if(error) {
	            manager.enqueue('finishAuth', false, cb);
	            self.finished = true;
	        } else { 
		        api.credentials = {
		        	'oauth_token': oauth_token,
		        	'oauth_token_secret': oauth_token_secret,
		        	'type': 'oauth',
		        	'provider': api.auth.provider
		        };
		        client.set(bid+key+'oauth', JSON.stringify(api.credentials));
            	var tout = {
	  				expires: new Date(),
	  				redirect: oaData.authorize+
	  					"?oauth_token="+api.credentials.oauth_token+"&oauth_nonce="+bid+","+key,
	  				err: {errnum:1, errtxt:"Authentication provider requires code."},
	  				cname: key
	  			};
	  			manager.enqueue('finishAuth', tout, cb);
  				self.finished = true;		        				   
			}
		});
	}
});

manager.addJob('getAccessToken', {
	work: function(api, bid, key, oauth_token, cb) {
		
		var oaData = config.authentication[api.auth.provider],
			self = this;
		
		var oa = new oauth(oaData.requestTemporaryCredentials,
              oaData.requestAccessToken,
              oaData.oauth_consumer_key,
              oaData.client_secret,
              oaData.version,
              oaData.authorize,
              oaData.encryption);

        oa.getOAuthAccessToken(oauth_token, api.credentials.oauth_token_secret, function(error, oauth_access_token, oauth_access_token_secret, results2) {
	    
	    	if(error) {
		        // handle error
		        manager.enqueue('finishAuth', false, cb);
		        self.finsihed = true;
	    	} else { 
	        	api.credentials.oauth_access_token = oauth_access_token;
	        	api.credentials.oauth_token_secret = oauth_access_token_secret;
	        	api.credentials.oauth_consumer_key = oaData.oauth_consumer_key;
	        	api.credentials.authConfig = oaData;
	        	client.set(bid+key+'oauth', JSON.stringify(api.credentials));
	        	var tout = {
	  				expires: new Date(),
	  				redirect: config.url+'/bundle/'+bid,
	  				err: {errnum:1, errtxt:"Authentication provider requires code."},
	  				cname: key
	  			};
	  			manager.enqueue('finishAuth', tout, cb, {
					"oauth_consumer_key": api.credentials.oauth_consumer_key, 
					//"oauth_nonce": bid+","+key,
					//"oauth_signature": xxx,
					"oauth_signature_method": oaData.encryption,
					"oauth_timestamp": Math.floor( (new Date()).getTime() / 1000 ),
					"oauth_token": api.credentials.oauth_access_token
				});
	  			self.finsihed = true;
	  		}
	   });
	}
})

exports.authorize = function( api, bid, key, cb ) {

	if (!_.has(config, 'authentication') || !_.has(config.authentication, api.auth.provider)) {
		winston.error('Authentication provider ' + api.auth.provider + ' node defined');
		return false;
	}
	
{'fraction_non_alphanumeric': 0.13062295081967212, 'fraction_numerical': 0.0018360655737704918, 'mean_word_length': 2.5387470997679813, 'pattern_counts': {'":': 12, '<': 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': '2299142', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2488, 'n_words': 404}