refactor(api): one password rule, one hash, one unique-violation check (#8002)

The length check and bcrypt call were copied into sign-up, reset,
change and both admin paths; the unique-violation test into three api
handlers and the library. checkNewPassword and hashPassword now sit in
api/prelude.go, and db.IsUniqueViolation serves api and library. The
taken-username and taken-email conflicts now carry a message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:45:18 -05:00
co-authored by Claude Opus 5.5
parent cfdd6b916f
commit 074d9ffb0e
10 changed files with 66 additions and 55 deletions
+2 -4
View File
@@ -6,7 +6,6 @@ import (
"net/http"
"github.com/jackc/pgx/v5"
"golang.org/x/crypto/bcrypt"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
@@ -34,8 +33,7 @@ func (h *handlers) handleResetPassword(w http.ResponseWriter, r *http.Request) {
writeErr(w, apierror.BadRequest("invalid_body", ""))
return
}
if len(req.NewPassword) < minPasswordLength {
writeErr(w, apierror.BadRequest("password_too_short", "password must be at least 8 chars"))
if !checkNewPassword(w, req.NewPassword) {
return
}
if req.Token == "" {
@@ -89,7 +87,7 @@ func (h *handlers) handleResetPassword(w http.ResponseWriter, r *http.Request) {
// claim, the token is "burned" but the password isn't reset —
// user has to request another. That's acceptable; bcrypt and
// UPDATE rarely fail in healthy systems.
hash, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
hash, err := hashPassword(req.NewPassword)
if err != nil {
h.logger.Error("reset password: hash failed", "err", err)
writeErr(w, apierror.Internal(err))