Skip to content
Draft
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
5 changes: 3 additions & 2 deletions app/src/components/desktop/window-signed-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const WINDOW_SIGN_IN_FAILED_LINE =
* the page the sign-in was for (`lib/auth/window-session.ts`).
*
* While it does, it draws what the signed-in gate draws while its checks run (`AppFrameSkeleton`):
* the app's empty frame for somebody who was signed in here before, else nothing. When the sign-in
* didn't finish it tells Desktop, which shows its own connect screen and says why. Until then, or in
* the app's empty frame for somebody who was signed in here before, else nothing. Signed in, it tells
* Desktop before going on (`finishWindowSignIn`). When the sign-in didn't finish it tells Desktop,
* which shows its own connect screen and says why. Until then, or in
* a browser, it says so in the frame `/desktop-return` and `OpenFromApp` use, with the way to sign in
* again.
*/
Expand Down
14 changes: 10 additions & 4 deletions app/src/lib/auth/window-session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { tryClient } from "@/lib/client";
import type { WindowSignInFailure } from "@/lib/desktop/window-sign-in";
import {
tellDesktopSignedIn,
type WindowSignInFailure,
} from "@/lib/desktop/window-sign-in";
import { signInReturnPath } from "./resume";

/**
Expand Down Expand Up @@ -84,16 +87,19 @@ let once: Promise<WindowSessionAnswer> | null = null;

/**
* Take the fragment and swap its code, once per page however often the page is drawn: the code is
* spent by the first try. Signed in, the window goes on to `next`, replacing this page in the
* history.
* spent by the first try. Signed in, the page tells Desktop (`window_signed_in`), and the window
* goes on to `next`, replacing this page in the history.
*/
export function finishWindowSignIn(): Promise<WindowSessionAnswer> {
once ??= (async (): Promise<WindowSessionAnswer> => {
const { code, codeVerifier, next } = takeWindowSignIn();
if (!code || !codeVerifier)
return { signedIn: false, reason: "invalid_code" };
const answer = await redeemWindowSignIn(code, codeVerifier);
if (answer.signedIn) windowSessionDeps.go(next);
if (answer.signedIn) {
await tellDesktopSignedIn();
windowSessionDeps.go(next);
}
return answer;
})();
return once;
Expand Down
28 changes: 28 additions & 0 deletions app/src/lib/desktop/window-sign-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,31 @@ export async function tellDesktopSignInFailed(
return false;
}
}

/** How long the page waits for Desktop to take `window_signed_in` before it goes on regardless. */
export const SIGNED_IN_TELL_WAIT_MS = 3_000;

/**
* Tell Desktop the window's sign-in finished (`window_signed_in`), before the window goes on to the
* app, as it is told when it didn't (`tellDesktopSignInFailed`): Desktop ends setup with the
* connection then, not before. Answers whether Desktop heard it; outside Desktop nobody does, and a
* Desktop from before the command refuses it (it counted the connection before the trade). Never
* waits longer than `SIGNED_IN_TELL_WAIT_MS`, so a Desktop that doesn't answer never holds the
* window here; Desktop then takes the window going on to the app as the same news.
*/
export async function tellDesktopSignedIn(): Promise<boolean> {
if (desktopShell() === null) return false;
let timer: ReturnType<typeof setTimeout> | undefined;
const waited = new Promise<boolean>((resolve) => {
timer = setTimeout(() => resolve(false), SIGNED_IN_TELL_WAIT_MS);
});
const told = invokeDesktop("window_signed_in").then(
() => true,
() => false,
);
try {
return await Promise.race([told, waited]);
} finally {
clearTimeout(timer);
}
}
75 changes: 72 additions & 3 deletions app/tests/desktop-window-sign-in.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import { test } from "./renderer-crash";
*
* - `/desktop/signed-in#code=…&code_verifier=…&next=…`: the fragment is read and gone from the
* address before anything is asked, the code and verifier are posted from the page's own origin,
* and the window goes on to `next` only when it is a path on this app. A refused code tells
* Desktop (`window_sign_in_failed`) and says the sign-in didn't finish.
* and the window goes on to `next` only when it is a path on this app, once it told Desktop the
* code traded (`window_signed_in`). A refused code tells Desktop (`window_sign_in_failed`) and
* says the sign-in didn't finish. A Desktop without `window_signed_in`, or one that doesn't
* answer it, never holds the window there.
* - The sign-in screen inside Desktop asks Desktop to sign in in the person's browser
* (`sign_in_with_browser`). A desktop app without the command signs in as it always did.
* - OpenBot refusing the window (401) goes to the sign-in screen, as in a browser, and never asks
Expand Down Expand Up @@ -66,9 +68,14 @@ const NEW_DESKTOP = [
"sign_out",
"sign_in_with_browser",
"window_sign_in_failed",
"window_signed_in",
"open_in_browser",
"sign_in_refused",
];
/** A Desktop with cookie mode from before the page said the code traded (`window_signed_in`). */
const COOKIE_DESKTOP_BEFORE_TRADED = NEW_DESKTOP.filter(
(command) => command !== "window_signed_in",
);
/** A Desktop from before cookie mode. */
const OLDER_DESKTOP = ["connection", "sign_out"];

