mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
* fix: Hand desktop sign-in back to the app on self-hosted deployments The desktop `/desktop-redirect` handoff — which bounces the transfer token back into the app via the `outline://` protocol — was nested inside the `env.isCloudHosted && team.subdomain` branch of `signIn`. On a self-hosted deployment that branch is never taken, so a desktop OIDC (or any) sign-in fell through to the single-domain path, which set an `accessToken` cookie in the browser and redirected to the app's home. The desktop app never received the token and appeared to hang on the SSO screen. Move the desktop handoff out of the cloud-hosted guard so it runs for every deployment. The cloud sessions cookie is still written for cloud-hosted teams (web and desktop alike), and the web / single-domain redirects are unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv8LuN54DQoxofSoWmTPVK * refactor: Encode transfer token in redirect URLs and clarify comment Address PR review: URL-encode the transfer token before interpolating it into the desktop-redirect and auth/redirect query strings so the value can't truncate the query if the token format ever changes, and reword the comment that inaccurately described a cookie being set in this branch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv8LuN54DQoxofSoWmTPVK * docs: Trim desktop redirect comment Remove the trailing explanation added earlier; the comment reads cleaner without it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv8LuN54DQoxofSoWmTPVK --------- Co-authored-by: Claude <noreply@anthropic.com>
192 lines
5.6 KiB
TypeScript
192 lines
5.6 KiB
TypeScript
import querystring from "node:querystring";
|
|
import { addMonths } from "date-fns";
|
|
import type { Context } from "koa";
|
|
import { pick } from "es-toolkit/compat";
|
|
import { toError } from "@shared/utils/error";
|
|
import { Client } from "@shared/types";
|
|
import { getCookieDomain } from "@shared/utils/domains";
|
|
import env from "@server/env";
|
|
import Logger from "@server/logging/Logger";
|
|
import { Event, Collection, View } from "@server/models";
|
|
import type { APIContext, AuthenticationResult } from "@server/types";
|
|
import { AuthenticationType } from "@server/types";
|
|
|
|
/**
|
|
* Parse and return the details from the "sessions" cookie in the request, if
|
|
* any. The cookie is on the apex domain and includes session details for
|
|
* other subdomains.
|
|
*
|
|
* @param ctx The Koa context
|
|
* @returns The session details
|
|
*/
|
|
export function getSessionsInCookie(ctx: Context) {
|
|
try {
|
|
const sessionCookie = ctx.cookies.get("sessions") || "";
|
|
const decodedSessionCookie = decodeURIComponent(sessionCookie);
|
|
return decodedSessionCookie ? JSON.parse(decodedSessionCookie) : {};
|
|
} catch (_err) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Signs in a user by setting authentication cookies and recording the sign-in
|
|
* event, then redirects to the appropriate destination.
|
|
*
|
|
* @param ctx the Koa context.
|
|
* @param service the name of the authentication service used to sign in.
|
|
* @param result the authentication result containing user and team details.
|
|
*/
|
|
export async function signIn(
|
|
ctx: Context | APIContext,
|
|
service: string,
|
|
{ user, team, client, isNewTeam }: AuthenticationResult
|
|
) {
|
|
const { transaction } = ctx.state;
|
|
|
|
if (team.isSuspended) {
|
|
return ctx.redirect("/?notice=team-suspended");
|
|
}
|
|
if (user.isSuspended) {
|
|
return ctx.redirect("/?notice=user-suspended");
|
|
}
|
|
|
|
if (isNewTeam) {
|
|
// see: scenes/Login/index.js for where this cookie is written when
|
|
// viewing the /login or /create pages. It is a URI encoded JSON string.
|
|
const cookie = ctx.cookies.get("signupQueryParams");
|
|
|
|
if (cookie) {
|
|
try {
|
|
const signupQueryParams = pick(
|
|
JSON.parse(querystring.unescape(cookie)),
|
|
["ref", "utm_content", "utm_medium", "utm_source", "utm_campaign"]
|
|
);
|
|
await team.update(
|
|
{
|
|
signupQueryParams,
|
|
},
|
|
{
|
|
transaction,
|
|
}
|
|
);
|
|
} catch (error) {
|
|
Logger.error(`Error persisting signup query params`, toError(error));
|
|
}
|
|
}
|
|
}
|
|
|
|
// update the database when the user last signed in
|
|
await user.updateSignedIn(ctx);
|
|
|
|
await Event.createFromContext(
|
|
ctx,
|
|
{
|
|
name: "users.signin",
|
|
userId: user.id,
|
|
authType: AuthenticationType.APP,
|
|
data: {
|
|
name: user.name,
|
|
service,
|
|
},
|
|
},
|
|
{
|
|
actorId: user.id,
|
|
teamId: team.id,
|
|
}
|
|
);
|
|
|
|
const domain = getCookieDomain(ctx.request.hostname, env.isCloudHosted);
|
|
const expires = addMonths(new Date(), 3);
|
|
|
|
// set a cookie for which service we last signed in with. This is
|
|
// only used to display a UI hint for the user for next time
|
|
ctx.cookies.set("lastSignedIn", service, {
|
|
httpOnly: false,
|
|
sameSite: true,
|
|
expires: new Date("2100"),
|
|
domain,
|
|
});
|
|
|
|
// On cloud hosted multi-team deployments, record the signed-in team in the
|
|
// sessions cookie so the web team switcher stays in sync. This applies to
|
|
// both web and desktop clients.
|
|
if (env.isCloudHosted && team.subdomain) {
|
|
// get any existing sessions (teams signed in) and add this team
|
|
const existing = getSessionsInCookie(ctx);
|
|
const sessions = encodeURIComponent(
|
|
JSON.stringify({
|
|
...existing,
|
|
[team.id]: {
|
|
name: team.name,
|
|
logoUrl: team.avatarUrl,
|
|
url: team.url,
|
|
},
|
|
})
|
|
);
|
|
ctx.cookies.set("sessions", sessions, {
|
|
httpOnly: false,
|
|
expires,
|
|
domain,
|
|
});
|
|
}
|
|
|
|
// If the authentication request originally came from the desktop app then we send the user
|
|
// back to a screen in the web app that will immediately redirect to the desktop. The reason
|
|
// to do this from the client is that if you redirect from the server then the browser ends up
|
|
// stuck on the SSO screen.
|
|
if (client === Client.Desktop) {
|
|
const token = encodeURIComponent(user.getTransferToken(service));
|
|
ctx.redirect(`${team.url}/desktop-redirect?token=${token}`);
|
|
return;
|
|
}
|
|
|
|
// Redirect to the team subdomain with a short-lived transfer token that the
|
|
// /auth/redirect handler exchanges for the actual session cookie.
|
|
if (env.isCloudHosted && team.subdomain) {
|
|
const token = encodeURIComponent(user.getTransferToken(service));
|
|
ctx.redirect(`${team.url}/auth/redirect?token=${token}`);
|
|
} else {
|
|
ctx.cookies.set("accessToken", user.getSessionToken(expires, service), {
|
|
sameSite: "lax",
|
|
expires,
|
|
});
|
|
|
|
const defaultCollectionId = team.defaultCollectionId;
|
|
|
|
if (defaultCollectionId) {
|
|
const collection = await Collection.findOne({
|
|
where: {
|
|
id: defaultCollectionId,
|
|
teamId: team.id,
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
if (collection) {
|
|
ctx.redirect(`${team.url}${collection.path}`);
|
|
return;
|
|
}
|
|
}
|
|
|
|
const [collection, view] = await Promise.all([
|
|
Collection.findFirstCollectionForUser(user, {
|
|
transaction,
|
|
}),
|
|
View.findOne({
|
|
where: {
|
|
userId: user.id,
|
|
},
|
|
transaction,
|
|
}),
|
|
]);
|
|
const hasViewedDocuments = !!view;
|
|
|
|
ctx.redirect(
|
|
!hasViewedDocuments && collection
|
|
? `${team.url}${collection.path}/recent`
|
|
: `${team.url}/home`
|
|
);
|
|
}
|
|
}
|