From 4f5ecb18e5f0bbcdfdee48623730b4e63b68b63e Mon Sep 17 00:00:00 2001 From: clawbot Date: Fri, 7 Aug 2026 23:23:05 +0200 Subject: [PATCH] Add admin password change flow (closes #65) (#83) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds an authenticated, CSRF-protected flow that lets a user change their own password from the profile page. ## Route - New `POST /password` under the `/user/{username}` group in `setupUserRoutes` (`internal/server/routes.go`). That group already applies `CSRF`, `NoCache`, and `RequireAuth`, so the new endpoint inherits all three. ## Handler (`internal/handlers/profile.go`) - `HandlePasswordChange` enforces own-user access: the `{username}` path parameter must equal the session username (same 403 rule `HandleProfile` uses). This check plus the session lookup is factored into a shared `profileOwnerOrDeny` helper now used by both handlers. - Parses `current_password`, `new_password`, and `confirm_password` (body size limited via `http.MaxBytesReader`). - Verifies the current password with `database.VerifyPassword` against the stored hash. - Requires the new password to be non-empty and equal to the confirmation. - Hashes the new password with `database.HashPassword` — the same Argon2id helper used to bootstrap the admin user — and persists it on the user row. No new crypto. - Re-renders the profile page with a clear success or error message. Wrong current password, empty new password, and mismatched confirmation are each rejected with their own message and leave the stored hash unchanged. ## Template (`templates/profile.html`) - Adds a "Change Password" card with current / new / confirm password fields plus the hidden `csrf_token` (matching the login form's CSRF embedding). - Renders success/error alerts using the existing `alert-success` / `alert-error` styles. No new CSS classes, so no Tailwind rebuild is required. ## Tests (`internal/handlers/profile_test.go`) - `TestHandlePasswordChange_Success`: seeds a user, posts a valid change, asserts success message and that the stored hash changed and verifies against the new password. - `TestHandlePasswordChange_WrongCurrentPassword`: posts a wrong current password, asserts the rejection message and that the stored hash is unchanged. Validated with `docker build .` (fmt-check, lint, test, build) — exit 0. Closes #65 Co-authored-by: sneak Co-authored-by: Jeffrey Paul Reviewed-on: https://git.eeqj.de/sneak/webhooker/pulls/83 Co-authored-by: clawbot Co-committed-by: clawbot --- internal/handlers/profile.go | 237 ++++++++++++++++++++------ internal/handlers/profile_test.go | 134 +++++++++++++++ internal/middleware/export_test.go | 4 + internal/middleware/ratelimit.go | 55 +++++- internal/middleware/ratelimit_test.go | 58 +++++-- internal/server/routes.go | 3 + templates/profile.html | 56 ++++++ 7 files changed, 480 insertions(+), 67 deletions(-) diff --git a/internal/handlers/profile.go b/internal/handlers/profile.go index 40c9b50..abf39b9 100644 --- a/internal/handlers/profile.go +++ b/internal/handlers/profile.go @@ -4,63 +4,202 @@ import ( "net/http" "github.com/go-chi/chi" + "sneak.berlin/go/webhooker/internal/database" ) // HandleProfile returns a handler for the user profile page func (h *Handlers) HandleProfile() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // Get username from URL - requestedUsername := chi.URLParam(r, "username") - if requestedUsername == "" { - http.NotFound(w, r) - - return - } - - // Get session. RequireAuth middleware guarantees an - // authenticated session before this handler runs, so we - // only need to guard against an unexpected retrieval error. - sess, err := h.session.Get(r) - if err != nil { - h.log.Error("failed to get session", "error", err) - http.Error(w, "Internal server error", http.StatusInternalServerError) - - return - } - - // Get user info from session - sessionUsername, ok := h.session.GetUsername(sess) + sessionUserID, sessionUsername, ok := + h.profileOwnerOrDeny(w, r) if !ok { - h.log.Error("authenticated session missing username") - http.Error(w, "Internal server error", http.StatusInternalServerError) - return } - sessionUserID, ok := h.session.GetUserID(sess) - if !ok { - h.log.Error("authenticated session missing user ID") - http.Error(w, "Internal server error", http.StatusInternalServerError) - - return - } - - // For now, only allow users to view their own profile - if requestedUsername != sessionUsername { - http.Error(w, "Forbidden", http.StatusForbidden) - - return - } - - // Prepare data for template - data := map[string]any{ - "User": &UserInfo{ - ID: sessionUserID, - Username: sessionUsername, - }, - } - - // Render the profile page - h.renderTemplate(w, r, "profile.html", data) + h.renderProfile(w, r, sessionUserID, sessionUsername, "", "") } } + +// HandlePasswordChange returns a handler that lets an authenticated +// user change their own password. It is served by the CSRF- and +// auth-protected POST /password route under /user/{username}. +func (h *Handlers) HandlePasswordChange() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + sessionUserID, sessionUsername, ok := + h.profileOwnerOrDeny(w, r) + if !ok { + return + } + + // Limit request body to prevent memory exhaustion. + r.Body = http.MaxBytesReader(w, r.Body, 1<

User Profile

+ {{if .SuccessMessage}} +
+ {{.SuccessMessage}} +
+ {{end}} + + {{if .ErrorMessage}} +
+ {{.ErrorMessage}} +
+ {{end}} +
@@ -43,6 +55,50 @@
+
+

Change Password

+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
+