diff --git a/lib/server/github.js b/lib/server/github.js deleted file mode 100644 index 993e8707..00000000 --- a/lib/server/github.js +++ /dev/null @@ -1,85 +0,0 @@ -(function() { - 'use strict'; - - var DIR = './', - DIR_LIB = DIR + '../', - DIR_JSON = DIR_LIB + '../json/', - - https = require('https'), - qs = require('querystring'), - - pipe = require('pipe-io'), - - Modules = require(DIR_JSON + 'modules'), - Util = require(DIR_LIB + 'util'), - - GithubAuth = { - host: 'github.com', - port: 443, - path: '/login/oauth/access_token', - method: 'POST' - }; - - /** - * function do authentication - * @param code - * @param callback - */ - - exports.auth = function(code, callback){ - code = code.replace('code=', ''); - - Util.log(code); - - authenticate(code, function(error, token) { - var result = { 'token': token }; - Util.log(error || result); - - Util.exec(callback, error, result); - }); - }; - - function authenticate(code, callback) { - var req, - storage = Util.findObjByNameInArr(Modules, 'storage'), - github = Util.findObjByNameInArr(storage, 'GitHub'), - - id = github && github.key, - secret = github && github.secret, - env = process.env, - - clientId = env.github_key || id, - clientSecret = env.github_secret || secret, - - data = qs.stringify({ - client_id : clientId, - client_secret : clientSecret, - code : code - }); - - Util.log(clientId, clientSecret, data); - - GithubAuth.headers = { 'content-length': data.length }; - - req = https.request(GithubAuth, function(res) { - pipe.getBody(res, function(error, body) { - var parsed, token; - - if (!error) { - parsed = qs.parse(body); - - if (parsed) - token = parsed.access_token; - } - - Util.exec(callback, error, token); - }); - }); - - req.end(data); - - req.on('error', function(e) { - Util.exec(callback, e.message); - }); - } -})(); diff --git a/lib/server/rest.js b/lib/server/rest.js index f013fc9a..e31093e7 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -11,6 +11,7 @@ var DIR = './', DIR_LIB = DIR + '../', + DIR_JSON = DIR_LIB + '../json/', fs = require('fs'), path = require('path'), @@ -21,7 +22,7 @@ markdown = require(DIR + 'rest/markdown'), - github = require(DIR + 'github'), + github = require(DIR + 'faust'), packer = require('jag'), tryRequire = require('./tryRequire'), @@ -32,6 +33,8 @@ pipe = tryRequire('pipe-io', tryOptions), ponse = tryRequire('ponse', tryOptions), + Modules = require(DIR_JSON + 'modules'), + isWin32 = process.platform === 'win32', Fs = {}; @@ -256,6 +259,19 @@ } } + function auth(code, callback) { + var storage = Util.findObjByNameInArr(Modules, 'storage'), + gh = Util.findObjByNameInArr(storage, 'GitHub'), + env = process.env, + + key = env.github_key || gh.key, + secret = env.github_secret || gh.secret, + + ghAuth = github(key, secret); + + ghAuth(code, callback); + } + /** * process data on PUT request * @@ -273,7 +289,7 @@ switch(cmd) { case 'auth': - github.auth(body, function(error, token) { + auth(body, function(error, token) { callback(error, Util.json.stringify({ data: token })); diff --git a/package.json b/package.json index 94d47139..67a37712 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "console-io": "~1.11.0", "execon": "~1.0.1", "express": "~4.10.0", + "faust": "~1.0.0", "files-io": "~1.2.0", "flop": "~1.0.25", "http-auth": "~2.2.3",