release / web (push) Successful in 1m30s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 2m43s
release / android (push) Successful in 4m31s
release / Build signed APK (push) Successful in 5m0s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 13s
release / Verify release artifacts (tag releases only) (push) Skipped
#8330: every password the api handlers store goes through hashPassword at bcrypt.DefaultCost, most of a second per hash under -race, so the password tests took about 1.4s each, roughly 15s of the package. The cost is now a package variable the tests' TestMain lowers to MinCost, after refusing a production default below bcrypt.DefaultCost. #8331: recommendation and taste each had a splitGenres and subsonic's hasGenre split inline, the same rule three times. internal/genretag.Split is the one copy, a stdlib-only leaf, and the two TestSplitGenres tables are one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.7 KiB
Go
79 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/apierror"
|
|
"github.com/Fabled-Sword/minstrel/internal/auth"
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// requireUser returns the authenticated user from request context. On
|
|
// missing context, writes a 401 envelope and returns ok=false; the
|
|
// caller MUST return immediately on ok=false. Defensive belt against
|
|
// middleware bypass — handlers using this don't need a separate check.
|
|
func requireUser(w http.ResponseWriter, r *http.Request) (dbq.User, bool) {
|
|
user, ok := auth.UserFromContext(r.Context())
|
|
if !ok {
|
|
writeErr(w, apierror.Unauthorized("unauthenticated", ""))
|
|
return dbq.User{}, false
|
|
}
|
|
return user, true
|
|
}
|
|
|
|
// minPasswordLength is the shortest password any account may set: at
|
|
// sign-up, on reset, on change, and when an admin sets one.
|
|
const minPasswordLength = 8
|
|
|
|
// checkNewPassword answers 400 password_too_short and returns false when pw
|
|
// is too short to set. Every path that sets a password calls it, so the
|
|
// rule lives in one place.
|
|
func checkNewPassword(w http.ResponseWriter, pw string) bool {
|
|
if len(pw) < minPasswordLength {
|
|
writeErr(w, apierror.BadRequest("password_too_short",
|
|
fmt.Sprintf("password must be at least %d characters", minPasswordLength)))
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// passwordCost is bcrypt's work factor for stored passwords. The package's
|
|
// tests lower it, since each hash at the default cost takes most of a second
|
|
// under the race detector; their TestMain refuses a default below bcrypt's.
|
|
var passwordCost = bcrypt.DefaultCost
|
|
|
|
// hashPassword is the one place a password is hashed for storage.
|
|
func hashPassword(pw string) ([]byte, error) {
|
|
return bcrypt.GenerateFromPassword([]byte(pw), passwordCost)
|
|
}
|
|
|
|
// requireURLUUID parses the chi URL parameter named key as a UUID. On
|
|
// invalid input, writes a 400 envelope and returns ok=false; the caller
|
|
// MUST return immediately on ok=false.
|
|
func requireURLUUID(w http.ResponseWriter, r *http.Request, key string) (pgtype.UUID, bool) {
|
|
id, ok := parseUUID(chi.URLParam(r, key))
|
|
if !ok {
|
|
writeErr(w, apierror.BadRequest("invalid_id", ""))
|
|
return id, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
// decodeBody decodes the request body as JSON into out. On decode
|
|
// failure, writes a 400 envelope and returns false; the caller MUST
|
|
// return immediately on false. Out parameter pattern (rather than
|
|
// returning T) keeps the type explicit at the call site.
|
|
func decodeBody[T any](w http.ResponseWriter, r *http.Request, out *T) bool {
|
|
if err := json.NewDecoder(r.Body).Decode(out); err != nil {
|
|
writeErr(w, apierror.BadRequest("invalid_body", ""))
|
|
return false
|
|
}
|
|
return true
|
|
}
|