Skip to content

Commit 24ef7e8

Browse files
authored
Merge pull request #34 from LearnTeachCode/issue15
Close #15, send GitHub access token via headers
2 parents 4c0f63a + e890598 commit 24ef7e8

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

public/local.js

+40-11
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,29 @@ var currentGistView = document.getElementById('currentgist');
5454
GITHUB AUTHENTICATION
5555
---------------------------------------------------- */
5656

57-
// If GitHub access_token is available as a parameter, log in!
58-
// TODO: pass the token as a header instead? can client access it that way?
59-
if (getAllUrlParams().access_token) {
60-
console.log('*********** AUTHENTICATED!!! **********');
61-
console.log('access_token from URL params: ' + getAllUrlParams().access_token);
57+
// If GitHub tempcode is available as a parameter, get access_token from server and log in!
58+
if (getAllUrlParams().tempcode) {
59+
60+
let tempCode = getAllUrlParams().tempcode;
61+
62+
// Remove parameter from URL, updating this entry in the client's browser history
63+
history.replaceState(null, '', '/');
6264

6365
// TODO: show loading animation while waiting???
64-
6566
// TODO: refactor getAllUrlParams(), don't need it, just need ONE param!
66-
67-
// For now, save the access token as a global variable (I'm sure this is SUPER wrong though!)
68-
currentAccessToken = getAllUrlParams().access_token;
6967

70-
getJSON('https://api.github.com/user?access_token=' + currentAccessToken)
71-
.then(loginUser).catch(handleError);
68+
// Send tempCode to server in exchange for GitHub access token sent via headers
69+
getTokenFromServer(tempCode)
70+
.then(function(access_token){
71+
72+
// Save the access token as a global variable for now
73+
currentAccessToken = access_token;
74+
75+
// Authenticate with GitHub!
76+
getJSON('https://api.github.com/user?access_token=' + currentAccessToken)
77+
.then(loginUser).catch(handleError);
78+
79+
}, handleError).catch(handleError);
7280

7381
// Otherwise, if user has not yet started the login process,
7482
} else {
@@ -601,6 +609,27 @@ function get(url) {
601609
});
602610
}
603611

612+
function getTokenFromServer(tempCode) {
613+
return new Promise(function(succeed, fail) {
614+
var req = new XMLHttpRequest();
615+
req.open("GET", '/github-token', true);
616+
617+
// Set header:
618+
req.setRequestHeader('GitHub-Temp-Code', tempCode);
619+
620+
req.addEventListener("load", function() {
621+
if (req.status < 400)
622+
succeed(req.getResponseHeader('GitHub-Token'));
623+
else
624+
fail(new Error("Request failed: " + req.statusText));
625+
});
626+
req.addEventListener("error", function() {
627+
fail(new Error("Network error"));
628+
});
629+
req.send(null);
630+
});
631+
}
632+
604633
// Returns a promise for a POST request, similar to get() above
605634
function postWithGitHubToken(url, postDataObject) {
606635
return new Promise(function(succeed, fail) {

server.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ var port = process.env.PORT || 8000; // Set the default port number to 8000, or
1111
// Use Express to serve everything in the "public" folder as static files
1212
app.use(express.static('public'));
1313

14+
// Save table of temp codes and access tokens, for sending access tokens to the corresponding clients via headers
15+
let clientTokens = {};
16+
1417
// Pass GITHUB_CLIENT_ID to client when requested (using AJAX for now)
1518
// TODO (later): mess around with templating engines and Express .render()?
1619
app.get('/github-client', function (req, res) {
@@ -48,8 +51,13 @@ function authenticateUser (req, res) {
4851

4952
// TODO (later): check the scopes, because users can authorize less than what my app requested!
5053

51-
// Redirect to home page again but now with the access token!
52-
res.redirect('/?access_token=' + JSON.parse(githubResponseBody).access_token);
54+
// Save received access token to clientTokens to keep it associated with this client
55+
clientTokens[req.query.code] = JSON.parse(githubResponseBody).access_token;
56+
57+
// Redirect to home page again, with the temp code as a URL param
58+
// TODO (later): can I use server-side rendering to accomplish this also???
59+
res.redirect('/?tempcode=' + req.query.code);
60+
5361
});
5462
});
5563

@@ -58,6 +66,23 @@ function authenticateUser (req, res) {
5866

5967
}
6068

69+
// Pass GitHub access token to corresponding client, if it matches client's temp code
70+
app.get('/github-token', function (req, res) {
71+
72+
let tempCode = req.header('GitHub-Temp-Code');
73+
74+
console.log('Request received for /github-token route for temp code: ' + tempCode);
75+
76+
if ( clientTokens.hasOwnProperty(tempCode) ) {
77+
console.log('\t Temp code MATCHES! Sending access token in response header!');
78+
res.header('GitHub-Token', clientTokens[tempCode]);
79+
}
80+
res.end(); // Double check: can I use res.end() with no body?
81+
82+
console.log("\nclientTokens:\n");
83+
console.log(clientTokens);
84+
});
85+
6186
// Activate the server and listen on our specified port number
6287
server.listen(port, function() {
6388
// Display this message in the server console once the server is active

0 commit comments

Comments
 (0)