Files
LambdaMail/services/webmail/src/app/(auth)/admin/login/page.tsx
T
Lucas RayanandClaude Opus 5 ee240ae227 #23: show real data, unify the surfaces and make enrolment scannable
The interface was reporting things that were not true. Several screens were
mockups wired to nothing, and a reader with the console in Portuguese was
served a mixture of two languages.

Fabricated data, removed:

- The outbound queue was two invented jobs held in component state. Retry and
  Freeze only edited that local array, so the screen showed the same two rows
  on every deployment and reported success for actions that never happened.
  "Freeze" named an operation the backend has never implemented. It now reads
  /api/v1/admin/queue, which was already serving the real thing, and offers
  retry and cancel, which exist.
- Every dashboard KPI fell back to an invented constant (1420 received, 890
  sent, 3 queued, 18.4% disk), so an unreachable API produced a dashboard of
  plausible traffic for a server that might have delivered nothing. Two of
  those fields were never returned by the endpoint at all, so the fake disk
  figure was what everyone always saw. DMARC did the same with 1250/98%/97%.
- The domains screen showed thirteen DNS records for example.com, all marked
  VERIFIED, and posted {domain_id: "default-domain"} to endpoints expecting a
  real id. DKIM rotation sends "domain", not "domain_id", so its 400 was
  swallowed and a made-up TXT record displayed as though a key had been
  issued: rotation had never once worked from this screen.
- The TLS panel asserted a Let's Encrypt RSA 4096 certificate and verified
  DANE from static markup, on a deployment currently serving a self-signed
  one. It now reads the cert watcher via /api/v1/admin/tls.
- The readiness scorecard printed "10 / 10 all checks passed" regardless of
  what preflight reported, including failures.
- A message trace that found nothing invented a five-step delivery - TLS 1.3
  accepted, SPF pass, spam score 0.8 - for a message that does not exist.
- Both sidebars showed a fixed admin@lambdamail.local with a hardcoded
  "SUPER_ADMIN (2FA)", claiming a second factor whether or not one existed.
- Settings seeded a "[Boletim]" Sieve rule. Saving posts the whole list, so
  the first edit wrote that invented filter into the account's real script.

The reconcile endpoint itself answered {records_verified: 13, status:
"VERIFIED"} for any domain id, including one that does not exist, without
resolving a name. It now reports what it does, which is record the request.

Surfaces:

The root was a chooser listing webmail and the console side by side, which
asked visitors a question they should not have to answer and advertised cookie
names and audiences to anonymous readers. It now redirects into the mail
client. Accounts that may administer the server get a link to the console from
inside the app - a link to /admin/login, so crossing over still costs the
second factor the admin audience requires - and the console gets a plain link
back, since the webmail session was never given up to get there.

Enrolment:

Both enrolment screens printed the base32 secret as text and left the operator
to type 32 characters into their phone. The enrol endpoint already returned an
otpauth:// URI; it simply was not drawn. Added a shared component that renders
it as a QR locally - the seed is already in the page, and sending it to an
image service would put it in another request log for nothing.

The recovery-code step rendered its own "sign in" button while the form's
submit button stayed visible below it: two identical buttons, and no way to
copy the codes it insisted you save. It is now terminal, with copy and
continue.

Language:

translate() had no interpolation, so "Undo Send ({seconds}s remaining)" showed
the placeholder and a second hardcoded countdown was appended after it. It now
substitutes, leaving an unmatched token visible rather than blanking it.

The pt-BR bundle had lost its diacritics - "Destinatario", "Ultimo erro",
"Acoes" - from the ASCII-source rule being applied to the translations, which
are data. Restored, and the three bundles are back in key parity at 270 each,
with roughly 120 strings that were hardcoded in the source now keyed.

Healthchecks:

web had none, despite being the only service users reach directly: a wedged
Next process kept its container running and Traefik kept routing to it. Added,
using node's own fetch since the image ships no curl. rspamd and clamav gained
the timeouts and start_periods whose absence would fail them during startup.
migrate stays without one and says why: it is a one-shot job, and a probe
would report unhealthy exactly when it has succeeded.

Verified: 36 webmail tests and 41 auth tests against a real Postgres, both new
admin/me cases confirmed to fail without the route (404). lang-lint clean,
production build clean, 0 vulnerabilities, compose validates.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 19:13:53 -03:00

215 lines
8.9 KiB
TypeScript

