From 21e5dae5144838fa783bdc72bb8af319a78223d7 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 16 Nov 2012 06:44:27 -0500 Subject: [PATCH] setted up auth on GitHub --- ChangeLog | 2 + lib/client/storage/_github.js | 11 +++++ lib/server/auth.js | 6 +-- lib/server/rest.js | 79 +++++++++++++++++++++++++---------- server.js | 12 +----- 5 files changed, 76 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index d1ba1bb1..812a1317 100644 --- a/ChangeLog +++ b/ChangeLog @@ -94,6 +94,8 @@ when CodeMirror is open on the right panel. * Function generateHeaders moved to main module. +* Setted up auth on GitHub thru rest. + 2012.10.01, Version 0.1.7 diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index 42359d26..2bbfb2bb 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -69,6 +69,16 @@ var CloudCommander, Util, DOM, $, Github; if ( Util.isContainStr(lCode, '?code=') ){ lCode = lCode.replace('?code=',''); + + $.ajax( + {type:'put', + url:'/api/v1/auth', + data: lCode, + success: function(pDate){ + console.log(pDate);} + }); + + /* $.post("https://github.com/login/oauth/access_token",{ client_id : CLIENT_ID, client_secret : CLIENT_SECRET, @@ -80,6 +90,7 @@ var CloudCommander, Util, DOM, $, Github; //GithubStore.Login(lToken); console.log(pDate); }, "json"); + */ } else cloudcmd.Auth(); diff --git a/lib/server/auth.js b/lib/server/auth.js index 7b43bc4c..0e808144 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -25,15 +25,15 @@ * @param pCallBack */ - exports.auth = function(pCode, pCallBack){ - pCode = pCode.replace('code=',''); + exports.auth = function(pCode, pCallBack){ + pCode = pCode.replace('code=',''); console.log(pCode); authenticate(pCode, function(token) { var result = { "token": token }; console.log(result); - Util.exec(pCallBack); + Util.exec(pCallBack, result); }); }; diff --git a/lib/server/rest.js b/lib/server/rest.js index 3b628e5a..c70955f9 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -22,12 +22,20 @@ lMethod = lReq.method; if( Util.isContainStr(lUrl, APIURL) ){ - var lCommand = Util.removeStr(lUrl, APIURL), - lData = getData(lMethod, lCommand); - - send(lRes, lData); - lRet = true; + + getBody(lReq, function(pBody){ + var lCommand = Util.removeStr(lUrl, APIURL), + lData = getData({ + command : lCommand, + method : lMethod, + body : pBody, + response : lRes + }); + + if(lData) + send(lRes, lData); + }); } return lRet; @@ -42,25 +50,31 @@ */ function send(pRes, pData){ pRes.writeHead(OK, Header); - pRes.end( pData.toString() ); + pRes.end( JSON.stringify(pData) ); } /** * getting data on method and command * - * @param pMethod - * @param pCommand + * @param pParams {command, method, body, response} */ - function getData(pMethod, pCommand){ - var lResult; + function getData(pParams){ + var lResult, + lCmd = pParams.command, + lMethod = pParams.method; - switch(pMethod){ + if(lCmd[0] === '/'){ + lCmd = Util.removeStr(lCmd, '/'); + pParams.command = lCmd; + } + + switch(lMethod){ case 'GET': - lResult = onGET(pCommand); + lResult = onGET(pParams); break; case 'PUT': - lResult = onPUT(pCommand); + lResult = onPUT(pParams); break; } @@ -70,15 +84,18 @@ /** * process data on GET request * - * @param pCommand + * @param pParams {command, method, body, response} */ - function onGET(pCommand){ - var lResult; + function onGET(pParams){ + var lResult, + lCmd = pParams.command; - switch(pCommand){ + switch(lCmd){ case '': lResult = {info: 'Cloud Commander API v1'}; break; + case 'kill': + process.exit(); } return lResult; @@ -87,15 +104,35 @@ /** * process data on PUT request * - * @param pCommand + * @param pParams {command, method, body, response} */ - function onPUT(pCommand){ - var lResult; + function onPUT(pParams){ + var lResult, + lCmd = pParams.command, + lBody = pParams.body, + lRes = pParams.response; - switch(pCommand){ + switch(lCmd){ + case 'auth': + main.auth(lBody, function(pTocken){ + send(lRes, pTocken); + }); + lResult = false; + break; } return lResult; } + function getBody(pReq, pCallBack){ + var lBody = ''; + pReq.on('data', function(chunk) { + lBody += chunk.toString(); + }); + + pReq.on('end', function() { + Util.exec(pCallBack, lBody); + }); + } + })(); diff --git a/server.js b/server.js index 31c349c9..0ed31905 100644 --- a/server.js +++ b/server.js @@ -401,17 +401,9 @@ CloudServer._controller = function(pReq, pRes) console.log(DirPath); + /* читаем основные данные о файле */ - if( lQuery && lQuery.indexOf('code=') === 0){ - var lAuth = main.auth; - - if( Util.isFunction(lAuth) ) - lAuth(lQuery, function(){ - Fs.stat(DirPath, CloudServer._stated); - }); - console.log('***********'); - }else - Fs.stat(DirPath, CloudServer._stated); + Fs.stat(DirPath, CloudServer._stated); /* если установлено сжатие * меняем название html-файла и