Expand Down Expand Up @@ -373,6 +380,8 @@ async function inWindow(
commands?: string[] | null;
/** Commands the desktop app has but refuses, with what it says (`<kind>: <sentence>`). */
refusals?: Record<string, string>;
/** Commands the desktop app has and never answers. */
unanswered?: string[];
signedIn?: boolean;
prepare?: (openbot: FakeOpenBot) => void;
/** Something answering in OpenBot's place before the window opens, a captive portal say. */
Expand Down Expand Up @@ -406,9 +415,11 @@ async function inWindow(
({
commands,
refusals,
unanswered,
}: {
commands: string[] | null;
refusals: Record<string, string>;
unanswered: string[];
}) => {
// How long the tab's history was when it first opened this OpenBot, kept across its pages.
if (sessionStorage.getItem("test-history-start") === null)
Expand All @@ -433,6 +444,7 @@ async function inWindow(
// The command's own refusal, in Desktop's words.
const refusal = refusals[name];
if (refusal !== undefined) throw refusal;
if (unanswered.includes(name)) return new Promise(() => {});
if (name === "connection")
return { mode: "connected", address: "https://my.open.bot" };
return null;
Expand All @@ -442,6 +454,7 @@ async function inWindow(
{
commands: options.commands === undefined ? NEW_DESKTOP : options.commands,
refusals: options.refusals ?? {},
unanswered: options.unanswered ?? [],
},
);
openbot.keepCookie = () =>
Expand Down Expand Up @@ -569,6 +582,58 @@ describe.skipIf(!asked)(
// Replaced, never pushed: back doesn't return to the page with the code.
expect(await historyAdded(page)).toBe(0);
expect(named(calls, "window_sign_in_failed")).toEqual([]);
// Desktop is told the code traded, once, after the trade and before the app asks
// anything: setup ends there, not before.
expect(named(calls, "window_signed_in")).toEqual([
{ name: "window_signed_in", args: null },
]);
const told = openbot.order.indexOf("desktop:window_signed_in");
expect(told).toBeGreaterThan(
openbot.order.indexOf("/api/auth/window/session"),
);
expect(told).toBeLessThan(openbot.order.indexOf(ME));
},
);
}, 240_000);

test(`a Desktop from before window_signed_in still goes on to next, in ${engine}`, async () => {
await inWindow(
{
engine,
path: fragment("/channel/c_1"),
commands: COOKIE_DESKTOP_BEFORE_TRADED,
},
async (page, openbot, calls) => {
await conversation(page);
expect(landed(page)).toBe("/channel/c_1");
expect(openbot.asked("/api/auth/window/session")).toHaveLength(1);
// Asked, refused in Tauri's words, and the window went on regardless.
expect(named(calls, "window_signed_in")).toHaveLength(1);
expect(named(calls, "window_sign_in_failed")).toEqual([]);
},
);
}, 240_000);

test(`a Desktop that doesn't answer window_signed_in never holds the window, in ${engine}`, async () => {
await inWindow(
{
engine,
path: fragment("/channel/c_1"),
unanswered: ["window_signed_in"],
},
async (page, openbot, calls) => {
// Traded and told: the window waits for Desktop's answer before it goes on, as long
// as it waits for none.
await until(
() =>
openbot.asked("/api/auth/window/session").length === 1 &&
named(calls, "window_signed_in").length === 1,
);
await new Promise((resolve) => setTimeout(resolve, 1_000));
expect(landed(page)).toBe("/desktop/signed-in");
await conversation(page);
expect(landed(page)).toBe("/channel/c_1");
expect(named(calls, "window_signed_in")).toHaveLength(1);
},
);
}, 240_000);
Expand Down Expand Up @@ -611,8 +676,10 @@ describe.skipIf(!asked)(
args: { reason: "invalid_code" },
},
]);
// Not signed in, so the app behind the gate was never asked for.
// Not signed in, so the app behind the gate was never asked for, and Desktop was never
// told the code traded.
expect(openbot.asked(ME)).toEqual([]);
expect(named(calls, "window_signed_in")).toEqual([]);
await expect(
page.getByRole("link", { name: "Sign in" }).getAttribute("href"),
).resolves.toBe("/sign");
Expand Down Expand Up @@ -660,6 +727,7 @@ describe.skipIf(!asked)(
expect(named(calls, "window_sign_in_failed")).toEqual([
{ name: "window_sign_in_failed", args: { reason: "refused" } },
]);
expect(named(calls, "window_signed_in")).toEqual([]);
},
);
}, 240_000);
Expand All @@ -673,6 +741,7 @@ describe.skipIf(!asked)(
.waitFor();
expect(openbot.asked("/api/auth/window/session")).toEqual([]);
expect(named(calls, "window_sign_in_failed")).toHaveLength(1);
expect(named(calls, "window_signed_in")).toEqual([]);
},
);
}, 240_000);
Expand Down
Loading
Loading