diff --git a/box-resources/homeapp_google_auth.html b/box-resources/homeapp_google_auth.html index e209a6a..6c87c00 100644 --- a/box-resources/homeapp_google_auth.html +++ b/box-resources/homeapp_google_auth.html @@ -18,45 +18,20 @@ var token = ""; var match = location.hash.match(/id_token=(.*?)(&|$)/); if (match) { - token = decodeURIComponent(match[1]); - console.log(match); - console.log("token : " + token); + token = match[1]; } var target = ""; var matchState = location.hash.match(/state=(.*?)(&|$)/); if (matchState) { target = decodeURIComponent(matchState[1]); - console.log("target : " + target); } if (token) { - var cellUrl = sessionStorage.getItem("targetCellUrl"); - $.ajax({ - type: "POST", - url: cellUrl + "__token", - data: { - grant_type: 'urn:x-personium:oidc:google', - id_token: token - }, - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded' - } - }).done(function (data) { - data.cellUrl = cellUrl; - var i = cellUrl.indexOf("/"); // first slash - i = cellUrl.indexOf("/", i + 2); // second slash - data.baseUrl = cellUrl.substring(0, i + 1); - data.profile = JSON.parse(sessionStorage.getItem("myProfile")); - //data.userName = username; - data.userName = "googleAccount"; - data.logoutUrl = target; - sessionStorage.setItem("sessionData", JSON.stringify(data)); - location.href = target; - }).fail(function (data) { - console.log(data); - logout(target); - }); + if (target.indexOf("?") < 0) { + location.href = target + "?id_token=" + token; + } else { + location.href = target + "&id_token=" + token; + } } else { logout(target); } diff --git a/src/js/login.js b/src/js/login.js index 9ac0c35..f62cd9c 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -11,6 +11,40 @@ lg.loadLogin = function () { }); } +lg.googleLogin = function (gToken) { + var cellUrl = sessionStorage.googleLoginCell; + $.ajax({ + type: "POST", + url: cellUrl + "__token", + data: { + grant_type: 'urn:x-personium:oidc:google', + id_token: gToken + }, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + } + }).done(function (data) { + data.cellUrl = cellUrl; + var i = cellUrl.indexOf("/"); // first slash + i = cellUrl.indexOf("/", i + 2); // second slash + data.baseUrl = cellUrl.substring(0, i + 1); + data.profile = JSON.parse(sessionStorage.getItem("myProfile")); + //data.userName = username; + data.userName = "googleAccount"; + var pos = location.href.indexOf("id_token"); + if (pos >= 0) { + // Exclude conjunctions that preceded id_token + data.logoutUrl = location.href.substring(0, pos - 1); + } + + sessionStorage.setItem("sessionData", JSON.stringify(data)); + location.href = data.logoutUrl; + }).fail(function (data) { + console.log(data); + }); +} + lg.initTarget = function () { ut.loadScript(function () { var mode = "local"; @@ -57,6 +91,7 @@ lg.initTarget = function () { }); $("#gLogin").on("click", function (e) { var homeUrl = location.href; + sessionStorage.googleLoginCell = lg.rootUrl; var url = "https://accounts.google.com/o/oauth2/v2/auth?client_id=102363313215-408im4hc7mtsgrda4ratkro2thn58bcd.apps.googleusercontent.com&response_type=code+id_token&scope=openid%20email%20profile&redirect_uri=https%3a%2f%2fdemo%2epersonium%2eio%2fapp%2dcc%2dhome%2f__%2fhtml%2fhomeapp_google_auth%2ehtml&display=popup&nonce=personium&state=" + homeUrl; window.location.href = url; }); diff --git a/src/js/personium.js b/src/js/personium.js index e51fe2a..3e2e7fd 100644 --- a/src/js/personium.js +++ b/src/js/personium.js @@ -477,8 +477,14 @@ personium.recursiveDeleteBoxAPI = function (cellUrl, token, boxName) { /* Transition method */ personium.init = function () { if (!cm.user) { - cm.user = {}; - lg.loadLogin(); + var match = location.search.match(/id_token=(.*?)(&|$)/); + if (match) { + var gToken = decodeURIComponent(match[1]); + lg.googleLogin(gToken); + } else { + cm.user = {}; + lg.loadLogin(); + } } else { ha.loadMain(); }