GitHub auth: better messages

This commit is contained in:
Thaddee Tyl 2016-06-26 16:41:50 +02:00
parent f6ee6745cd
commit 014753a736

View File

@ -22,7 +22,7 @@ autosave(githubUserTokensFile, {data:[]}).then(function(f) {
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');
return end('This server is missing GitHub client secrets.');
}
var query = querystring.stringify({
client_id: serverSecrets.gh_client_id,
@ -35,10 +35,10 @@ function setRoutes(server) {
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');
return end('This server is missing GitHub client secrets.');
}
if (!data.code) {
return end('GitHub OAuth authentication failed to provide a code');
return end('GitHub OAuth authentication failed to provide a code.');
}
var options = {
url: 'https://github.com/login/oauth/access_token',
@ -54,17 +54,26 @@ function setRoutes(server) {
method: 'POST',
};
request(options, function(err, res, body) {
if (err != null) { return end('The connection to GitHub failed'); }
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'); }
} 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');
return end('The GitHub OAuth process did not return a user token.');
}
console.log('GitHub OAuth: ' + token);
end('Done!');
ask.res.setHeader('Content-Type', 'text/html');
end('<p>Shields.io has received your app-specific GitHub user token. ' +
'You can revoke it by going to ' +
'<a href="https://github.com/settings/applications">GitHub</a>.</p>' +
'<p>Until you do, you have now increased the rate limit for GitHub ' +
'requests going through Shields.io. GitHub-related badges are ' +
'therefore more robust.</p>' +
'<p>Thanks for contributing to a smoother experience for ' +
'everyone!</p>' +
'<p><a href="/">Back to the website</a></p>');
sendTokenToAllServers(token)
.catch(function(e) {
@ -77,7 +86,7 @@ function setRoutes(server) {
console.log('GitHub add token called with', JSON.stringify(data));
if (data.shieldsSecret !== serverSecrets.shieldsSecret) {
// An unknown entity tries to connect. Let the connection linger for a minute.
return setTimeout(function() { end('Invalid secret'); }, 60000);
return setTimeout(function() { end('Invalid secret.'); }, 60000);
}
addGithubToken(data.token);
end('Thanks!');
@ -204,6 +213,7 @@ function githubRequest(request, url, query, cb) {
var githubToken = getReqRemainingToken();
if (githubToken != null) {
// Typically, GitHub user tokens grants us 12500 req/hour.
headers['Authorization'] = 'token ' + githubToken;
} else if (serverSecrets && serverSecrets.gh_client_id) {
// Using our OAuth App secret grants us 5000 req/hour
@ -222,7 +232,6 @@ function githubRequest(request, url, query, cb) {
} else {
var remaining = +res.headers['x-ratelimit-remaining'];
var reset = +res.headers['x-ratelimit-reset'];
console.log('GitHub auth: token', githubToken, 'at rate limit', remaining);
setReqRemaining(githubToken, remaining, reset);
}
}