Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Simplify redirect logic for self-hosted #6867

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions server/errors.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems vestigial – why construct the URL above and then override the host immediately?


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