feature(github) github -> faust

This commit is contained in:
coderaiser 2014-12-08 02:48:35 -05:00
parent ef90c67ac2
commit 8053b81db7
3 changed files with 19 additions and 87 deletions

View file

@ -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);
});
}
})();

View file

@ -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
}));

View file

@ -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",