Skip to content

Commit

Permalink
fix: Simplify redirect logic for self-hosted (#6867)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed May 4, 2024
1 parent 4cb0f8b commit 86abd27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
16 changes: 8 additions & 8 deletions server/errors.ts
Expand Up @@ -8,20 +8,20 @@ export function InternalError(message = "Internal error") {

export function AuthenticationError(
message = "Authentication required",
redirectUrl = "/"
redirectPath = "/"
) {
return httpErrors(401, message, {
redirectUrl,
redirectPath,
id: "authentication_required",
});
}

export function InvalidAuthenticationError(
message = "Invalid authentication",
redirectUrl = "/"
redirectPath = "/"
) {
return httpErrors(401, message, {
redirectUrl,
redirectPath,
id: "invalid_authentication",
});
}
Expand Down Expand Up @@ -159,10 +159,10 @@ export function TeamPendingDeletionError(

export function EmailAuthenticationRequiredError(
message = "User must authenticate with email",
redirectUrl = "/"
redirectPath = "/"
) {
return httpErrors(400, message, {
redirectUrl,
redirectPath,
id: "email_auth_required",
});
}
Expand Down Expand Up @@ -201,10 +201,10 @@ export function OIDCMalformedUserInfoError(

export function AuthenticationProviderDisabledError(
message = "Authentication method has been disabled by an admin",
redirectUrl = "/"
redirectPath = "/"
) {
return httpErrors(400, message, {
redirectUrl,
redirectPath,
id: "authentication_provider_disabled",
});
}
Expand Down
15 changes: 7 additions & 8 deletions server/middlewares/passport.ts
Expand Up @@ -25,8 +25,8 @@ export default function createMiddleware(providerName: string) {

if (err.id) {
const notice = err.id.replace(/_/g, "-");
const redirectUrl = err.redirectUrl ?? "/";
const hasQueryString = redirectUrl?.includes("?");
const redirectPath = err.redirectPath ?? "/";
const hasQueryString = redirectPath?.includes("?");

// Every authentication action is routed through the apex domain.
// But when there is an error, we want to redirect the user on the
Expand All @@ -35,18 +35,17 @@ export default function createMiddleware(providerName: string) {
// get original host
const stateString = ctx.cookies.get("state");
const state = stateString ? parseState(stateString) : undefined;
const host = state?.host ?? ctx.hostname;

// form a URL object with the err.redirectUrl and replace the host
// form a URL object with the err.redirectPath and replace the host
const reqProtocol =
state?.client === Client.Desktop ? "outline" : ctx.protocol;
const requestHost = ctx.get("host");
const requestHost = state?.host ?? ctx.hostname;
const url = new URL(
`${reqProtocol}://${requestHost}${redirectUrl}`
env.isCloudHosted
? `${reqProtocol}://${requestHost}${redirectPath}`
: `${env.URL}${redirectPath}`
);

url.host = host;

return ctx.redirect(
`${url.toString()}${hasQueryString ? "&" : "?"}notice=${notice}`
);
Expand Down

0 comments on commit 86abd27

Please sign in to comment.