From 014753a736688732dac8c55de6232e1c642c5ae1 Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Sun, 26 Jun 2016 16:41:50 +0200 Subject: [PATCH] GitHub auth: better messages --- lib/github-auth.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/github-auth.js b/lib/github-auth.js index 0096ede..e766475 100644 --- a/lib/github-auth.js +++ b/lib/github-auth.js @@ -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('

Shields.io has received your app-specific GitHub user token. ' + + 'You can revoke it by going to ' + + 'GitHub.

' + + '

Until you do, you have now increased the rate limit for GitHub ' + + 'requests going through Shields.io. GitHub-related badges are ' + + 'therefore more robust.

' + + '

Thanks for contributing to a smoother experience for ' + + 'everyone!

' + + '

Back to the website

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