2021-03-16 17:44:31 +01:00
|
|
|
|
import { getQsParam } from "./common";
|
|
|
|
|
|
2020-07-16 15:18:25 +02:00
|
|
|
|
require("./sso.scss");
|
|
|
|
|
|
2024-01-16 15:39:52 +01:00
|
|
|
|
window.addEventListener("load", () => {
|
2020-07-16 15:18:25 +02:00
|
|
|
|
const code = getQsParam("code");
|
|
|
|
|
const state = getQsParam("state");
|
2023-10-24 20:37:48 +02:00
|
|
|
|
const lastpass = getQsParam("lp");
|
2020-08-20 21:30:22 +02:00
|
|
|
|
|
2023-10-24 20:37:48 +02:00
|
|
|
|
if (lastpass === "1") {
|
2024-02-02 19:53:11 +01:00
|
|
|
|
initiateBrowserSsoIfDocumentReady(code, state, true);
|
2023-10-24 20:37:48 +02:00
|
|
|
|
} else if (state != null && state.includes(":clientId=browser")) {
|
2024-02-02 19:53:11 +01:00
|
|
|
|
initiateBrowserSsoIfDocumentReady(code, state, false);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
} else {
|
2020-08-20 21:30:22 +02:00
|
|
|
|
window.location.href = window.location.origin + "/#/sso?code=" + code + "&state=" + state;
|
2020-08-27 17:44:04 +02:00
|
|
|
|
// Match any characters between "_returnUri='" and the next "'"
|
|
|
|
|
const returnUri = extractFromRegex(state, "(?<=_returnUri=')(.*)(?=')");
|
|
|
|
|
if (returnUri) {
|
|
|
|
|
window.location.href = window.location.origin + `/#${returnUri}`;
|
2020-08-20 21:30:22 +02:00
|
|
|
|
} else {
|
2020-08-27 17:44:04 +02:00
|
|
|
|
window.location.href = window.location.origin + "/#/sso?code=" + code + "&state=" + state;
|
2020-08-20 21:30:22 +02:00
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
}
|
2020-07-16 15:18:25 +02:00
|
|
|
|
});
|
|
|
|
|
|
2024-02-02 19:53:11 +01:00
|
|
|
|
function initiateBrowserSsoIfDocumentReady(code: string, state: string, lastpass: boolean) {
|
2024-02-02 22:23:15 +01:00
|
|
|
|
const MAX_ATTEMPTS = 200;
|
|
|
|
|
const TIMEOUT_MS = 50;
|
|
|
|
|
let attempts = 0;
|
|
|
|
|
|
|
|
|
|
const pingInterval = setInterval(() => {
|
|
|
|
|
if (attempts >= MAX_ATTEMPTS) {
|
|
|
|
|
clearInterval(pingInterval);
|
|
|
|
|
throw new Error("Failed to initiate browser SSO");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attempts++;
|
|
|
|
|
window.postMessage({ command: "checkIfReadyForAuthResult" }, "*");
|
|
|
|
|
}, TIMEOUT_MS);
|
|
|
|
|
|
|
|
|
|
const handleWindowMessage = (event: MessageEvent) => {
|
|
|
|
|
if (event.source === window && event.data?.command === "readyToReceiveAuthResult") {
|
|
|
|
|
clearInterval(pingInterval);
|
|
|
|
|
window.removeEventListener("message", handleWindowMessage);
|
2024-02-02 19:53:11 +01:00
|
|
|
|
|
|
|
|
|
initiateBrowserSso(code, state, lastpass);
|
|
|
|
|
}
|
2024-02-02 22:23:15 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener("message", handleWindowMessage);
|
2024-02-02 19:53:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 20:37:48 +02:00
|
|
|
|
function initiateBrowserSso(code: string, state: string, lastpass: boolean) {
|
|
|
|
|
window.postMessage({ command: "authResult", code: code, state: state, lastpass: lastpass }, "*");
|
2020-12-14 19:27:32 +01:00
|
|
|
|
const handOffMessage = ("; " + document.cookie)
|
|
|
|
|
.split("; ssoHandOffMessage=")
|
|
|
|
|
.pop()
|
|
|
|
|
.split(";")
|
|
|
|
|
.shift();
|
|
|
|
|
document.cookie = "ssoHandOffMessage=;SameSite=strict;max-age=0";
|
2021-11-11 04:35:35 +01:00
|
|
|
|
const content = document.getElementById("content");
|
2021-11-09 18:15:58 +01:00
|
|
|
|
content.innerHTML = "";
|
2021-11-11 04:35:35 +01:00
|
|
|
|
const p = document.createElement("p");
|
2021-11-09 18:15:58 +01:00
|
|
|
|
p.innerText = handOffMessage;
|
|
|
|
|
content.appendChild(p);
|
2020-08-20 21:30:22 +02:00
|
|
|
|
}
|
2020-08-27 17:44:04 +02:00
|
|
|
|
|
|
|
|
|
function extractFromRegex(s: string, regexString: string) {
|
|
|
|
|
const regex = new RegExp(regexString);
|
|
|
|
|
const results = regex.exec(s);
|
|
|
|
|
|
|
|
|
|
if (!results) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results[0];
|
|
|
|
|
}
|