quak login: say in words why the login failed (closes #244)
check / check (push) Successful in 8m39s
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:
1 parent
2492946e9d
commit
6e361ace8a
3 files changed
+107
-19
No files matched your search
+43
-11
@@ -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" };
|
||||
|
||||
Reference in new issue
Block a user