Skip to content

Commit

Permalink
fix for sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
increpare committed May 18, 2018
1 parent 2fffcad commit 9f1f847
Show file tree
Hide file tree
Showing 3 changed files with 4,400 additions and 27 deletions.
60 changes: 60 additions & 0 deletions auth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Flickgame Login</title> </head> <body> <pre id="pre"></pre> <script>

(function() {

// An external helper that can exchange a code for an access token.
//
// It fetches https://github.com/login/oauth/access_token, passing:
// * code and state, which the caller has to provide as query parameters.
// * client_id, same as OAUTH_CLIENT_ID above.
// * client_secret, which corresponds to client_id, but is secret on the server.
//
// It returns an access token as JSON, something like:
// {"access_token": "a440b61aa137bb25bb739b697ef5c96a76881107"}
//
// The server has CORS set up so that flickgame.net can access it.
OAUTH_HELPER_URL = "https://ded.increpare.com/cgi-bin/access_token_pp.py";

var pre = document.getElementById("pre");

var url = new URL(window.location);
var code = url.searchParams.get("code");
var state = url.searchParams.get("state");

if (typeof code !== "string" || typeof state !== "string") {
pre.innerText = "Nothing to see here.";
return;
}

pre.innerText = "Just a moment…";

var xhr = new XMLHttpRequest();
xhr.open("GET", OAUTH_HELPER_URL + "?code=" + code + "&state=" + state);
xhr.onreadystatechange = function() {
if (xhr.readyState !== 4) {
return;
}

if (xhr.status === 403) {
pre.innerText = result.message;
return;
} else if ((xhr.status !== 200) && (xhr.status !== 201)) {
pre.innerText = "HTTP Error " + xhr.status + " - " + xhr.statusText;
return;
}

var result = JSON.parse(xhr.responseText);
console.log(result);
if (typeof result.error === "string") {
pre.innerText = "Oops, got an error: " + JSON.stringify(result, null, 4);
return;
}

window.localStorage.setItem("oauth_access_token", result.access_token);
pre.innerText = "OK! You’re all set up. You can close this window and share your game :)";
}
xhr.send();

})();

</script> </body> </html>
76 changes: 66 additions & 10 deletions pinball.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,72 @@ function makeConnections(){
}
}



OAUTH_CLIENT_ID = "cef5bd30263de4332818";


function getAuthURL(){
var randomState = window.btoa(Array.prototype.map.call(
window.crypto.getRandomValues(new Uint8Array(24)),
function(x) { return String.fromCharCode(x); }).join(""));

var authUrl = "https://github.com/login/oauth/authorize"
+ "?client_id=" + OAUTH_CLIENT_ID
+ "&scope=gist"
+ "&state=" + randomState
+ "&allow_signup=true";

return authUrl;
}

function printUnauthorized(){

var authUrl = getAuthURL();
var toPrint = "<a target=\"_blank\" href=\"" + authUrl + "\">Log in with Github to share</a><br>";

var shareLink = document.getElementById("shareLink");
shareLink.innerHTML = toPrint;
shareLinkInner=null;
}



function githubLogOut(){
window.localStorage.removeItem("oauth_access_token");
var authUrl = getAuthURL();
var toPrint = "Logged out of Github.<br>";
var shareLink = document.getElementById("shareLink");
shareLink.innerHTML = toPrint;
shareLinkInner=null;
}


function shareClick() {
var title = gameTitle;
var oauthAccessToken = window.localStorage.getItem("oauth_access_token");
if (typeof oauthAccessToken !== "string") {
// Generates 32 letters of random data, like "liVsr/e+luK9tC02fUob75zEKaL4VpQn".
printUnauthorized();
return;
}


var str = stateToString();

var gistToCreate = {
"description" : title,
"description" : "flickgame",
"public" : true,
"files": {
"readme.txt" : {
"content": "A game made with www.flipcode.org"
"content": "A game made with www.flickgame.org. You can import game.txt there to play the game. Uh, too lazy to describe - HMU at analytic@gmail.com if you want to know how (basically just use the gist ID in the url like other flickgames do...) "
},
"game.txt" : {
"content": str
}
}
};

var githubURL = 'https://api.github.com/gists';
var githubURL = 'https://api.github.com/gists?access_token=' + oauthAccessToken;
var githubHTTPClient = new XMLHttpRequest();
githubHTTPClient.open('POST', githubURL);
githubHTTPClient.onreadystatechange = function() {
Expand All @@ -338,6 +386,16 @@ function shareClick() {
errorCount++;
alert(result.message);
} else if (githubHTTPClient.status!==200&&githubHTTPClient.status!==201) {

if (githubHTTPClient.statusText==="Unauthorized"){
alert("Authorization check failed. You have to log back into GitHub (or give it permission again or something).");
window.localStorage.removeItem("oauth_access_token");
} else {
alert("HTTP Error "+ githubHTTPClient.status + ' - ' + githubHTTPClient. statusText);
}

printUnauthorized();
} else if (githubHTTPClient.status!==200&&githubHTTPClient.status!==201) {
errorCount++;
alert("HTTP Error "+ githubHTTPClient.status + ' - ' + githubHTTPClient.statusText);
} else {
Expand All @@ -350,7 +408,8 @@ function shareClick() {
var sourceCodeLink = "link to source code:<br><a href=\""+editurl+"\">"+editurl+"</a>";

var shareLink = document.getElementById("shareLink");
shareLink.innerHTML = "<a target=\"_blank\" href=\""+url+"\">&#8627;"+id+"</a><br>";
shareLink.innerHTML = "<a target=\"_blank\" href=\""+url+"\">&#8627;"+id+"</a><br>"+
'(<a onclick="githubLogOut();" href="javascript:void(0);">log out of GitHub</a>)<br>';
shareLinkInner = shareLink.childNodes[0];

if (errorCount>0) {
Expand All @@ -359,19 +418,16 @@ function shareClick() {

}

if(PLAYER!==true){
window.history.pushState({}, "plingpling game maker", "index.html?p="+id);
}


}
}
githubHTTPClient.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var stringifiedGist = JSON.stringify(gistToCreate);
githubHTTPClient.send(stringifiedGist);
lastDownTarget=masterCanvas;
lastDownTarget=canvas;
}


function RLE_encode(input) {
var encoding = [];
var prev, count, i;
Expand Down
Loading

0 comments on commit 9f1f847

Please sign in to comment.