quak login: say in words why the login failed (closes #244)
check / check (push) Successful in 8m39s

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

+7 -2
View File
@@ -276,8 +276,13 @@ factor at that point, login fails.
On success it logs `Logged in as <email> (user <id>)` and
`Session saved to <path>`. On failure it logs `Login failed: <reason>`, and the
exit status is 1. `login` is the only command that prompts, and its prompts go
to stdout.
exit status is 1. Two reasons are given in words:
- a wrong password: `Login failed: incorrect email or password`
- an email with no account on the server:
`Login failed: no account with this email on the server`
`login` is the only command that prompts, and its prompts go to stdout.
#### `quak whoami`
+43 -11
View File
@@ -1,5 +1,5 @@
import { SRP, SrpClient } from "fast-srp-hap";
import type { ApiClient } from "../api/client.js";
import { ApiError, type ApiClient } from "../api/client.js";
import { deriveKEK, deriveLoginSubkey, fromBase64 } from "../crypto/index.js";
import type {
AuthorizationResponse,
@@ -10,6 +10,14 @@ import type {
// Ente uses the 4096-bit SRP group. Both client and server must agree.
const SRP_PARAMS = SRP.params["4096"];
// The same error, with a message that says in words what the server refused.
const withMessage = (err: ApiError, message: string): ApiError =>
new ApiError(message, err.status, {
code: err.code,
requestID: err.requestID,
body: err.body,
});
// Wrap fast-srp-hap's callback-based genKey into a Promise.
const genKey = (): Promise<Buffer> => SRP.genKey();
@@ -63,13 +71,20 @@ const srpHandshake = async (
srpClient.setB(Buffer.from(srpB, "base64"));
const srpM1 = srpClient.computeM1().toString("base64");
const verifyResponse = await api.postJSON<
AuthorizationResponse & { srpM2: string }
>("/users/srp/verify-session", {
sessionID,
srpUserID: srpAttributes.srpUserID,
srpM1,
});
type VerifyResponse = AuthorizationResponse & { srpM2: string };
let verifyResponse: VerifyResponse;
try {
verifyResponse = await api.postJSON<VerifyResponse>(
"/users/srp/verify-session",
{ sessionID, srpUserID: srpAttributes.srpUserID, srpM1 },
);
} catch (err) {
// The server answers 401 when the password does not match.
if (err instanceof ApiError && err.status === 401) {
throw withMessage(err, "incorrect email or password");
}
throw err;
}
srpClient.checkM2(Buffer.from(verifyResponse.srpM2, "base64"));
@@ -101,9 +116,26 @@ export const beginLogin = async (
email: string,
password: string,
): Promise<LoginChallenge> => {
const { attributes } = await api.getJSON<{
attributes: SRPAttributes;
}>("/users/srp/attributes", { email });
let attributes: SRPAttributes;
try {
({ attributes } = await api.getJSON<{ attributes: SRPAttributes }>(
"/users/srp/attributes",
{ email },
));
} catch (err) {
// The Ente server answers 404 with a JSON body when no account has the
// email. A server address that points somewhere else also answers 404,
// but not with JSON, so that error keeps its bare status.
if (
err instanceof ApiError &&
err.status === 404 &&
typeof err.body === "object" &&
err.body !== null
) {
throw withMessage(err, "no account with this email on the server");
}
throw err;
}
if (attributes.isEmailMFAEnabled) {
return { kind: "emailOTP" };
+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$/);
});
});