"use client";
import React, { useState } from "react";
import Link from "next/link";
import { useTranslations } from "../../../../i18n/provider";
import { TotpEnrolment, RecoveryCodes } from "../../../../components/TotpEnrolment";
export default function AdminLoginPage() {
const t = useTranslations();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [mfaCode, setMfaCode] = useState("");
const [challengeToken, setChallengeToken] = useState<string | null>(null);
// Enrolment state: the console requires a second factor, so an account
// without one is walked through creating it here rather than being told to
// go and find another screen.
const [enrolmentToken, setEnrolmentToken] = useState<string | null>(null);
const [enrolSecret, setEnrolSecret] = useState<string | null>(null);
const [enrolUri, setEnrolUri] = useState<string | null>(null);
const [recoveryCodes, setRecoveryCodes] = useState<string[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const handleLogin = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
setLoading(true);
try {
if (enrolmentToken) {
const res = await fetch("/api/v1/user/mfa/totp/confirm", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${enrolmentToken}` },
body: JSON.stringify({ code: mfaCode }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.message || t("auth.verificationFailed"));
// Shown once. The operator signs in again with the factor they just
// created, which is also the first proof that it works.
setRecoveryCodes(data.recovery_codes ?? []);
setEnrolmentToken(null);
setEnrolSecret(null);
setMfaCode("");
return;
}
if (challengeToken) {
// Step 2: Mandatory Admin 2FA Code
const res = await fetch("/api/v1/auth/mfa/verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ challenge_token: challengeToken, code: mfaCode }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.message || t("auth.verificationFailed"));
window.location.href = "/admin/dashboard";
return;
}
// Step 1: Admin credentials
const res = await fetch("/api/v1/auth/admin/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
const data = await res.json();
// The password was right but no second factor exists. The response
// carries a grant that permits enrolling one and nothing else.
if (res.status === 403 && data.error === "MFA_ENROLLMENT_REQUIRED" && data.enrollment_token) {
const enroll = await fetch("/api/v1/user/mfa/totp/enroll", {
method: "POST",
headers: { Authorization: `Bearer ${data.enrollment_token}` },
});
const enrolData = await enroll.json();
if (!enroll.ok) throw new Error(enrolData.message || t("auth.enrolmentFailed"));
setEnrolmentToken(data.enrollment_token);
setEnrolSecret(enrolData.secret);
setEnrolUri(enrolData.uri);
return;
}
if (!res.ok) throw new Error(data.message || t("auth.authFailed"));
if (data.mfa_required) {
setChallengeToken(data.challenge_token);
} else {
window.location.href = "/admin/dashboard";
}
} catch (err: unknown) {
setError(err instanceof Error ? err.message : t("auth.genericError"));
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center px-4 bg-slate-950">
<div className="glass-panel p-8 rounded-2xl max-w-md w-full border border-emerald-900/40 shadow-2xl">
<div className="flex items-center gap-3 mb-6">
<div className="w-10 h-10 rounded-xl bg-emerald-600/20 border border-emerald-500/30 flex items-center justify-center text-emerald-400 font-bold text-xl">
&#9881;
</div>
<div>
<h1 className="text-xl font-bold text-white">{t("auth.adminLoginTitle")}</h1>
<p className="text-xs text-slate-400">{t("auth.adminStepUpNote")}</p>
</div>
</div>
{error && (
<div className="mb-4 p-3 rounded-lg bg-red-500/10 border border-red-500/30 text-red-400 text-sm">
{error}
</div>
)}
{/* The recovery-code step is terminal: it owns its own buttons, so the
form's submit button is not rendered underneath it. That overlap is
what previously put two identical "sign in" buttons on the screen
and left no way to copy the codes it insisted you save. */}
{recoveryCodes ? (
<RecoveryCodes
codes={recoveryCodes}
onContinue={() => setRecoveryCodes(null)}
continueLabel={t("auth.signInButton")}
/>
) : (
<form onSubmit={handleLogin} className="space-y-4">
{enrolSecret ? (
<div className="space-y-4">
<div className="rounded-lg border border-indigo-500/30 bg-indigo-500/10 p-3 text-xs text-indigo-300">
{t("auth.enrolmentIntro")}
</div>
<TotpEnrolment secret={enrolSecret} uri={enrolUri} />
<div>
<label className="mb-1 block text-xs font-medium text-slate-300">{t("auth.totpCodeLabel")}</label>
<input
type="text"
inputMode="numeric"
autoComplete="one-time-code"
value={mfaCode}
onChange={(e) => setMfaCode(e.target.value)}
maxLength={6}
required
className="w-full rounded-lg border border-slate-800 bg-slate-900 px-4 py-2.5 text-center font-mono text-lg tracking-widest text-white focus:border-emerald-500 focus:outline-none"
/>
</div>
</div>
) : !challengeToken ? (
<>
<div>
<label className="block text-xs font-medium text-slate-300 mb-1">{t("auth.emailLabel")}</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="admin@domain.com"
required
className="w-full px-4 py-2.5 rounded-lg bg-slate-900 border border-slate-800 text-white placeholder-slate-500 focus:outline-none focus:border-emerald-500 transition-colors"
/>
</div>
<div>
<label className="block text-xs font-medium text-slate-300 mb-1">{t("auth.passwordLabel")}</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="************"
required
className="w-full px-4 py-2.5 rounded-lg bg-slate-900 border border-slate-800 text-white placeholder-slate-500 focus:outline-none focus:border-emerald-500 transition-colors"
/>
</div>
</>
) : (
<div>
<div className="p-3 mb-4 rounded-lg bg-emerald-500/10 border border-emerald-500/30 text-emerald-300 text-xs">
{t("auth.mfaRequired")}
</div>
<label className="block text-xs font-medium text-slate-300 mb-1">{t("auth.totpCodeLabel")}</label>
<input
type="text"
value={mfaCode}
onChange={(e) => setMfaCode(e.target.value)}
placeholder="123456"
maxLength={6}
required
className="w-full px-4 py-2.5 rounded-lg bg-slate-900 border border-slate-800 text-white text-center tracking-widest text-lg font-mono focus:outline-none focus:border-emerald-500 transition-colors"
/>
</div>
)}
<button
type="submit"
disabled={loading}
className="w-full py-3 rounded-xl bg-emerald-600 hover:bg-emerald-500 text-white font-medium transition-colors shadow-lg shadow-emerald-600/20 disabled:opacity-50"
>
{loading
? t("common.loading")
: challengeToken || enrolSecret
? t("auth.verifyCodeButton")
: t("auth.signInButton")}
</button>
</form>
)}
<div className="mt-6 pt-4 border-t border-slate-800 text-center">
<Link href="/user/mail/inbox" className="text-xs text-slate-400 hover:text-slate-200 transition-colors">
{t("admin.backToWebmail")}
</Link>
</div>
</div>
</div>
);
}