1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/apps/web/src/connectors/sso.ts
Cesar Gonzalez 25711afaf6
[PM-5976] Safari Browser SSO Initialization Race Condition Attempted Fix 3 (#7800)
* [PM-5976] Safari Browser SSO Initialization Race Condition Attempted Fix 3

* [PM-5976] Safari Browser SSO Initialization Race Condition Attempted Fix 3

* [PM-5976] Removing usage of pinging system and keeping reworked top-level registration of window message listener events

* [PM-5976] Pulling the implementation of the static content script delcaration for the content-message-handler file to the top of the list of content_scripts

* [PM-5976] Pulling the implementation of the static content script delcaration for the content-message-handler file to the top of the list of content_scripts

* [PM-5976] Removing the useCapture value within the window message event listener
2024-02-05 15:23:17 +00:00

51 lines
1.6 KiB
TypeScript

import { getQsParam } from "./common";
require("./sso.scss");
window.addEventListener("load", () => {
const code = getQsParam("code");
const state = getQsParam("state");
const lastpass = getQsParam("lp");
if (lastpass === "1") {
initiateBrowserSso(code, state, true);
} else if (state != null && state.includes(":clientId=browser")) {
initiateBrowserSso(code, state, false);
} else {
window.location.href = window.location.origin + "/#/sso?code=" + code + "&state=" + state;
// Match any characters between "_returnUri='" and the next "'"
const returnUri = extractFromRegex(state, "(?<=_returnUri=')(.*)(?=')");
if (returnUri) {
window.location.href = window.location.origin + `/#${returnUri}`;
} else {
window.location.href = window.location.origin + "/#/sso?code=" + code + "&state=" + state;
}
}
});
function initiateBrowserSso(code: string, state: string, lastpass: boolean) {
window.postMessage({ command: "authResult", code: code, state: state, lastpass: lastpass }, "*");
const handOffMessage = ("; " + document.cookie)
.split("; ssoHandOffMessage=")
.pop()
.split(";")
.shift();
document.cookie = "ssoHandOffMessage=;SameSite=strict;max-age=0";
const content = document.getElementById("content");
content.innerHTML = "";
const p = document.createElement("p");
p.innerText = handOffMessage;
content.appendChild(p);
}
function extractFromRegex(s: string, regexString: string) {
const regex = new RegExp(regexString);
const results = regex.exec(s);
if (!results) {
return null;
}
return results[0];
}