refactor(auth) authenticate

This commit is contained in:
coderaiser 2014-03-05 10:59:23 -05:00
parent 4dc038b277
commit 409d1a1c53

View file

@ -11,7 +11,7 @@
'# If you wont to see at work set auth' + '\n' +
'# parameters in config.json or environment' + '\n' +
'# and start cloudcmd.js or just do' + '\n' +
'# require(\'auth.js\').auth(pCode, pCallBack)' + '\n' +
'# require(\'auth.js\').auth(code, сallback)' + '\n' +
'# http://cloudcmd.io' + '\n');
var main = global.cloudcmd.main,
@ -31,56 +31,57 @@
/**
* function do authentication
* @param pCode
* @param pCallBack
* @param code
* @param callback
*/
exports.auth = function(pCode, pCallBack){
pCode = pCode.replace('code=', '');
exports.auth = function(code, callback){
code = code.replace('code=', '');
console.log(pCode);
authenticate(pCode, function(token) {
Util.log(code);
authenticate(code, function(token) {
var result = { "token": token };
Util.log(result);
Util.exec(pCallBack, result);
Util.exec(callback, result);
});
};
function authenticate(pCode, pCallBack) {
var lStorage = Util.findObjByNameInArr(Modules, 'storage'),
lGitHub = Util.findObjByNameInArr(lStorage, 'GitHub'),
function authenticate(code, callback) {
var body, req,
storage = Util.findObjByNameInArr(Modules, 'storage'),
github = Util.findObjByNameInArr(storage, 'GitHub'),
lId = lGitHub && lGitHub.key,
lSecret = lGitHub && lGitHub.secret,
lEnv = process.env,
id = lGitHub && lGitHub.key,
secret = lGitHub && lGitHub.secret,
env = process.env,
lClientId = lEnv.github_key || lId,
lClientSecret = lEnv.github_secret || lSecret;
clientId = env.github_key || id,
clientSecret = env.github_secret || secret;
data = qs.stringify({
client_id : clientId,
client_secret : clientSecret,
code : code
});
Util.log(lClientId);
Util.log(lClientSecret);
var data = qs.stringify({
client_id : lClientId,
client_secret : lClientSecret,
code : pCode
});
Util.log(data);
Util.log(clientId, clientSecret, data);
GithubAuth.headers = { 'content-length': data.length };
var body = "",
req = https.request(GithubAuth, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) { body += chunk; });
res.on('end', function() {
Util.exec(pCallBack, qs.parse(body).access_token);
});
body = "",
req = https.request(GithubAuth, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) { body += chunk; });
res.on('end', function() {
Util.exec(pCallBack, qs.parse(body).access_token);
});
});
req.write(data);
req.end();
req.on('error', function(e) { Util.exec(pCallBack, e.message); });
req.on('error', function(e) {
Util.exec(callback, e.message);
});
}
})();