mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
feat(auth): record credentials sign-in successes alongside rejects
Follow-up to 91a5989 (structured reject forensics). The reject helper made
"why did sign-in fail" answerable, but without a matching success record
the pair correlation is lost — debug_logs can't tell:
• 5 rejects + 1 success → forgotten password recovered
• 5 rejects + 0 success → locked-out user gave up OR attacker bailed
• 0 rejects + 1 success → normal sign-in
• 5 rejects + 5 success → busy household sharing a laptop (benign)
Added a `logAuthAccept(provider, identifier, userId)` companion that mirrors
the existing logAuthReject helper but with level:'info' and message
'credentials accepted'. Wired into the success return path of all three
Credentials providers (local / email-password / user-password). Identifier
preserved (email or userId) plus the resolved DB userId so an operator can
join across the user table for richer queries.
Operator workflow becomes:
SELECT to_char(created_at, 'HH24:MI:SS') t, level, message, meta->>'identifier'
FROM debug_logs
WHERE source='auth.authorize'
AND created_at > NOW() - INTERVAL '1h'
ORDER BY created_at;
…which reads chronologically as "X tried 5 times, succeeded on the 6th"
or "X tried 5 times, never succeeded" — exactly the signal needed to
distinguish a real lockout from a recovery.
tsc + eslint + build clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 file changed
+22
+22
@@ -36,6 +36,25 @@ function logAuthReject(
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Companion to logAuthReject — records the matching success event so that
|
||||
* a 5-reject / 1-accept burst reads as "forgotten password recovered" and
|
||||
* a 5-reject / 0-accept burst reads as "locked out OR attacker bailed".
|
||||
* Without this, reject events float alone and the pair correlation is lost.
|
||||
*/
|
||||
function logAuthAccept(
|
||||
provider: "local" | "email-password" | "user-password",
|
||||
identifier: string | null | undefined,
|
||||
userId: string,
|
||||
): void {
|
||||
logDebug({
|
||||
source: "auth.authorize",
|
||||
level: "info",
|
||||
message: `credentials accepted: ${provider}`,
|
||||
meta: { provider, identifier: identifier ?? null, userId },
|
||||
});
|
||||
}
|
||||
|
||||
declare module "next-auth" {
|
||||
interface Session {
|
||||
user: {
|
||||
@@ -164,6 +183,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
: false;
|
||||
|
||||
if (!ok) return logAuthReject("local", envPassword || user.passwordHash ? "wrong-password" : "no-password-hash", user.email);
|
||||
logAuthAccept("local", user.email, user.id);
|
||||
return { id: user.id, email: user.email ?? "", name: user.name ?? "Local user" };
|
||||
},
|
||||
}),
|
||||
@@ -186,6 +206,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
|
||||
const ok = await verifyPassword(password, user.passwordHash);
|
||||
if (!ok) return logAuthReject("email-password", "wrong-password", email);
|
||||
logAuthAccept("email-password", email, user.id);
|
||||
return { id: user.id, email: user.email ?? "", name: user.name ?? "" };
|
||||
},
|
||||
}),
|
||||
@@ -208,6 +229,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
|
||||
const ok = await verifyPassword(password, user.passwordHash);
|
||||
if (!ok) return logAuthReject("user-password", "wrong-password", userId);
|
||||
logAuthAccept("user-password", userId, user.id);
|
||||
return { id: user.id, email: user.email ?? "", name: user.name ?? "" };
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user