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
+3 -16
View File
@@ -2,30 +2,17 @@ package library
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"github.com/dhowden/tag"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// pgUniqueViolation is the SQLSTATE for a unique-constraint conflict.
const pgUniqueViolation = "23505"
// isUniqueViolation reports whether err is a Postgres unique-constraint
// conflict. Used to distinguish "another row already owns this MBID" (a
// duplicate album/artist row in our DB, for an admin to merge) from genuine
// write failures.
func isUniqueViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == pgUniqueViolation
}
// BackfillMBIDsResult tallies what the one-shot worker did.
type BackfillMBIDsResult struct {
Processed int // Albums considered (mbid IS NULL).
@@ -80,7 +67,7 @@ func BackfillMBIDs(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger,
if err := q.SetAlbumMbidIfNull(ctx, dbq.SetAlbumMbidIfNullParams{
ID: r.AlbumID, Mbid: &m,
}); err != nil {
if isUniqueViolation(err) {
if db.IsUniqueViolation(err) {
// Another album row already owns this MBID — i.e. our DB
// has two rows for the same MusicBrainz release (split by
// title/artist disagreements during the pre-MBID scan).
@@ -109,7 +96,7 @@ func BackfillMBIDs(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger,
if err := q.SetArtistMbidIfNull(ctx, dbq.SetArtistMbidIfNullParams{
ID: r.ArtistID, Mbid: &am,
}); err != nil {
if isUniqueViolation(err) {
if db.IsUniqueViolation(err) {
logger.Info("mbid backfill: duplicate artist mbid (canonical row already owns it)",
"artist_id", r.ArtistID, "mbid", artistMBID)
} else {