<commit_before>'use strict';

var onAuthenticatedCallbacks = {}

log("load githubAuth")


window.addEventListener('error', function(error) {
        console.error(error);
});

export function onAuthenticated(windowUuid, authInfo) {

	var state = authInfo.state
	var token = authInfo.access_token

	if (!state) { alert("authinfo: " + JSON.stringify(authInfo))}

	localStorage.GithubToken = token
	focalStorage.setItem("githubToken", localStorage.GithubToken).then(function() {
		var cb = onAuthenticatedCallbacks[state]
		if (cb) {cb(token)}
	})
}

export function logout(cb) {
	localStorage.GithubToken = null
	focalStorage.setItem("githubToken", null).then(cb)
}

function parseAuthInfoFromUrl(data) {
  var authInfo = JSON.parse(
    '{"' + data.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
    function(key, value) { return key === "" ? value : decodeURIComponent(value); });
  return authInfo;
}

function notifyMe(title, text, cb) {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification(title, {
      icon: 'https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png',
      body: text,
    });
    notification.onclick = db
  }
}

export function challengeForAuth(uuid, cb) {
	if (uuid && cb) {
		onAuthenticatedCallbacks[uuid] = cb
	}
	function popup(url) {
	    var width = 525,
	        height = 525,
	        screenX = window.screenX,
	        screenY = window.screenY,
	        outerWidth = window.outerWidth,
	        outerHeight = window.outerHeight;

	    var left = screenX + Math.max(outerWidth - width, 0) / 2;
	    var top = screenY + Math.max(outerHeight - height, 0) / 2;
	    
	    var features = [
	              "width=" + width,
	              "height=" + height,
	              "top=" + top,
	              "left=" + left,
	              "status=no",
	              "resizable=yes",
	              "toolbar=no",
	              "menubar=no",
	              "scrollbars=yes"];
	    popup = window.open(url, "oauth", features.join(","));
	    if (!popup) {
	    	notifyMe("Github Authenfication required", "click here to authenticate", function() {
				window.open(url, "oauth", features.join(","))
	    	})
	        // alert("failed to pop up auth window");
	    }
	    // popup.uuid = lively.net.CloudStorage.addPendingRequest(req);
	    popup.focus();
	}

    var appInfo = {
	        "clientId": "21b67bb82b7af444a7ef",
	        "redirectUri": "https://lively-kernel.org/lively4-auth/oauth/github.html"
	 };

	$.get("https://lively-kernel.org/lively4-auth/open_github_accesstoken?state="+uuid, null, function(data){
	    alert("challenge got a token, too: " + data)
	    var authInfo = parseAuthInfoFromUrl(data)
	    onAuthenticated(uuid, authInfo)
	})

    var url =
        "https://github.com/login/oauth/authorize/" +
        "?client_id=" + appInfo.clientId +
        "&response_type=token" +
       	"&scope=repo" +
        "&state=" + uuid +
        "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);
    popup(url);
}

// receive messages
navigator.serviceWorker.addEventListener("message", function(event) {
    if (event.data.name == 'githubAuthTokenRequired') {
    	console.log("goth auth token required")
    	var callbackId = event.data.callbackId
    	$('#uploadPic').dialog({
		         autoOpen: false,
		         modal: true,
		         draggable: true,
		         title: "Upload Picture",
		         open: function(type, data) {
		             $(this).parent().appendTo("form");
		         }
		     });
		 }
    	var foo = confirm('foobar')
    	challengeForAuth(Date.now(), function(token) {
    		messaging.postMessage({
	        	type: 'callback',
	    		callbackId: callbackId,
	    		args: [token]
			})
    	})
    } 
})
<commit_msg>SYNC  M draft/authgithub.js;<commit_after>'use strict';

var onAuthenticatedCallbacks = {}

log("load githubAuth")


window.addEventListener('error', function(error) {
        console.error(error);
});

export function onAuthenticated(windowUuid, authInfo) {

	var state = authInfo.state
	var token = authInfo.access_token

	if (!state) { alert("authinfo: " + JSON.stringify(authInfo))}

	localStorage.GithubToken = <PASSWORD>
	focalStorage.setItem("githubToken", localStorage.GithubToken).then(function() {
		var cb = onAuthenticatedCallbacks[state]
		if (cb) {cb(token)}
	})
}

export function logout(cb) {
	localStorage.GithubToken = null
	focalStorage.setItem("githubToken", null).then(cb)
}

function parseAuthInfoFromUrl(data) {
  var authInfo = JSON.parse(
    '{"' + data.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
    function(key, value) { return key === "" ? value : decodeURIComponent(value); });
  return authInfo;
}

function notifyMe(title, text, cb) {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification(title, {
      icon: 'https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png',
      body: text,
    });
    notification.onclick = db
  }
}

export function challengeForAuth(uuid, cb) {
	if (uuid && cb) {
		onAuthenticatedCallbacks[uuid] = cb
	}
	function popup(url) {
	    var width = 525,
	        height = 525,
	        screenX = window.screenX,
	        screenY = window.screenY,
	        outerWidth = window.outerWidth,
	        outerHeight = window.outerHeight;

	    var left = screenX + Math.max(outerWidth - width, 0) / 2;
	    var top = screenY + Math.max(outerHeight - height, 0) / 2;
	    
	    var features = [
	              "width=" + width,
	              "height=" + height,
	              "top=" + top,
	              "left=" + left,
	              "status=no",
	              "resizable=yes",
	              "toolbar=no",
	              "menubar=no",
	              "scrollbars=yes"];
	    popup = window.open(url, "oauth", features.join(","));
	    if (!popup) {
	    	notifyMe("Github Authenfication required", "click here to authenticate", function() {
				window.open(url, "oauth", features.join(","))
	    	})
	        // alert("failed to pop up auth window");
	    }
	    // popup.uuid = lively.net.CloudStorage.addPendingRequest(req);
	    popup.focus();
	}

    var appInfo = {
	        "clientId": "21b67bb82b7af444a7ef",
	        "redirectUri": "https://lively-kernel.org/lively4-auth/oauth/github.html"
	 };

	$.get("https://lively-kernel.org/lively4-auth/open_github_accesstoken?state="+uuid, null, function(data){
	    alert("challenge got a token, too: " + data)
	    var authInfo = parseAuthInfoFromUrl(data)
	    onAuthenticated(uuid, authInfo)
	})

    var url =
        "https://github.com/login/oauth/authorize/" +
        "?client_id=" + appInfo.clientId +
        "&response_type=token" +
       	"&scope=repo" +
        "&state=" + uuid +
        "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);
    popup(url);
}

// receive messages
navigator.serviceWorker.addEventListener("message", function(event) {
    if (event.data.name == 'githubAuthTokenRequired') {
    	console.log("goth auth token required")
    	var callbackId = event.data.callbackId
    	challengeForAuth(Date.now(), function(token) {
    		messaging.postMessage({
	        	type: 'callback',
	    		callbackId: callbackId,
	    		args: [token]
			})
    	})
    } 
})

{'fraction_non_alphanumeric': 0.14991785323110624, 'fraction_numerical': 0.006297918948521358, 'mean_word_length': 2.75, 'pattern_counts': {'":': 6, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '467229', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2313, 'n_words': 435}