Skip to content

Commit

Permalink
Merge pull request #201 from csKyou/master
Browse files Browse the repository at this point in the history
At the time of Google login, X-domain support is not done
  • Loading branch information
Dixon Siu authored Oct 9, 2018
2 parents 3895928 + 3a3db00 commit eb5d56d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
37 changes: 6 additions & 31 deletions box-resources/homeapp_google_auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 35 additions & 0 deletions src/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
});
Expand Down
10 changes: 8 additions & 2 deletions src/js/personium.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit eb5d56d

Please sign in to comment.