Files
minstrel/internal/api/me_password.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

96 lines
3.1 KiB
Go

package api
import (
"encoding/json"
"net/http"
"golang.org/x/crypto/bcrypt"
"github.com/Fabled-Sword/minstrel/internal/apierror"
"github.com/Fabled-Sword/minstrel/internal/audit"
"github.com/Fabled-Sword/minstrel/internal/auth"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
type changePasswordReq struct {
CurrentPassword string `json:"current_password"`
NewPassword string `json:"new_password"`
// SignOutSubsonic also ends the account's Subsonic apps' access, for
// someone changing a password they think another person knows. It is a
// choice here, unlike on a reset, because it signs out the owner's own
// apps too: they make a new key or Subsonic password in Settings.
SignOutSubsonic bool `json:"sign_out_subsonic"`
}
// handleChangePassword implements PUT /api/me/password. Self-service:
// the caller proves they know their current password before they can
// set a new one. Distinct from the admin-driven reset path.
func (h *handlers) handleChangePassword(w http.ResponseWriter, r *http.Request) {
user, ok := requireUser(w, r)
if !ok {
return
}
var req changePasswordReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeErr(w, apierror.BadRequest("invalid_body", ""))
return
}
if !checkNewPassword(w, req.NewPassword) {
return
}
q := dbq.New(h.pool)
current, err := q.GetUserByID(r.Context(), user.ID)
if err != nil {
h.logger.Error("change password: lookup failed", "err", err)
writeErr(w, apierror.Internal(err))
return
}
if err := bcrypt.CompareHashAndPassword([]byte(current.PasswordHash), []byte(req.CurrentPassword)); err != nil {
writeErr(w, apierror.Unauthorized("wrong_password", ""))
return
}
hash, err := hashPassword(req.NewPassword)
if err != nil {
h.logger.Error("change password: hash failed", "err", err)
writeErr(w, apierror.Internal(err))
return
}
if err := q.ChangeUserPassword(r.Context(), dbq.ChangeUserPasswordParams{
ID: user.ID,
PasswordHash: string(hash),
}); err != nil {
h.logger.Error("change password: update failed", "err", err)
writeErr(w, apierror.Internal(err))
return
}
// Sign out every other device. A password change is often the response
// to suspecting someone else has it, and their session would otherwise
// outlive the password it was opened with. The device making the change
// stays signed in.
if sessID, ok := auth.SessionIDFromContext(r.Context()); ok {
if _, err := q.DeleteOtherSessionsForUser(r.Context(), dbq.DeleteOtherSessionsForUserParams{
UserID: user.ID,
ID: sessID,
}); err != nil {
h.logger.Error("change password: revoke other sessions failed", "err", err)
}
}
var auditMeta map[string]any
if req.SignOutSubsonic {
if err := h.signOutSubsonicApps(r.Context(), user.ID); err != nil {
h.logger.Error("change password: sign out subsonic apps failed", "err", err)
writeErr(w, apierror.Internal(err))
return
}
auditMeta = map[string]any{"signed_out_subsonic": true}
}
audit.WriteOrLog(r.Context(), h.pool, h.logger, user.ID, user.ID, audit.ActionPasswordChangeSelf, auditMeta)
w.WriteHeader(http.StatusNoContent)
}