Another attempt at better supporting auth proxies

pull/1098/head
Zef Hemel 2024-09-25 17:06:43 +02:00
parent cb88eae885
commit 4eae0c975b
2 changed files with 34 additions and 2 deletions

View File

@ -1,6 +1,9 @@
import type { SpacePrimitives } from "./space_primitives.ts"; import type { SpacePrimitives } from "./space_primitives.ts";
import type { FileMeta } from "../../plug-api/types.ts"; import type { FileMeta } from "../../plug-api/types.ts";
import { flushCachesAndUnregisterServiceWorker } from "../sw_util.ts"; import {
flushCachesAndUnregisterServiceWorker,
unregisterServiceWorkers,
} from "../sw_util.ts";
import { encodePageURI } from "@silverbulletmd/silverbullet/lib/page_ref"; import { encodePageURI } from "@silverbulletmd/silverbullet/lib/page_ref";
const defaultFetchTimeout = 30000; // 30 seconds const defaultFetchTimeout = 30000; // 30 seconds
@ -41,13 +44,29 @@ export class HttpSpacePrimitives implements SpacePrimitives {
} }
const redirectHeader = result.headers.get("location"); const redirectHeader = result.headers.get("location");
if (result.type === "opaqueredirect" && !redirectHeader) {
// This is a scenario where the server sent a redirect, but this redirect is not visible to the client, likely due to CORS
// The best we can do is to reload the page and hope that the server will redirect us to the correct location
alert(
"You are not authenticated, reloading to reauthenticate",
);
console.log("Unregistering service workers", redirectHeader);
await unregisterServiceWorkers();
location.reload();
// Let's throw to avoid any further processing
throw Error("Not authenticated");
}
// console.log("Got response", result.status, result.statusText, result.url); // console.log("Got response", result.status, result.statusText, result.url);
// Attempting to handle various authentication proxies // Attempting to handle various authentication proxies
if (result.status >= 300 && result.status < 400) { if (result.status >= 300 && result.status < 400) {
if (redirectHeader) { if (redirectHeader) {
// Got a redirect // Got a redirect
alert("Received a redirect, redirecting to URL: " + redirectHeader); alert(
"Received an authentication redirect, redirecting to URL: " +
redirectHeader,
);
location.href = redirectHeader; location.href = redirectHeader;
throw new Error("Redirected"); throw new Error("Redirected");
} else { } else {

View File

@ -1,3 +1,16 @@
export async function unregisterServiceWorkers() {
if (navigator.serviceWorker) {
const registrations = await navigator.serviceWorker
.getRegistrations();
for (const registration of registrations) {
await registration.unregister();
console.log("Service worker unregistered");
}
} else {
console.error("No service worker found to unregister");
}
}
export function flushCachesAndUnregisterServiceWorker() { export function flushCachesAndUnregisterServiceWorker() {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
if (!navigator.serviceWorker) { if (!navigator.serviceWorker) {