Whoever knew the password could guess codes at 10 an hour forever,
unnoticed, each guess checked against every authenticator (the second
reading's M4). Every code an account's second factor refuses after a
right password (sign-in, a phrase's reset, a change) or from a signed-in
device (removing an authenticator, new recovery codes, the code from one
confirmed before) now counts, per account, from every network; after
every tenth in a row no code is taken (codesWait, saying until when), an
hour, then twice as long each time, a day at the most (longestCodeWait,
second_factor_waits.dart): at a day, ten guesses a day still have about a
1 % chance a year against one authenticator, so the first wait mails the
owner that someone knows their password. The instance keeps no address,
so sign-in and the phrase's reset now send the address the device typed
(and its language), used only for that mail after its hash is checked
against the account's. A code accepted clears it all.
Migration (module, Learn's and Todo's servers, Learn's device schema):
CREATE TABLE "second_factor_failures" ("id" bigserial PRIMARY KEY,
"authUserId" uuid NOT NULL, "failures" bigint NOT NULL, "waits"
bigint NOT NULL, "waitUntil" timestamp, "updatedAt" timestamp NOT
NULL) with a unique index on "authUserId": a new table, nothing
touched.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
71 lines
1.7 KiB
Dart
71 lines
1.7 KiB
Dart
import 'package:halkyon_base_server/base.dart';
|
|
import 'package:serverpod/serverpod.dart';
|
|
|
|
/// Keeps every code and notice mailed, in place of a mail service.
|
|
final class Mailbox implements CodeMailer {
|
|
final codes = <({String to, String code, String language})>[];
|
|
final notices =
|
|
<
|
|
({
|
|
String to,
|
|
AccountResetKind kind,
|
|
DateTime takesEffectAt,
|
|
String language,
|
|
})
|
|
>[];
|
|
|
|
/// The notices that codes wait (ADR 0017).
|
|
final codesWait = <({String to, DateTime until, String language})>[];
|
|
|
|
/// Whether it takes no mail, as a mail service down.
|
|
bool refusing = false;
|
|
|
|
@override
|
|
Future<void> send(
|
|
Session session, {
|
|
required String to,
|
|
required String code,
|
|
required String language,
|
|
}) async {
|
|
if (refusing) {
|
|
throw Exception('Connection refused');
|
|
}
|
|
codes.add((to: to, code: code, language: language));
|
|
}
|
|
|
|
@override
|
|
Future<void> sendResetNotice(
|
|
Session session, {
|
|
required String to,
|
|
required AccountResetKind kind,
|
|
required DateTime takesEffectAt,
|
|
required String language,
|
|
}) async {
|
|
if (refusing) {
|
|
throw Exception('Connection refused');
|
|
}
|
|
notices.add((
|
|
to: to,
|
|
kind: kind,
|
|
takesEffectAt: takesEffectAt,
|
|
language: language,
|
|
));
|
|
}
|
|
|
|
@override
|
|
Future<void> sendCodesWaitNotice(
|
|
Session session, {
|
|
required String to,
|
|
required DateTime until,
|
|
required String language,
|
|
}) async {
|
|
if (refusing) {
|
|
throw Exception('Connection refused');
|
|
}
|
|
codesWait.add((to: to, until: until, language: language));
|
|
}
|
|
|
|
/// The last code mailed to [address].
|
|
String codeFor(String address) =>
|
|
codes.lastWhere((mail) => mail.to == address).code;
|
|
}
|