Account switcher: more than one account signed in at once
ci / node (pull_request) Skipped
ci / version (pull_request) Skipped
github/ci (branch) GitHub Actions
ci / github (pull_request) Successful in 2m26s
ci / docker-build (pull_request) Skipped
ci / publish (pull_request) Skipped
ci / announce (pull_request) Skipped
ci / node (pull_request) Skipped
ci / version (pull_request) Skipped
github/ci (branch) GitHub Actions
ci / github (pull_request) Successful in 2m26s
ci / docker-build (pull_request) Skipped
ci / publish (pull_request) Skipped
ci / announce (pull_request) Skipped
Someone who looks after several mailboxes of their own can now keep them all signed in in one browser and move between them from the account menu, without signing out (multi-account spec, MA-B; forum topic 75). How it holds them. The session cookie is unchanged: it is the account in front, and every request is answered with it, so nothing else in the server changes. The others ride in a second cookie, <name>_more, as a list of their own session cookies. Each session stays its own -- sealed credential, expiry and "this is my device" -- and nothing about one is read through another. At most 5 in all, all on this mail server. - Add account (account menu): with sign-in on the mail server's page it goes there with prompt=login, so the server asks again rather than reuse the first sign-in; with the password form, a small dialog asks. Refused, and the front stays, when either account's organization has addAccounts off (inbuxa:SharingPolicy), the account is on another server, or 5 are open. The same account again just comes to the front. - Switching (POST /api/auth/accounts/<id>/front) swaps it into front; the web app clears what it cached for the previous account and reloads. A message being written blocks the switch. - Sign out ends only the account in front, and the next one comes forward; Sign out of all accounts ends every one. - GET /api/auth/accounts lists them, the front first, and says whether one more may be added. Not in this change: unread counts and notifications for the accounts not in front (MA-8), which the spec puts last. The mock can sign in a second user (MOCK_SECOND_USER/PASS) and answer addAccounts false (MOCK_NO_ADD_ACCOUNTS), for the new server tests: two accounts joining, switching, the same account twice, a switch to a session it doesn't hold, signing out of one and of all, and an organization that forbids it; and the OAuth start asking prompt=login. The client tests cover listing, switching (cache cleared, reload) and both sign-outs. Checked in Chrome against the mock: add, switch, sign out of one. New strings (9, English only in the other ten catalogs): "Add account", "Sign out of all accounts", "Add an account", "Both accounts stay signed in here; switch between them from this menu.", "Working…", "That account couldn't be added.", "You can't add more accounts here.", and the other-server and organization refusals. typecheck, tests (web 1538, server 276) and build pass.
This commit is contained in:
1 parent
9f53759462
commit
34baca4365
10 files changed
+716
-9
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
/**
|
||||
* inbuxa MA-B: more than one account signed in in one browser. The session
|
||||
* cookie is the account in front; the others ride in `<name>_more`. Adding
|
||||
* signs a second account in beside the first, switching swaps them, signing
|
||||
* out ends only the one in front, and an organization that doesn't allow it
|
||||
* keeps adding off.
|
||||
*/
|
||||
|
||||
const PORT = 18801;
|
||||
process.env.MOCK_PORT = String(PORT);
|
||||
process.env.MOCK_USER = "[email protected]";
|
||||
process.env.MOCK_PASS = "first-password";
|
||||
process.env.MOCK_SECOND_USER = "[email protected]";
|
||||
process.env.MOCK_SECOND_PASS = "second-password";
|
||||
process.env.MAIL_SERVER_URL = `http://127.0.0.1:${PORT}`;
|
||||
process.env.APP_SECRET = "test-secret-for-accounts";
|
||||
|
||||
const mock = await import("./mock/index.js");
|
||||
const { createApp } = await import("./app.js");
|
||||
const { config } = await import("./config.js");
|
||||
const { MAX_ACCOUNTS, parseOthers, serializeOthers } = await import("./accounts.js");
|
||||
|
||||
const app = createApp();
|
||||
const FRONT = config.cookieName;
|
||||
const MORE = `${config.cookieName}_more`;
|
||||
|
||||
after(() => {
|
||||
(mock as { server?: { close(): void } }).server?.close();
|
||||
});
|
||||
|
||||
/** A browser's cookie jar, as far as these two cookies go. */
|
||||
class Browser {
|
||||
jar = new Map<string, string>();
|
||||
|
||||
private take(res: Response) {
|
||||
for (const line of res.headers.getSetCookie()) {
|
||||
const [pair, ...attrs] = line.split(";");
|
||||
const at = pair!.indexOf("=");
|
||||
const name = pair!.slice(0, at).trim();
|
||||
const value = pair!.slice(at + 1).trim();
|
||||
const expired = attrs.some((a) => /max-age=0/i.test(a) || /expires=thu, 01 jan 1970/i.test(a));
|
||||
if (expired || !value) this.jar.delete(name);
|
||||
else this.jar.set(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
async call(path: string, init: { method?: string; body?: unknown } = {}): Promise<{ status: number; body: any }> {
|
||||
const cookie = [...this.jar].map(([k, v]) => `${k}=${v}`).join("; ");
|
||||
const res = await app.request(path, {
|
||||
method: init.method ?? "GET",
|
||||
headers: { "content-type": "application/json", "x-requested-with": "ihasmail", ...(cookie ? { cookie } : {}) },
|
||||
...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}),
|
||||
});
|
||||
this.take(res);
|
||||
const text = await res.text();
|
||||
return { status: res.status, body: text ? JSON.parse(text) : null };
|
||||
}
|
||||
|
||||
signIn(username: string, password: string, add = false) {
|
||||
return this.call("/api/auth/login", { method: "POST", body: { username, password, ...(add ? { add: true } : {}) } });
|
||||
}
|
||||
|
||||
async accounts(): Promise<{ username: string; front: boolean; id: string }[]> {
|
||||
const res = await this.call("/api/auth/accounts");
|
||||
assert.equal(res.status, 200, JSON.stringify(res.body));
|
||||
return res.body.accounts;
|
||||
}
|
||||
}
|
||||
|
||||
test("the cookie list keeps only well-formed session cookies, at most one fewer than the cap", () => {
|
||||
const good = "abcdefghij.ABCDEFGHIJKLMN";
|
||||
assert.deepEqual(parseOthers(`${good}~not a cookie~${good}`), [good]);
|
||||
const many = Array.from({ length: 9 }, (_, i) => `abcdefgh${i}x.ABCDEFGHIJKLMN`);
|
||||
assert.equal(parseOthers(many.join("~")).length, MAX_ACCOUNTS - 1);
|
||||
assert.equal(serializeOthers(["bad", good]), good);
|
||||
});
|
||||
|
||||
test("a second account joins the first, and switching swaps them", async () => {
|
||||
const b = new Browser();
|
||||
assert.equal((await b.signIn("[email protected]", "first-password")).status, 200);
|
||||
assert.deepEqual((await b.accounts()).map((a) => a.username), ["[email protected]"]);
|
||||
const first = b.jar.get(FRONT);
|
||||
|
||||
const added = await b.signIn("[email protected]", "second-password", true);
|
||||
assert.equal(added.status, 200, JSON.stringify(added.body));
|
||||
assert.equal(added.body.added, true);
|
||||
assert.equal(b.jar.get(MORE), first, "the first account moved beside the new one");
|
||||
let accounts = await b.accounts();
|
||||
assert.deepEqual(accounts.map((a) => [a.username, a.front]), [["[email protected]", true], ["[email protected]", false]]);
|
||||
|
||||
// The same account again is not a second copy
|
||||
await b.signIn("[email protected]", "first-password", true);
|
||||
accounts = await b.accounts();
|
||||
assert.equal(accounts.length, 2);
|
||||
assert.equal(accounts[0]!.username, "[email protected]", "it came to the front instead");
|
||||
|
||||
// Switch back
|
||||
const second = accounts.find((a) => !a.front)!;
|
||||
assert.equal((await b.call(`/api/auth/accounts/${second.id}/front`, { method: "POST" })).status, 200);
|
||||
assert.equal((await b.accounts())[0]!.username, "[email protected]");
|
||||
|
||||
// Signing out ends only the one in front; the other comes forward
|
||||
const out = await b.call("/api/auth/logout", { method: "POST" });
|
||||
assert.equal(out.body.next, true);
|
||||
assert.deepEqual((await b.accounts()).map((a) => a.username), ["[email protected]"]);
|
||||
|
||||
// Sign out of all
|
||||
await b.signIn("[email protected]", "second-password", true);
|
||||
assert.equal((await b.accounts()).length, 2);
|
||||
await b.call("/api/auth/logout-all", { method: "POST" });
|
||||
assert.equal(b.jar.has(FRONT), false);
|
||||
assert.equal(b.jar.has(MORE), false);
|
||||
assert.equal((await b.call("/api/auth/accounts")).status, 401);
|
||||
});
|
||||
|
||||
test("an account in front can't switch to one it doesn't hold", async () => {
|
||||
const b = new Browser();
|
||||
await b.signIn("[email protected]", "first-password");
|
||||
assert.equal((await b.call("/api/auth/accounts/not-a-session/front", { method: "POST" })).status, 404);
|
||||
});
|
||||
|
||||
test("an organization that doesn't allow it keeps adding off", async () => {
|
||||
const b = new Browser();
|
||||
await b.signIn("[email protected]", "first-password");
|
||||
process.env.MOCK_NO_ADD_ACCOUNTS = "1";
|
||||
try {
|
||||
// The cached upstream session is a minute old at most; ask afresh
|
||||
await b.call("/api/auth/session?refresh=1");
|
||||
const res = await b.call("/api/auth/accounts");
|
||||
assert.equal(res.body.canAdd, false);
|
||||
const added = await b.signIn("[email protected]", "second-password", true);
|
||||
assert.equal(added.status, 403);
|
||||
assert.equal(added.body.error, "add_not_allowed");
|
||||
assert.deepEqual((await b.accounts()).map((a) => a.username), ["[email protected]"], "the front stayed");
|
||||
} finally {
|
||||
delete process.env.MOCK_NO_ADD_ACCOUNTS;
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* More than one signed-in account in a browser (multi-account spec, MA-B).
|
||||
*
|
||||
* The session cookie is unchanged: it is the account in front, and every
|
||||
* request is answered with it, so nothing else in the server has to know
|
||||
* there may be others. The others ride in a second cookie, `<name>_more`: a
|
||||
* list of their own session cookies, each `id.secret` exactly as the front one
|
||||
* is. Switching swaps one of them into front; adding moves the front one into
|
||||
* the list. Each session stays its own -- its own sealed credential, its own
|
||||
* expiry, its own "this is my device" -- and nothing about one can be read
|
||||
* through another.
|
||||
*
|
||||
* At most `MAX_ACCOUNTS` in all, all on the same mail server (MA-9), and only
|
||||
* while both accounts' organizations allow it (`addAccounts`, MA-C).
|
||||
*/
|
||||
import type { UpstreamSession } from "./upstream.js";
|
||||
|
||||
export const MAX_ACCOUNTS = 5;
|
||||
|
||||
/** A session cookie's shape: `id.secret`, both base64url. Anything else is dropped. */
|
||||
const COOKIE_SHAPE = /^[A-Za-z0-9_-]{8,128}\.[A-Za-z0-9_-]{8,256}$/;
|
||||
const SEP = "~";
|
||||
|
||||
export function parseOthers(value: string | undefined): string[] {
|
||||
if (!value) return [];
|
||||
const seen = new Set<string>();
|
||||
const out: string[] = [];
|
||||
for (const part of value.split(SEP)) {
|
||||
if (!COOKIE_SHAPE.test(part) || seen.has(part)) continue;
|
||||
seen.add(part);
|
||||
out.push(part);
|
||||
if (out.length >= MAX_ACCOUNTS - 1) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function serializeOthers(cookies: string[]): string {
|
||||
return cookies.filter((c) => COOKIE_SHAPE.test(c)).slice(0, MAX_ACCOUNTS - 1).join(SEP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the account behind `upstream` may have other accounts beside it:
|
||||
* `addAccounts` on its own account's `urn:inbuxa:jmap` capability. A server
|
||||
* that doesn't say (an older one, or not inbuxa) allows it, as before.
|
||||
*/
|
||||
export function mayAddAccounts(upstream: UpstreamSession): boolean {
|
||||
const primary = upstream.primaryAccounts?.["urn:ietf:params:jmap:mail"] ?? Object.keys(upstream.accounts ?? {})[0];
|
||||
const account = primary ? (upstream.accounts?.[primary] as { accountCapabilities?: Record<string, unknown> } | undefined) : undefined;
|
||||
const inbuxa = account?.accountCapabilities?.["urn:inbuxa:jmap"] as { addAccounts?: unknown } | undefined;
|
||||
return inbuxa?.addAccounts !== false;
|
||||
}
|
||||
|
||||
/** Why an account can't be added, in words for the person. */
|
||||
export const ADD_REFUSED: Record<string, string> = {
|
||||
add_full: `You can have at most ${MAX_ACCOUNTS} accounts open here.`,
|
||||
add_not_allowed: "Your organization doesn't allow adding other accounts here.",
|
||||
add_other_server: "That account is on another mail server. Only accounts on this server can be added.",
|
||||
};
|
||||
+159
-2
@@ -45,6 +45,7 @@ import { SignInError, finish as finishSignIn, needsRefresh, oauthEnabled, passwo
|
||||
import { icsProxyHandler } from "./icsproxy.js";
|
||||
import { staticHandler } from "./static.js";
|
||||
import { mailNode, webmailNode } from "./nodes.js";
|
||||
import { ADD_REFUSED, MAX_ACCOUNTS, mayAddAccounts, parseOthers, serializeOthers } from "./accounts.js";
|
||||
|
||||
type Env = { Variables: { session: LiveSession } };
|
||||
|
||||
@@ -330,6 +331,93 @@ function setSessionCookie(c: Context, value: string, remember: boolean) {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* inbuxa MA-B: the other signed-in accounts, beside the one in front. See
|
||||
* accounts.ts. Kept across a browser restart only when every account in it
|
||||
* would be (MA-7).
|
||||
*/
|
||||
const OTHERS_COOKIE = `${config.cookieName}_more`;
|
||||
|
||||
function setOthersCookie(c: Context, cookies: string[]) {
|
||||
if (!cookies.length) {
|
||||
deleteCookie(c, OTHERS_COOKIE, { path: cookiePath });
|
||||
return;
|
||||
}
|
||||
const remember = cookies.every((cookie) => sessions.resolve(cookie)?.remember === true);
|
||||
setCookie(c, OTHERS_COOKIE, serializeOthers(cookies), {
|
||||
httpOnly: true,
|
||||
sameSite: "Lax",
|
||||
secure: isSecureRequest(c),
|
||||
path: cookiePath,
|
||||
...(remember ? { maxAge: config.sessionRememberTtl } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
/** The other accounts still signed in, in their order; ended ones are left out. */
|
||||
function liveOthers(c: Context): { cookie: string; session: LiveSession }[] {
|
||||
const out: { cookie: string; session: LiveSession }[] = [];
|
||||
for (const cookie of parseOthers(getCookie(c, OTHERS_COOKIE))) {
|
||||
const session = sessions.resolve(cookie);
|
||||
if (session) out.push({ cookie, session });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Whether the account in front may have more beside it, or why not. */
|
||||
async function addRefusal(c: Context, front: LiveSession): Promise<string | null> {
|
||||
if (1 + liveOthers(c).length >= MAX_ACCOUNTS) return "add_full";
|
||||
try {
|
||||
const upstream = await getUpstreamSession(front.id, front.authorization, upstreamFor(front.username));
|
||||
if (!mayAddAccounts(upstream)) return "add_not_allowed";
|
||||
} catch {
|
||||
return "add_not_allowed";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A session just signed in to be added beside the one in front (MA-B). It
|
||||
* comes to the front and the old front joins the others; or, refused, it is
|
||||
* ended and the front stays. Returns the refusal, or null.
|
||||
*/
|
||||
async function joinAccount(
|
||||
c: Context,
|
||||
created: { cookie: string; session: LiveSession },
|
||||
upstream: Awaited<ReturnType<typeof fetchUpstreamSession>>,
|
||||
): Promise<string | null> {
|
||||
const frontCookie = getCookie(c, config.cookieName);
|
||||
const front = sessions.resolve(frontCookie);
|
||||
if (!front || !frontCookie) {
|
||||
// Nobody in front any more: an ordinary sign-in
|
||||
setSessionCookie(c, created.cookie, created.session.remember);
|
||||
return null;
|
||||
}
|
||||
const others = liveOthers(c);
|
||||
const end = (code: string | null) => {
|
||||
sessions.destroy(created.session.id);
|
||||
forgetUpstreamSession(created.session.id);
|
||||
return code;
|
||||
};
|
||||
if (upstreamFor(created.session.username) !== upstreamFor(front.username)) return end("add_other_server");
|
||||
// Both organizations must allow it
|
||||
if (!mayAddAccounts(upstream)) return end("add_not_allowed");
|
||||
const frontRefusal = await addRefusal(c, front);
|
||||
if (frontRefusal === "add_not_allowed") return end(frontRefusal);
|
||||
// Already open: that one comes to the front instead of a second copy
|
||||
if (created.session.account === front.account) return end(null);
|
||||
const existing = others.find((o) => o.session.account === created.session.account);
|
||||
if (existing) {
|
||||
end(null);
|
||||
setSessionCookie(c, existing.cookie, existing.session.remember);
|
||||
setOthersCookie(c, [frontCookie, ...others.filter((o) => o !== existing).map((o) => o.cookie)]);
|
||||
return null;
|
||||
}
|
||||
if (1 + others.length >= MAX_ACCOUNTS) return end("add_full");
|
||||
setSessionCookie(c, created.cookie, created.session.remember);
|
||||
setOthersCookie(c, [frontCookie, ...others.map((o) => o.cookie)]);
|
||||
return null;
|
||||
}
|
||||
|
||||
function upstreamFailure(c: Context, err: unknown) {
|
||||
if (err instanceof UpstreamError) {
|
||||
return c.json({ error: err.status === 401 ? "invalid_credentials" : "upstream_error", message: err.message }, err.status as 401 | 502);
|
||||
@@ -406,8 +494,19 @@ export function createApp(basePath = config.basePath): Hono<Env> {
|
||||
return c.redirect(`${basePath}/?signin_error=rate_limited`, 302);
|
||||
}
|
||||
const username = (c.req.query("username") ?? "").trim().slice(0, 320);
|
||||
// inbuxa MA-B: another account beside the one in front, if it may have one
|
||||
const front = c.req.query("add") === "1" ? sessions.resolve(getCookie(c, config.cookieName)) : null;
|
||||
if (front) {
|
||||
const refused = await addRefusal(c, front);
|
||||
if (refused) return c.redirect(`${basePath}/?account_error=${refused}`, 302);
|
||||
}
|
||||
try {
|
||||
const { location, state } = await startSignIn({ username, base: upstreamFor(username), remember: c.req.query("remember") === "1" });
|
||||
const { location, state } = await startSignIn({
|
||||
username,
|
||||
base: upstreamFor(username),
|
||||
remember: c.req.query("remember") === "1",
|
||||
adding: front !== null,
|
||||
});
|
||||
setCookie(c, OAUTH_STATE_COOKIE, state, { httpOnly: true, sameSite: "Lax", secure: isSecureRequest(c), path: `${basePath}/api/auth`, maxAge: 600 });
|
||||
return c.redirect(location, 302);
|
||||
} catch (err) {
|
||||
@@ -452,6 +551,11 @@ export function createApp(basePath = config.basePath): Hono<Env> {
|
||||
userAgent: c.req.header("user-agent") ?? "",
|
||||
ip: clientIp(c),
|
||||
});
|
||||
if (result.adding) {
|
||||
const refused = await joinAccount(c, { cookie, session }, upstream);
|
||||
if (refused) return c.redirect(`${basePath}/?account_error=${refused}`, 302);
|
||||
return c.redirect(`${basePath}/`, 302);
|
||||
}
|
||||
setSessionCookie(c, cookie, session.remember);
|
||||
const mailAccount = upstream.primaryAccounts?.["urn:ietf:params:jmap:mail"];
|
||||
if (mailAccount) pushPrepare(session.username, mailAccount, pushCredential(session));
|
||||
@@ -473,7 +577,7 @@ export function createApp(basePath = config.basePath): Hono<Env> {
|
||||
c.header("Retry-After", String(loginFloodLimiter.retryAfterSeconds(rateIp)));
|
||||
return c.json({ error: "rate_limited", message: "Too many login attempts. Please wait and try again." }, 429);
|
||||
}
|
||||
let body: { username?: string; password?: string; totp?: string; remember?: boolean };
|
||||
let body: { username?: string; password?: string; totp?: string; remember?: boolean; add?: boolean };
|
||||
try {
|
||||
body = await c.req.json();
|
||||
} catch {
|
||||
@@ -536,6 +640,12 @@ export function createApp(basePath = config.basePath): Hono<Env> {
|
||||
userAgent: c.req.header("user-agent") ?? "",
|
||||
ip,
|
||||
});
|
||||
// inbuxa MA-B: beside the account in front, when that's what was asked
|
||||
if (body.add && sessions.resolve(getCookie(c, config.cookieName))) {
|
||||
const refused = await joinAccount(c, { cookie, session }, upstream);
|
||||
if (refused) return c.json({ error: refused, message: ADD_REFUSED[refused] }, 403);
|
||||
return c.json({ ok: true, added: true });
|
||||
}
|
||||
setSessionCookie(c, cookie, session.remember);
|
||||
// Start the account's push subscription now, so it is usually verified
|
||||
// by the time the browser opens its stream. See push.ts.
|
||||
@@ -606,7 +716,54 @@ export function createApp(basePath = config.basePath): Hono<Env> {
|
||||
sessions.destroy(session.id);
|
||||
forgetUpstreamSession(session.id);
|
||||
}
|
||||
// inbuxa MA-B: only this account ends; the next one comes to the front
|
||||
const [next, ...rest] = liveOthers(c);
|
||||
if (next) {
|
||||
setSessionCookie(c, next.cookie, next.session.remember);
|
||||
setOthersCookie(c, rest.map((o) => o.cookie));
|
||||
return c.json({ ok: true, next: true });
|
||||
}
|
||||
deleteCookie(c, config.cookieName, { path: cookiePath });
|
||||
deleteCookie(c, OTHERS_COOKIE, { path: cookiePath });
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
|
||||
/* inbuxa MA-B: every account signed in here ends. */
|
||||
api.post("/auth/logout-all", async (c) => {
|
||||
const front = sessions.resolve(getCookie(c, config.cookieName));
|
||||
for (const session of [front, ...liveOthers(c).map((o) => o.session)]) {
|
||||
if (!session) continue;
|
||||
sessions.destroy(session.id);
|
||||
forgetUpstreamSession(session.id);
|
||||
}
|
||||
deleteCookie(c, config.cookieName, { path: cookiePath });
|
||||
deleteCookie(c, OTHERS_COOKIE, { path: cookiePath });
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
|
||||
/* inbuxa MA-B: the accounts signed in here, the one in front first, and whether one more may be added. */
|
||||
api.get("/auth/accounts", requireSession, async (c) => {
|
||||
const front = c.get("session");
|
||||
const others = liveOthers(c);
|
||||
if (others.length !== parseOthers(getCookie(c, OTHERS_COOKIE)).length) {
|
||||
setOthersCookie(c, others.map((o) => o.cookie));
|
||||
}
|
||||
const canAdd = (await addRefusal(c, front)) === null;
|
||||
return c.json({
|
||||
accounts: [front, ...others.map((o) => o.session)].map((s, i) => ({ id: s.id, username: s.username, front: i === 0 })),
|
||||
canAdd,
|
||||
max: MAX_ACCOUNTS,
|
||||
});
|
||||
});
|
||||
|
||||
/* inbuxa MA-B: bring another signed-in account to the front. */
|
||||
api.post("/auth/accounts/:id/front", requireSession, async (c) => {
|
||||
const frontCookie = getCookie(c, config.cookieName)!;
|
||||
const others = liveOthers(c);
|
||||
const chosen = others.find((o) => o.session.id === c.req.param("id"));
|
||||
if (!chosen) return c.json({ error: "not_found" }, 404);
|
||||
setSessionCookie(c, chosen.cookie, chosen.session.remember);
|
||||
setOthersCookie(c, [frontCookie, ...others.filter((o) => o !== chosen).map((o) => o.cookie)]);
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -23,9 +23,25 @@ function unauthorized(res: ServerResponse) {
|
||||
res.end(JSON.stringify({ type: "about:blank", status: 401, title: "Unauthorized" }));
|
||||
}
|
||||
|
||||
/*
|
||||
* inbuxa MA-B: an optional second user, so the account switcher has two real
|
||||
* accounts to move between. It signs in with a password only, and reads the
|
||||
* same mailbox: what the tests look at is who the session says it is.
|
||||
*/
|
||||
const SECOND_USER = process.env.MOCK_SECOND_USER;
|
||||
const SECOND_PASS = process.env.MOCK_SECOND_PASS;
|
||||
|
||||
/** Who a Basic header signs in as, when it is the second user. */
|
||||
function secondUser(req: IncomingMessage): boolean {
|
||||
const h = req.headers.authorization ?? "";
|
||||
if (!SECOND_USER || !h.startsWith("Basic ")) return false;
|
||||
return Buffer.from(h.slice(6), "base64").toString() === `${SECOND_USER}:${SECOND_PASS}`;
|
||||
}
|
||||
|
||||
function checkAuth(req: IncomingMessage): boolean {
|
||||
const h = req.headers.authorization ?? "";
|
||||
if (checkBearer(h)) return true;
|
||||
if (secondUser(req)) return true;
|
||||
if (!h.startsWith("Basic ") || basicRefused) return false;
|
||||
const raw = Buffer.from(h.slice(6), "base64").toString();
|
||||
const sep = raw.indexOf(":");
|
||||
@@ -83,7 +99,14 @@ export const server = createServer(async (req, res) => {
|
||||
if (!checkAuth(req)) return unauthorized(res);
|
||||
if (url.pathname === "/.well-known/jmap" || url.pathname === "/jmap/session") {
|
||||
res.writeHead(200, { "content-type": "application/json" });
|
||||
return res.end(JSON.stringify(session()));
|
||||
const answer = session() as ReturnType<typeof session> & { username: string };
|
||||
if (secondUser(req)) answer.username = SECOND_USER!;
|
||||
// MA-C: an organization that doesn't allow adding accounts
|
||||
if (process.env.MOCK_NO_ADD_ACCOUNTS === "1") {
|
||||
const own = answer.accounts[ACCOUNT] as { accountCapabilities: Record<string, unknown> };
|
||||
own.accountCapabilities = { ...own.accountCapabilities, "urn:inbuxa:jmap": { addAccounts: false } };
|
||||
}
|
||||
return res.end(JSON.stringify(answer));
|
||||
}
|
||||
// The account info endpoint; the only place a server reports its edition.
|
||||
if (url.pathname === "/api/account" && req.method === "GET") {
|
||||
|
||||
@@ -217,3 +217,23 @@ test("push keeps a credential that renews itself", async () => {
|
||||
assert.notEqual(second, first, "a fresh access token");
|
||||
assert.match(second, /^Bearer mock-at-/);
|
||||
});
|
||||
|
||||
test("inbuxa MA-B: adding an account asks the server's page to sign in again", async () => {
|
||||
// With nobody in front, add=1 is an ordinary sign-in
|
||||
let res = await call("/api/auth/oauth/[email protected]&add=1");
|
||||
assert.equal(new URL(res.headers.get("location")!).searchParams.has("prompt"), false);
|
||||
|
||||
await signIn();
|
||||
res = await call("/api/auth/oauth/[email protected]&add=1");
|
||||
assert.equal(res.status, 302);
|
||||
const signInPage = new URL(res.headers.get("location")!);
|
||||
assert.equal(signInPage.searchParams.get("prompt"), "login", "the server's page must not reuse the first sign-in");
|
||||
|
||||
// The mock's page signs the same account in again: it stays one account
|
||||
const approved = await fetch(signInPage, { redirect: "manual" });
|
||||
const back = new URL(approved.headers.get("location")!);
|
||||
res = await call(`/api/auth/callback${back.search}`);
|
||||
assert.equal(res.headers.get("location"), "/");
|
||||
const list = await jsonOf(await call("/api/auth/accounts"));
|
||||
assert.deepEqual(list.accounts.map((a: { username: string }) => a.username), ["[email protected]"]);
|
||||
});
|
||||
+9
-4
@@ -97,6 +97,8 @@ interface Pending {
|
||||
base: string;
|
||||
username: string;
|
||||
remember: boolean;
|
||||
/** MA-B: signing in a second account beside the one in front. */
|
||||
adding: boolean;
|
||||
createdAt: number;
|
||||
}
|
||||
|
||||
@@ -114,13 +116,13 @@ function challengeOf(verifier: string): string {
|
||||
* Begin a sign-in. Returns where to send the browser, and the state to bind
|
||||
* to it in a cookie.
|
||||
*/
|
||||
export async function start(params: { username: string; base: string; remember: boolean }): Promise<{ location: string; state: string }> {
|
||||
export async function start(params: { username: string; base: string; remember: boolean; adding?: boolean }): Promise<{ location: string; state: string }> {
|
||||
const metadata = await metadataFor(params.base);
|
||||
sweepPending();
|
||||
if (pending.size >= MAX_PENDING) throw new UpstreamError("Too many sign-ins in progress", 503);
|
||||
const state = randomToken(24);
|
||||
const verifier = randomToken(48);
|
||||
pending.set(state, { verifier, base: params.base, username: params.username, remember: params.remember, createdAt: Date.now() });
|
||||
pending.set(state, { verifier, base: params.base, username: params.username, remember: params.remember, adding: Boolean(params.adding), createdAt: Date.now() });
|
||||
const scope = ["openid", "offline_access"].filter((s) => metadata.scopes.length === 0 || metadata.scopes.includes(s)).join(" ");
|
||||
const url = new URL(metadata.authorizationEndpoint);
|
||||
url.searchParams.set("response_type", "code");
|
||||
@@ -131,6 +133,9 @@ export async function start(params: { username: string; base: string; remember:
|
||||
url.searchParams.set("code_challenge", challengeOf(verifier));
|
||||
url.searchParams.set("code_challenge_method", "S256");
|
||||
if (params.username) url.searchParams.set("login_hint", params.username);
|
||||
// MA-B: ask again, rather than let the server's page reuse the sign-in of
|
||||
// the account already in front
|
||||
if (params.adding) url.searchParams.set("prompt", "login");
|
||||
return { location: url.toString(), state };
|
||||
}
|
||||
|
||||
@@ -177,7 +182,7 @@ export class SignInError extends Error {
|
||||
* Finish a sign-in: `state` as it came back in the URL, `boundState` as the
|
||||
* browser's cookie holds it. Each state is good for one attempt.
|
||||
*/
|
||||
export async function finish(params: { state: string; boundState: string | undefined; code: string }): Promise<{ tokens: TokenSet; base: string; username: string; remember: boolean }> {
|
||||
export async function finish(params: { state: string; boundState: string | undefined; code: string }): Promise<{ tokens: TokenSet; base: string; username: string; remember: boolean; adding: boolean }> {
|
||||
const p = pending.get(params.state);
|
||||
if (!p || !params.boundState || params.boundState !== params.state) {
|
||||
throw new SignInError("state_mismatch", "This sign-in didn't start in this browser. Try again.");
|
||||
@@ -192,7 +197,7 @@ export async function finish(params: { state: string; boundState: string | undef
|
||||
redirect_uri: redirectUri(),
|
||||
});
|
||||
if (!tokens) throw new SignInError("exchange_failed", "The mail server didn't accept the sign-in. Try again.");
|
||||
return { tokens, base: p.base, username: p.username, remember: p.remember };
|
||||
return { tokens, base: p.base, username: p.username, remember: p.remember, adding: p.adding };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user