A reset ended every session but left the API key and Subsonic password working, and anyone signed in to the account can make either. So whoever had the account kept it through a Subsonic app after the owner reset the password. The forgotten-password reset and an admin's reset now replace the API key with one nobody holds and clear the Subsonic password. A self-service change keeps Subsonic apps signed in by default, since it would sign out the owner's own apps too. The web and Android password forms gain "Sign out Subsonic apps too", sent as sign_out_subsonic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
|
"git.fabledsword.com/bvandeusen/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)
|
|
}
|