Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code.iilab.org #23

Merged
merged 2 commits into from May 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions config.json
@@ -1,8 +1,8 @@
{
"oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID",
"oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET",
"oauth_host": "github.com",
"oauth_client_id": "",
"oauth_client_secret": "",
"oauth_host": "https://code.iilab.org",
"oauth_port": 443,
"oauth_path": "/login/oauth/access_token",
"oauth_path": "/oauth/token",
"oauth_method": "POST"
}
}
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"oauth"
],
"scripts": {
"start": "node server.js"
"start": "node server.js"
},
"repository": {
"type": "git",
Expand All @@ -16,7 +16,8 @@
"author": "Michael Aufreiter",
"contributors": [],
"dependencies": {
"express": "~3.16.0"
"express": "~3.16.0",
"request": "^2.72.0"
},
"engines": {
"node": ">= 0.6.x"
Expand Down
47 changes: 23 additions & 24 deletions server.js
@@ -1,8 +1,6 @@
var url = require('url'),
http = require('http'),
https = require('https'),
fs = require('fs'),
qs = require('querystring'),
request = require('request'),
express = require('express'),
app = express();

Expand All @@ -21,39 +19,37 @@ function loadConfig() {
var config = loadConfig();

function authenticate(code, cb) {
var data = qs.stringify({
var data = {
client_id: config.oauth_client_id,
client_secret: config.oauth_client_secret,
code: code
});
code: code,
grant_type: 'authorization_code',
redirect_uri: 'http://lieu.io/prose/'
};

var reqOptions = {
host: config.oauth_host,
port: config.oauth_port,
path: config.oauth_path,
method: config.oauth_method,
headers: { 'content-length': data.length }
url: config.oauth_host + config.oauth_path,
qs: data
};

var body = "";
var req = https.request(reqOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) { body += chunk; });
res.on('end', function() {
cb(null, qs.parse(body).access_token);
});
});

req.write(data);
req.end();
req.on('error', function(e) { cb(e.message); });
request.post(reqOptions, function (error, res, body) {
if (error) { cb(e.message); }
try {
body = JSON.parse(body);
}
catch (e) {
cb(e.message);
}
cb(null, body.access_token);
});
}


// Convenience for allowing CORS on routes - GET only
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
Expand All @@ -64,6 +60,9 @@ app.get('/authenticate/:code', function(req, res) {
authenticate(req.params.code, function(err, token) {
var result = err || !token ? {"error": "bad_code"} : { "token": token };
console.log(result);
if (err) {
console.log(err);
}
res.json(result);
});
});
Expand Down