Konten: Zugriffe vollständig in account.Store

Handle, Anzeigename, Beschreibung, Profilbilder, Profil- und Kontodaten,
Passwortprüfung und das Löschen der kontoeigenen Tabellen lagen als rohes
SQL in den Handlern, während account.Store nur Anlegen, Reset und
Passwortwechsel kannte. Jetzt ist der Store die eine Stelle für die
Tabelle account; die Handler lesen das Formular und antworten.

Nebenbei: die Passwortprüfung gab es zweimal mit zwei Texten unter
demselben Code auth.bad_password (ChangePassword und checkPassword).
Jetzt VerifyPassword mit einem Sentinel, das Formularfeld hängt der
Handler an. Profil und Konto gehen als Structs statt Maps über die
Leitung, wie die übrigen Antworten.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 13:10:40 +01:00
co-authored by Claude Fable 5.1
parent 1c1479d455
commit a93e10956b
4 changed files with 247 additions and 141 deletions
+18 -12
View File
@@ -119,22 +119,28 @@ func (s *Store) UID(ctx context.Context, handle string) (int64, error) {
return uid, nil
}
// ErrWrongPassword: das bisherige Passwort beim Ändern stimmt nicht.
var ErrWrongPassword = apperr.Forbidden("auth.bad_password", "Das bisherige Passwort ist falsch.").At("pass")
// ErrWrongPassword: das eingegebene Passwort stimmt nicht. Der Aufrufer hängt
// das Formularfeld an (At), in dem es stand.
var ErrWrongPassword = apperr.Forbidden("auth.bad_password", "Das Passwort ist falsch.")
// VerifyPassword bestätigt eine heikle Aktion mit dem Passwort des Kontos.
func (s *Store) VerifyPassword(ctx context.Context, uid int64, password string) error {
var hash []byte
err := s.db.QueryRowContext(ctx, `SELECT password FROM account WHERE uid = $1`, uid).Scan(&hash)
if err != nil {
return notFoundOr(err, "passwort")
}
if bcrypt.CompareHashAndPassword(hash, []byte(password)) != nil {
return ErrWrongPassword
}
return nil
}
// ChangePassword setzt ein neues Passwort, wenn current das bisherige ist.
// Alle anderen Sessions des Nutzers enden; keepSession (die eigene) bleibt.
func (s *Store) ChangePassword(ctx context.Context, uid int64, current, password, keepSession string) error {
var old []byte
err := s.db.QueryRowContext(ctx, `SELECT password FROM account WHERE uid = $1`, uid).Scan(&old)
if errors.Is(err, sql.ErrNoRows) {
return ErrUserNotFound
}
if err != nil {
return fmt.Errorf("passwort lesen: %w", err)
}
if bcrypt.CompareHashAndPassword(old, []byte(current)) != nil {
return ErrWrongPassword
if err := s.VerifyPassword(ctx, uid, current); err != nil {
return err
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {