quak login: say in words why the login failed (closes #244)
check / check (push) Waiting to run

A password the server refuses (401 from /users/srp/verify-session) now
fails with "incorrect email or password". An email the server has no
account for (404 with a JSON body from /users/srp/attributes) fails with
"no account with this email on the server". The error stays an ApiError
with the same status. A 404 without JSON, as from a server address that
is not an Ente server, keeps the bare "HTTP 404".

Model: opus-5-5
This commit is contained in:
sneak committed 2026-11-19 06:46:20 +00:00
1 parent 2492946e9d
commit 6e361ace8a
3 files changed
+107 -19

No files matched your search

+57 -6
View File
@@ -30,7 +30,7 @@
import sodium from "libsodium-wrappers-sumo";
import { SRP, SrpServer } from "fast-srp-hap";
import { beforeAll, describe, expect, it } from "vitest";
import { ApiClient } from "../../src/api/client.js";
import { ApiClient, ApiError } from "../../src/api/client.js";
import {
init,
toBase64,
@@ -142,10 +142,15 @@ const buildServerFixture = async (password: string) => {
*
* @param fixture The server fixture from buildServerFixture
* @param opts.requireTOTP If true, verify-session returns a 2FA challenge
* @param opts.unknownEmail If true, the server has no account with the email
*/
const buildMockFetch = (
fixture: Awaited<ReturnType<typeof buildServerFixture>>,
opts?: { requireTOTP?: boolean; requirePasskeyAndTOTP?: boolean },
opts?: {
requireTOTP?: boolean;
requirePasskeyAndTOTP?: boolean;
unknownEmail?: boolean;
},
) => {
let srpServer: SrpServer;
const sessionID = "test-session-id";
@@ -163,7 +168,15 @@ const buildMockFetch = (
const parsed = new URL(url);
const path = parsed.pathname;
// The Ente server answers its refusals with an empty JSON object.
const refusal = (status: number) =>
new Response("{}", {
status,
headers: { "content-type": "application/json" },
});
if (path === "/users/srp/attributes") {
if (opts?.unknownEmail) return refusal(404);
return new Response(
JSON.stringify({ attributes: fixture.srpAttributes }),
{
@@ -197,7 +210,11 @@ const buildMockFetch = (
if (path === "/users/srp/verify-session") {
const body = JSON.parse(init?.body as string);
srpServer.checkM1(Buffer.from(body.srpM1, "base64"));
try {
srpServer.checkM1(Buffer.from(body.srpM1, "base64"));
} catch {
return refusal(401);
}
const M2 = srpServer.computeM2();
// IMPORTANT: the museum server's EmailAuthorizationResponse
@@ -365,13 +382,47 @@ describe("beginLogin via SRP", () => {
}
});
it("rejects a wrong password (SRP M1 verification fails)", async () => {
it("says a wrong password is incorrect (SRP M1 verification fails)", async () => {
const fixture = await buildServerFixture(TEST_PASSWORD);
const api = new ApiClient({ fetch: buildMockFetch(fixture) });
const err = await beginLogin(api, TEST_EMAIL, "wrong password").catch(
(e: unknown) => e,
);
expect(err).toBeInstanceOf(ApiError);
expect((err as ApiError).message).toBe("incorrect email or password");
expect((err as ApiError).status).toBe(401);
});
it("says the server has no account with an unknown email", async () => {
const fixture = await buildServerFixture(TEST_PASSWORD);
const api = new ApiClient({
fetch: buildMockFetch(fixture, { unknownEmail: true }),
});
const err = await beginLogin(api, TEST_EMAIL, TEST_PASSWORD).catch(
(e: unknown) => e,
);
expect(err).toBeInstanceOf(ApiError);
expect((err as ApiError).message).toBe(
"no account with this email on the server",
);
expect((err as ApiError).status).toBe(404);
});
// A server address that points somewhere other than an Ente server.
it("keeps the bare status of a 404 that is not JSON", async () => {
const api = new ApiClient({
fetch: (async () =>
new Response("404 page not found", {
status: 404,
headers: { "content-type": "text/plain" },
})) as typeof globalThis.fetch,
});
await expect(
beginLogin(api, TEST_EMAIL, "wrong password"),
).rejects.toThrow();
beginLogin(api, TEST_EMAIL, TEST_PASSWORD),
).rejects.toThrow(/^HTTP 404$/);
});
});