Skip to content

Commit

Permalink
Attempt to fix auth proxies by making redirects manual #1028
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Sep 9, 2024
1 parent 409b0df commit d6fb5e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
41 changes: 27 additions & 14 deletions common/spaces/http_space_primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,46 @@ export class HttpSpacePrimitives implements SpacePrimitives {

try {
options.signal = AbortSignal.timeout(fetchTimeout);
options.redirect = "manual";
const result = await fetch(url, options);
if (result.status === 503) {
throw new Error("Offline");
}
const redirectHeader = result.headers.get("location");

// console.log("Got response", result.status, result.statusText, result.url);

// Attempting to handle various authentication proxies
if (result.redirected) {
if (result.status === 401 || result.status === 403) {
if (result.status >= 300 && result.status < 400) {
if (redirectHeader) {
// Got a redirect
alert("Received a redirect, redirecting to URL: " + redirectHeader);
location.href = redirectHeader;
throw new Error("Redirected");
} else {
console.error("Got a redirect status but no location header", result);
}
}
// Check for unauthorized status
if (result.status === 401 || result.status === 403) {
// If it came with a redirect header, we'll redirect to that URL
if (redirectHeader) {
console.log(
"Received unauthorized status and got a redirect via the API so will redirect to URL",
result.url,
);
alert("You are not authenticated, redirecting to: " + result.url);
location.href = result.url;
alert("You are not authenticated, redirecting to: " + redirectHeader);
location.href = redirectHeader;
throw new Error("Not authenticated");
} else {
alert("Received a redirect, redirecting to URL: " + result.url);
location.href = result.url;
throw new Error("Redirected");
// If not, let's reload
alert(
"You are not authenticated, going to reload and hope that that kicks off authentication",
);
location.reload();
throw new Error("Not authenticated, got 401");
}
}
if (result.status === 401 || result.status === 403) {
alert(
"You are not authenticated, going to reload and hope that that kicks off authentication",
);
location.reload();
throw new Error("Not authenticated, got 401");
}
return result;
} catch (e: any) {
// Errors when there is no internet connection:
Expand Down
6 changes: 3 additions & 3 deletions server/http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export class HttpServer {
return c.redirect(typeof from === "string" ? from : "/");
} else {
console.error("Authentication failed, redirecting to auth page.");
return c.redirect("/.auth?error=1");
return c.redirect("/.auth?error=1", 401);
}
},
).all((c) => {
Expand All @@ -389,9 +389,9 @@ export class HttpServer {
const redirectToAuth = () => {
// Try filtering api paths
if (req.path.startsWith("/.") || req.path.endsWith(".md")) {
return c.redirect("/.auth");
return c.redirect("/.auth", 401);
} else {
return c.redirect(`/.auth?from=${req.path}`);
return c.redirect(`/.auth?from=${req.path}`, 401);
}
};
if (!excludedPaths.includes(url.pathname)) {
Expand Down

0 comments on commit d6fb5e0

Please sign in to comment.