Support GitHub OAuth user authentication

Issue #529.
This commit is contained in:
Thaddee Tyl 2016-05-21 20:43:14 +02:00
parent cb95270abd
commit ccaacfc408
3 changed files with 68 additions and 1 deletions

63
lib/github-auth.js Normal file
View File

@ -0,0 +1,63 @@
// TODO:
// - add route in server.js
// - document user authorization in try.html
var querystring = require('querystring');
var request = require('request');
var serverSecrets;
try {
// Everything that cannot be checked in but is useful server-side
// is stored in this JSON data.
serverSecrets = require('../secret.json');
} catch(e) {}
function setRoutes(server) {
server.route(/^\/github-auth$/, function(data, match, end, ask) {
if (!(serverSecrets && serverSecrets.gh_client_id)) {
return end('This server is missing GitHub client secrets');
}
var query = querystring.stringify({
client_id: serverSecrets.gh_client_id,
redirect_uri: 'https://img.shields.io/github-auth/done',
});
ask.res.statusCode = 302; // Found.
ask.res.setHeader('Location', 'https://github.com/login/oauth/authorize?' + query);
end('');
});
server.route(/^\/github-auth\/done$/, function(data, match, end, ask) {
if (!(serverSecrets && serverSecrets.gh_client_id && serverSecrets.gh_client_secret)) {
return end('This server is missing GitHub client secrets');
}
if (!data.code) {
return end('GitHub OAuth authentication failed to provide a code');
}
var options = {
url: 'https://github.com/login/oauth/access_token',
headers: {
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'User-Agent': 'Shields.io',
},
form: querystring.stringify({
client_id: serverSecrets.gh_client_id,
client_secret: serverSecrets.gh_client_secret,
code: data.code,
}),
method: 'POST',
};
console.log(JSON.stringify(options));
request.post(options, function(err, res, body) {
if (err != null) { return end('The connection to GitHub failed'); }
try {
var content = querystring.parse(body);
} catch(e) { return end('The GitHub OAuth token could not be parsed'); }
var token = content.access_token;
if (!token) {
return end('The GitHub OAuth process did not return a user token');
}
console.log('GitHub OAuth: ' + token);
end('Done!');
});
});
};
exports.setRoutes = setRoutes;

View File

@ -23,7 +23,7 @@
"es6-promise": "~2.1.0",
"request": "~2.55.0",
"redis": "~1.0.0",
"camp": "~16.0.0",
"camp": "~16.2.0",
"semver": "~4.3.3",
"bower": "~1.4.1",
"promise": "~7.0.0",

View File

@ -18,6 +18,7 @@ var LruCache = require('./lru-cache.js');
var badge = require('./badge.js');
var svg2img = require('./svg-to-img.js');
var loadLogos = require('./load-logos.js');
var githubAuth = require('./lib/github-auth.js');
var querystring = require('querystring');
var serverSecrets;
try {
@ -5439,6 +5440,9 @@ var githubHeaders = {
'User-Agent': 'Shields.io',
'Accept': 'application/vnd.github.v3+json'
};
if (serverSecrets && serverSecrets.gh_client_id) {
githubAuth.setRoutes(camp);
}
// Personal tokens allow access to GitHub private repositories.
// You can manage your personal GitHub token at