38 lines
1.3 KiB
Dart
38 lines
1.3 KiB
Dart
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:shuffle/features/auth/domain/validation/common_passwords.dart';
|
|
|
|
part 'password_policy.freezed.dart';
|
|
|
|
/// Fehler der Passwortregel.
|
|
@freezed
|
|
sealed class PasswordError with _$PasswordError {
|
|
/// Zu kurz; [missing] Zeichen fehlen noch.
|
|
const factory PasswordError.tooShort(int missing) = PasswordTooShort;
|
|
const factory PasswordError.common() = PasswordCommon;
|
|
}
|
|
|
|
/// Passwortregel (KTO-01 Kriterium 3): mindestens 10 Zeichen und nicht auf der
|
|
/// Liste häufiger Passwörter.
|
|
///
|
|
/// Die Länge zählt Unicode-Codepoints des unveränderten Passworts (Leerzeichen
|
|
/// sind gültige Zeichen und werden nicht entfernt). Nur der Listenvergleich
|
|
/// trimmt und ignoriert die Groß-/Kleinschreibung.
|
|
abstract final class PasswordPolicy {
|
|
static const minLength = 10;
|
|
|
|
/// Prüft [password]. Ist die Liste noch nicht geladen ([common] `null`),
|
|
/// läuft nur die Längenprüfung; vor dem Absenden mit Liste erneut prüfen.
|
|
static PasswordError? validate(
|
|
String password, {
|
|
CommonPasswordList? common,
|
|
}) {
|
|
final length = password.runes.length;
|
|
if (length < minLength) {
|
|
return PasswordError.tooShort(minLength - length);
|
|
}
|
|
if (common != null && common.contains(password)) {
|
|
return const PasswordError.common();
|
|
}
|
|
return null;
|
|
}
|
|
}
|