From 409d1a1c53267265e05995519b52a7aba4f24bea Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 5 Mar 2014 10:59:23 -0500 Subject: [PATCH] refactor(auth) authenticate --- lib/server/auth.js | 67 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/server/auth.js b/lib/server/auth.js index eb474bc2..8c74b072 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -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); + }); } })();