@@ -0,0 +1,775 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +// Package auth wires the email/password auth handlers (signup, login, |
| 4 | +// logout, password reset, email verification) onto the chi router. |
| 5 | +// |
| 6 | +// Design points worth knowing: |
| 7 | +// - Login is constant-time: when the username doesn't exist we still |
| 8 | +// run a Verify against a pre-computed dummy hash so response time |
| 9 | +// doesn't leak existence. |
| 10 | +// - Password-reset returns the same generic notice whether or not the |
| 11 | +// email maps to a real account (no enumeration via the reset flow). |
| 12 | +// - Tokens (verification, reset) are 32-byte random, b64url-encoded for |
| 13 | +// the URL, sha256-stored in the DB. |
| 14 | +// - Rate limits are enforced via internal/auth/throttle (login, signup, |
| 15 | +// password-reset). Login on success resets the counter for that key. |
| 16 | +// - Honeypot: signup form has a hidden `company` field; non-empty |
| 17 | +// submissions are silently dropped (200 + same notice as success so |
| 18 | +// bots can't tell they were rejected). |
| 19 | +// - 4 KiB request-body cap on every POST here so weaponized inputs |
| 20 | +// can't push the argon2 hasher into a DoS spiral. |
| 21 | +package auth |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "errors" |
| 26 | + "fmt" |
| 27 | + "log/slog" |
| 28 | + "net/http" |
| 29 | + "regexp" |
| 30 | + "strconv" |
| 31 | + "strings" |
| 32 | + "time" |
| 33 | + |
| 34 | + "github.com/go-chi/chi/v5" |
| 35 | + "github.com/jackc/pgx/v5" |
| 36 | + "github.com/jackc/pgx/v5/pgtype" |
| 37 | + "github.com/jackc/pgx/v5/pgxpool" |
| 38 | + |
| 39 | + authpkg "github.com/tenseleyFlow/shithub/internal/auth" |
| 40 | + "github.com/tenseleyFlow/shithub/internal/auth/email" |
| 41 | + "github.com/tenseleyFlow/shithub/internal/auth/password" |
| 42 | + "github.com/tenseleyFlow/shithub/internal/auth/session" |
| 43 | + "github.com/tenseleyFlow/shithub/internal/auth/throttle" |
| 44 | + "github.com/tenseleyFlow/shithub/internal/auth/token" |
| 45 | + "github.com/tenseleyFlow/shithub/internal/passwords" |
| 46 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| 47 | + "github.com/tenseleyFlow/shithub/internal/web/middleware" |
| 48 | + "github.com/tenseleyFlow/shithub/internal/web/render" |
| 49 | +) |
| 50 | + |
| 51 | +// usernameRE mirrors the path whitelist used by RepoFS. |
| 52 | +var usernameRE = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$`) |
| 53 | + |
| 54 | +// Deps is everything the auth handlers need. Constructed by the web |
| 55 | +// package and injected at registration time. |
| 56 | +type Deps struct { |
| 57 | + Logger *slog.Logger |
| 58 | + Render *render.Renderer |
| 59 | + Pool *pgxpool.Pool |
| 60 | + SessionStore session.Store |
| 61 | + Email email.Sender |
| 62 | + Branding email.Branding |
| 63 | + Argon2 password.Params |
| 64 | + Limiter *throttle.Limiter |
| 65 | + RequireEmailVerification bool |
| 66 | +} |
| 67 | + |
| 68 | +// Handlers is the registered handler set. Construct with New. |
| 69 | +type Handlers struct { |
| 70 | + d Deps |
| 71 | + q *usersdb.Queries |
| 72 | +} |
| 73 | + |
| 74 | +// New constructs the handler set, validating Deps. |
| 75 | +func New(d Deps) (*Handlers, error) { |
| 76 | + if d.Render == nil { |
| 77 | + return nil, errors.New("auth: nil Render") |
| 78 | + } |
| 79 | + if d.SessionStore == nil { |
| 80 | + return nil, errors.New("auth: nil SessionStore") |
| 81 | + } |
| 82 | + if d.Email == nil { |
| 83 | + return nil, errors.New("auth: nil Email sender") |
| 84 | + } |
| 85 | + if d.Limiter == nil { |
| 86 | + d.Limiter = throttle.NewLimiter() |
| 87 | + } |
| 88 | + password.MustGenerateDummy(d.Argon2) |
| 89 | + return &Handlers{d: d, q: usersdb.New()}, nil |
| 90 | +} |
| 91 | + |
| 92 | +// Mount registers every auth route on r. The 4 KiB body cap is applied |
| 93 | +// to POST endpoints inside this method. |
| 94 | +func (h *Handlers) Mount(r chi.Router) { |
| 95 | + r.Group(func(r chi.Router) { |
| 96 | + r.Use(middleware.MaxBodySize(4 * 1024)) |
| 97 | + r.Get("/signup", h.signupForm) |
| 98 | + r.Post("/signup", h.signupSubmit) |
| 99 | + r.Get("/login", h.loginForm) |
| 100 | + r.Post("/login", h.loginSubmit) |
| 101 | + r.Post("/logout", h.logoutSubmit) |
| 102 | + r.Get("/password/reset", h.resetRequestForm) |
| 103 | + r.Post("/password/reset", h.resetRequestSubmit) |
| 104 | + r.Get("/password/reset/{token}", h.resetConfirmForm) |
| 105 | + r.Post("/password/reset/{token}", h.resetConfirmSubmit) |
| 106 | + r.Get("/verify-email/{token}", h.verifyEmail) |
| 107 | + r.Get("/verify-email/resend", h.verifyResendForm) |
| 108 | + r.Post("/verify-email/resend", h.verifyResendSubmit) |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +// ---------------------------- signup ----------------------------------- |
| 113 | + |
| 114 | +type signupForm struct { |
| 115 | + Username string |
| 116 | + Email string |
| 117 | +} |
| 118 | + |
| 119 | +func (h *Handlers) signupForm(w http.ResponseWriter, r *http.Request) { |
| 120 | + h.renderPage(w, r, "auth/signup", map[string]any{ |
| 121 | + "Title": "Sign up", |
| 122 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 123 | + "Form": signupForm{}, |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +func (h *Handlers) signupSubmit(w http.ResponseWriter, r *http.Request) { |
| 128 | + if err := r.ParseForm(); err != nil { |
| 129 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 130 | + return |
| 131 | + } |
| 132 | + form := signupForm{ |
| 133 | + Username: strings.ToLower(strings.TrimSpace(r.PostFormValue("username"))), |
| 134 | + Email: strings.ToLower(strings.TrimSpace(r.PostFormValue("email"))), |
| 135 | + } |
| 136 | + password := r.PostFormValue("password") |
| 137 | + honeypot := r.PostFormValue("company") |
| 138 | + |
| 139 | + render := func(msg string) { |
| 140 | + h.renderPage(w, r, "auth/signup", map[string]any{ |
| 141 | + "Title": "Sign up", |
| 142 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 143 | + "Form": form, |
| 144 | + "Error": msg, |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + // Honeypot: silently treat as success so bots can't probe. |
| 149 | + if honeypot != "" { |
| 150 | + http.Redirect(w, r, "/login?notice=signup-pending", http.StatusSeeOther) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + if err := h.throttleSignup(r); err != nil { |
| 155 | + h.writeRetryAfter(w, err) |
| 156 | + render("Too many signup attempts. Please try again later.") |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + if msg := validateUsername(form.Username); msg != "" { |
| 161 | + render(msg) |
| 162 | + return |
| 163 | + } |
| 164 | + if !looksLikeEmail(form.Email) { |
| 165 | + render("Please enter a valid email address.") |
| 166 | + return |
| 167 | + } |
| 168 | + if len(password) < 10 { |
| 169 | + render("Password must be at least 10 characters.") |
| 170 | + return |
| 171 | + } |
| 172 | + if passwords.IsCommon(password) { |
| 173 | + render("That password is too common. Please choose another.") |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + hash, err := hashPassword(password, h.d.Argon2) |
| 178 | + if err != nil { |
| 179 | + h.d.Logger.ErrorContext(r.Context(), "signup: hash", "error", err) |
| 180 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + ctx := r.Context() |
| 185 | + tx, err := h.d.Pool.Begin(ctx) |
| 186 | + if err != nil { |
| 187 | + h.d.Logger.ErrorContext(ctx, "signup: begin tx", "error", err) |
| 188 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 189 | + return |
| 190 | + } |
| 191 | + defer func() { _ = tx.Rollback(ctx) }() |
| 192 | + |
| 193 | + user, err := h.q.CreateUser(ctx, tx, usersdb.CreateUserParams{ |
| 194 | + Username: form.Username, |
| 195 | + DisplayName: form.Username, |
| 196 | + PasswordHash: hash, |
| 197 | + }) |
| 198 | + if err != nil { |
| 199 | + if isUniqueViolation(err) { |
| 200 | + render("That username is already taken.") |
| 201 | + return |
| 202 | + } |
| 203 | + h.d.Logger.ErrorContext(ctx, "signup: create user", "error", err) |
| 204 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + tokEnc, tokHash, err := token.New() |
| 209 | + if err != nil { |
| 210 | + h.d.Logger.ErrorContext(ctx, "signup: token", "error", err) |
| 211 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | + em, err := h.q.CreateUserEmail(ctx, tx, usersdb.CreateUserEmailParams{ |
| 216 | + UserID: user.ID, |
| 217 | + Email: form.Email, |
| 218 | + IsPrimary: true, |
| 219 | + Verified: false, |
| 220 | + VerificationTokenHash: tokHash, |
| 221 | + }) |
| 222 | + if err != nil { |
| 223 | + if isUniqueViolation(err) { |
| 224 | + render("That email is already registered. Try signing in?") |
| 225 | + return |
| 226 | + } |
| 227 | + h.d.Logger.ErrorContext(ctx, "signup: create email", "error", err) |
| 228 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 229 | + return |
| 230 | + } |
| 231 | + |
| 232 | + if err := h.q.LinkUserPrimaryEmail(ctx, tx, usersdb.LinkUserPrimaryEmailParams{ |
| 233 | + ID: user.ID, |
| 234 | + PrimaryEmailID: pgtype.Int8{Int64: em.ID, Valid: true}, |
| 235 | + }); err != nil { |
| 236 | + h.d.Logger.ErrorContext(ctx, "signup: link primary", "error", err) |
| 237 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 238 | + return |
| 239 | + } |
| 240 | + |
| 241 | + expires := pgtype.Timestamptz{Time: time.Now().Add(24 * time.Hour), Valid: true} |
| 242 | + if _, err := h.q.CreateEmailVerification(ctx, tx, usersdb.CreateEmailVerificationParams{ |
| 243 | + UserEmailID: em.ID, |
| 244 | + TokenHash: tokHash, |
| 245 | + ExpiresAt: expires, |
| 246 | + }); err != nil { |
| 247 | + h.d.Logger.ErrorContext(ctx, "signup: create verification", "error", err) |
| 248 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 249 | + return |
| 250 | + } |
| 251 | + |
| 252 | + if err := tx.Commit(ctx); err != nil { |
| 253 | + h.d.Logger.ErrorContext(ctx, "signup: commit", "error", err) |
| 254 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 255 | + return |
| 256 | + } |
| 257 | + |
| 258 | + // Best-effort send. SMTP transient failure must not break signup. |
| 259 | + msg, err := email.VerifyMessage(h.d.Branding, form.Email, form.Username, tokEnc) |
| 260 | + if err != nil { |
| 261 | + h.d.Logger.ErrorContext(ctx, "signup: build verify msg", "error", err) |
| 262 | + } else if err := h.d.Email.Send(ctx, msg); err != nil { |
| 263 | + h.d.Logger.WarnContext(ctx, "signup: send verify email", "error", err) |
| 264 | + } |
| 265 | + |
| 266 | + http.Redirect(w, r, "/login?notice=signup-pending", http.StatusSeeOther) |
| 267 | +} |
| 268 | + |
| 269 | +// ----------------------------- login ----------------------------------- |
| 270 | + |
| 271 | +type loginForm struct { |
| 272 | + Username string |
| 273 | +} |
| 274 | + |
| 275 | +func (h *Handlers) loginForm(w http.ResponseWriter, r *http.Request) { |
| 276 | + notice := "" |
| 277 | + switch r.URL.Query().Get("notice") { |
| 278 | + case "signup-pending": |
| 279 | + notice = "Account created. Check your email for the verification link, then sign in." |
| 280 | + case "verified": |
| 281 | + notice = "Email verified. You can sign in now." |
| 282 | + case "logged-out": |
| 283 | + notice = "Signed out." |
| 284 | + case "password-reset": |
| 285 | + notice = "Password updated. Sign in with your new password." |
| 286 | + } |
| 287 | + h.renderPage(w, r, "auth/login", map[string]any{ |
| 288 | + "Title": "Sign in", |
| 289 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 290 | + "Form": loginForm{}, |
| 291 | + "Notice": notice, |
| 292 | + "Next": r.URL.Query().Get("next"), |
| 293 | + }) |
| 294 | +} |
| 295 | + |
| 296 | +func (h *Handlers) loginSubmit(w http.ResponseWriter, r *http.Request) { |
| 297 | + if err := r.ParseForm(); err != nil { |
| 298 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 299 | + return |
| 300 | + } |
| 301 | + username := strings.ToLower(strings.TrimSpace(r.PostFormValue("username"))) |
| 302 | + pw := r.PostFormValue("password") |
| 303 | + next := r.PostFormValue("next") |
| 304 | + |
| 305 | + render := func(msg string) { |
| 306 | + h.renderPage(w, r, "auth/login", map[string]any{ |
| 307 | + "Title": "Sign in", |
| 308 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 309 | + "Form": loginForm{Username: username}, |
| 310 | + "Error": msg, |
| 311 | + "Next": next, |
| 312 | + }) |
| 313 | + } |
| 314 | + |
| 315 | + throttleKey := fmt.Sprintf("ip:%s|%s", clientIP(r), username) |
| 316 | + if err := h.d.Limiter.Hit(r.Context(), h.d.Pool, throttle.Limit{ |
| 317 | + Scope: "login", Identifier: throttleKey, |
| 318 | + Max: 6, Window: 15 * time.Minute, |
| 319 | + }); err != nil { |
| 320 | + h.writeRetryAfter(w, err) |
| 321 | + render("Too many sign-in attempts. Please try again later.") |
| 322 | + return |
| 323 | + } |
| 324 | + |
| 325 | + user, err := h.q.GetUserByUsername(r.Context(), h.d.Pool, username) |
| 326 | + if err != nil { |
| 327 | + // User doesn't exist — still hash to keep timing constant. |
| 328 | + password.VerifyAgainstDummy(pw) |
| 329 | + render("Incorrect username or password.") |
| 330 | + return |
| 331 | + } |
| 332 | + |
| 333 | + ok, err := password.Verify(pw, user.PasswordHash) |
| 334 | + if err != nil { |
| 335 | + h.d.Logger.ErrorContext(r.Context(), "login: verify", "error", err) |
| 336 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 337 | + return |
| 338 | + } |
| 339 | + if !ok { |
| 340 | + render("Incorrect username or password.") |
| 341 | + return |
| 342 | + } |
| 343 | + if user.SuspendedAt.Valid { |
| 344 | + render("This account has been suspended.") |
| 345 | + return |
| 346 | + } |
| 347 | + if h.d.RequireEmailVerification && !user.EmailVerified { |
| 348 | + render("Please verify your email before signing in. Check your inbox or request a new link.") |
| 349 | + return |
| 350 | + } |
| 351 | + |
| 352 | + // Forgive prior failed-attempt counter on success. |
| 353 | + _ = h.d.Limiter.Reset(r.Context(), h.d.Pool, "login", throttleKey) |
| 354 | + |
| 355 | + if err := h.q.TouchUserLastLogin(r.Context(), h.d.Pool, user.ID); err != nil { |
| 356 | + h.d.Logger.WarnContext(r.Context(), "login: touch last_login_at", "error", err) |
| 357 | + } |
| 358 | + |
| 359 | + // Session-fixation defense: bind user_id and re-issue cookie. The |
| 360 | + // AEAD store re-encrypts on every Save, producing a fresh ciphertext. |
| 361 | + s := middleware.SessionFromContext(r.Context()) |
| 362 | + s.UserID = user.ID |
| 363 | + s.IssuedAt = time.Now().Unix() |
| 364 | + if err := h.d.SessionStore.Save(w, r, s); err != nil { |
| 365 | + h.d.Logger.ErrorContext(r.Context(), "login: save session", "error", err) |
| 366 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 367 | + return |
| 368 | + } |
| 369 | + |
| 370 | + dest := "/" |
| 371 | + if next != "" && strings.HasPrefix(next, "/") && !strings.HasPrefix(next, "//") { |
| 372 | + // dest is constrained to single-leading-slash relative paths above, |
| 373 | + // which prevents the protocol-relative ("//evil.com") form gosec warns about. |
| 374 | + dest = next |
| 375 | + } |
| 376 | + //nolint:gosec // G710: dest is whitelisted to single-leading-slash relative paths. |
| 377 | + http.Redirect(w, r, dest, http.StatusSeeOther) |
| 378 | +} |
| 379 | + |
| 380 | +// ---------------------------- logout ----------------------------------- |
| 381 | + |
| 382 | +func (h *Handlers) logoutSubmit(w http.ResponseWriter, r *http.Request) { |
| 383 | + h.d.SessionStore.Clear(w) |
| 384 | + http.Redirect(w, r, "/login?notice=logged-out", http.StatusSeeOther) |
| 385 | +} |
| 386 | + |
| 387 | +// ------------------------- password reset ------------------------------ |
| 388 | + |
| 389 | +func (h *Handlers) resetRequestForm(w http.ResponseWriter, r *http.Request) { |
| 390 | + h.renderPage(w, r, "auth/reset_request", map[string]any{ |
| 391 | + "Title": "Reset password", |
| 392 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 393 | + "Sent": false, |
| 394 | + }) |
| 395 | +} |
| 396 | + |
| 397 | +func (h *Handlers) resetRequestSubmit(w http.ResponseWriter, r *http.Request) { |
| 398 | + if err := r.ParseForm(); err != nil { |
| 399 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 400 | + return |
| 401 | + } |
| 402 | + addr := strings.ToLower(strings.TrimSpace(r.PostFormValue("email"))) |
| 403 | + |
| 404 | + // Always show the same notice — no enumeration via this flow. |
| 405 | + notice := "If an account is registered to that address, we've sent a password-reset link." |
| 406 | + render := func() { |
| 407 | + h.renderPage(w, r, "auth/reset_request", map[string]any{ |
| 408 | + "Title": "Reset password", |
| 409 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 410 | + "Notice": notice, |
| 411 | + "Sent": true, |
| 412 | + }) |
| 413 | + } |
| 414 | + |
| 415 | + if !looksLikeEmail(addr) { |
| 416 | + render() |
| 417 | + return |
| 418 | + } |
| 419 | + |
| 420 | + if err := h.d.Limiter.Hit(r.Context(), h.d.Pool, throttle.Limit{ |
| 421 | + Scope: "reset", Identifier: "email:" + addr, |
| 422 | + Max: 3, Window: time.Hour, |
| 423 | + }); err != nil { |
| 424 | + h.writeRetryAfter(w, err) |
| 425 | + render() |
| 426 | + return |
| 427 | + } |
| 428 | + |
| 429 | + em, err := h.q.GetUserEmailByAddress(r.Context(), h.d.Pool, addr) |
| 430 | + if err != nil { |
| 431 | + // Don't even hint at existence. |
| 432 | + render() |
| 433 | + return |
| 434 | + } |
| 435 | + |
| 436 | + tokEnc, tokHash, err := token.New() |
| 437 | + if err != nil { |
| 438 | + h.d.Logger.ErrorContext(r.Context(), "reset: token", "error", err) |
| 439 | + render() |
| 440 | + return |
| 441 | + } |
| 442 | + expires := pgtype.Timestamptz{Time: time.Now().Add(time.Hour), Valid: true} |
| 443 | + if _, err := h.q.CreatePasswordReset(r.Context(), h.d.Pool, usersdb.CreatePasswordResetParams{ |
| 444 | + UserID: em.UserID, |
| 445 | + TokenHash: tokHash, |
| 446 | + ExpiresAt: expires, |
| 447 | + }); err != nil { |
| 448 | + h.d.Logger.ErrorContext(r.Context(), "reset: insert", "error", err) |
| 449 | + render() |
| 450 | + return |
| 451 | + } |
| 452 | + |
| 453 | + msg, err := email.ResetMessage(h.d.Branding, addr, tokEnc) |
| 454 | + if err == nil { |
| 455 | + if err := h.d.Email.Send(r.Context(), msg); err != nil { |
| 456 | + h.d.Logger.WarnContext(r.Context(), "reset: send", "error", err) |
| 457 | + } |
| 458 | + } |
| 459 | + render() |
| 460 | +} |
| 461 | + |
| 462 | +func (h *Handlers) resetConfirmForm(w http.ResponseWriter, r *http.Request) { |
| 463 | + tokStr := chi.URLParam(r, "token") |
| 464 | + if _, err := h.lookupValidReset(r.Context(), tokStr); err != nil { |
| 465 | + h.d.Render.HTTPError(w, r, http.StatusNotFound, "reset link invalid or expired") |
| 466 | + return |
| 467 | + } |
| 468 | + h.renderPage(w, r, "auth/reset_confirm", map[string]any{ |
| 469 | + "Title": "Choose new password", |
| 470 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 471 | + "Token": tokStr, |
| 472 | + }) |
| 473 | +} |
| 474 | + |
| 475 | +func (h *Handlers) resetConfirmSubmit(w http.ResponseWriter, r *http.Request) { |
| 476 | + if err := r.ParseForm(); err != nil { |
| 477 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 478 | + return |
| 479 | + } |
| 480 | + tokStr := chi.URLParam(r, "token") |
| 481 | + pw := r.PostFormValue("password") |
| 482 | + |
| 483 | + render := func(msg string) { |
| 484 | + h.renderPage(w, r, "auth/reset_confirm", map[string]any{ |
| 485 | + "Title": "Choose new password", |
| 486 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 487 | + "Token": tokStr, |
| 488 | + "Error": msg, |
| 489 | + }) |
| 490 | + } |
| 491 | + |
| 492 | + row, err := h.lookupValidReset(r.Context(), tokStr) |
| 493 | + if err != nil { |
| 494 | + h.d.Render.HTTPError(w, r, http.StatusNotFound, "reset link invalid or expired") |
| 495 | + return |
| 496 | + } |
| 497 | + if len(pw) < 10 { |
| 498 | + render("Password must be at least 10 characters.") |
| 499 | + return |
| 500 | + } |
| 501 | + if passwords.IsCommon(pw) { |
| 502 | + render("That password is too common. Please choose another.") |
| 503 | + return |
| 504 | + } |
| 505 | + |
| 506 | + hash, err := hashPassword(pw, h.d.Argon2) |
| 507 | + if err != nil { |
| 508 | + h.d.Logger.ErrorContext(r.Context(), "reset: hash", "error", err) |
| 509 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 510 | + return |
| 511 | + } |
| 512 | + |
| 513 | + tx, err := h.d.Pool.Begin(r.Context()) |
| 514 | + if err != nil { |
| 515 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 516 | + return |
| 517 | + } |
| 518 | + defer func() { _ = tx.Rollback(r.Context()) }() |
| 519 | + |
| 520 | + if err := h.q.UpdateUserPassword(r.Context(), tx, usersdb.UpdateUserPasswordParams{ |
| 521 | + ID: row.UserID, |
| 522 | + PasswordHash: hash, |
| 523 | + PasswordAlgo: password.Algo, |
| 524 | + }); err != nil { |
| 525 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 526 | + return |
| 527 | + } |
| 528 | + if err := h.q.ConsumePasswordReset(r.Context(), tx, row.ID); err != nil { |
| 529 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 530 | + return |
| 531 | + } |
| 532 | + if err := tx.Commit(r.Context()); err != nil { |
| 533 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 534 | + return |
| 535 | + } |
| 536 | + |
| 537 | + http.Redirect(w, r, "/login?notice=password-reset", http.StatusSeeOther) |
| 538 | +} |
| 539 | + |
| 540 | +// ------------------------- email verification -------------------------- |
| 541 | + |
| 542 | +func (h *Handlers) verifyEmail(w http.ResponseWriter, r *http.Request) { |
| 543 | + tokStr := chi.URLParam(r, "token") |
| 544 | + hash, err := token.HashOf(tokStr) |
| 545 | + if err != nil { |
| 546 | + h.d.Render.HTTPError(w, r, http.StatusNotFound, "verification link invalid") |
| 547 | + return |
| 548 | + } |
| 549 | + row, err := h.q.GetEmailVerificationByTokenHash(r.Context(), h.d.Pool, hash) |
| 550 | + if err != nil || row.UsedAt.Valid || time.Now().After(row.ExpiresAt.Time) { |
| 551 | + h.d.Render.HTTPError(w, r, http.StatusNotFound, "verification link invalid or expired") |
| 552 | + return |
| 553 | + } |
| 554 | + em, err := h.q.GetUserEmailByID(r.Context(), h.d.Pool, row.UserEmailID) |
| 555 | + if err != nil { |
| 556 | + h.d.Render.HTTPError(w, r, http.StatusNotFound, "verification link invalid") |
| 557 | + return |
| 558 | + } |
| 559 | + |
| 560 | + tx, err := h.d.Pool.Begin(r.Context()) |
| 561 | + if err != nil { |
| 562 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 563 | + return |
| 564 | + } |
| 565 | + defer func() { _ = tx.Rollback(r.Context()) }() |
| 566 | + |
| 567 | + if err := h.q.MarkUserEmailVerified(r.Context(), tx, em.ID); err != nil { |
| 568 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 569 | + return |
| 570 | + } |
| 571 | + if em.IsPrimary { |
| 572 | + if err := h.q.MarkUserEmailPrimaryVerified(r.Context(), tx, em.UserID); err != nil { |
| 573 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 574 | + return |
| 575 | + } |
| 576 | + } |
| 577 | + if err := h.q.ConsumeEmailVerification(r.Context(), tx, row.ID); err != nil { |
| 578 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 579 | + return |
| 580 | + } |
| 581 | + if err := tx.Commit(r.Context()); err != nil { |
| 582 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 583 | + return |
| 584 | + } |
| 585 | + |
| 586 | + http.Redirect(w, r, "/login?notice=verified", http.StatusSeeOther) |
| 587 | +} |
| 588 | + |
| 589 | +func (h *Handlers) verifyResendForm(w http.ResponseWriter, r *http.Request) { |
| 590 | + h.renderPage(w, r, "auth/verify_resend", map[string]any{ |
| 591 | + "Title": "Resend verification", |
| 592 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 593 | + }) |
| 594 | +} |
| 595 | + |
| 596 | +func (h *Handlers) verifyResendSubmit(w http.ResponseWriter, r *http.Request) { |
| 597 | + if err := r.ParseForm(); err != nil { |
| 598 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 599 | + return |
| 600 | + } |
| 601 | + addr := strings.ToLower(strings.TrimSpace(r.PostFormValue("email"))) |
| 602 | + notice := "If a pending account is registered to that address, we've sent a fresh verification link." |
| 603 | + render := func() { |
| 604 | + h.renderPage(w, r, "auth/verify_resend", map[string]any{ |
| 605 | + "Title": "Resend verification", |
| 606 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 607 | + "Notice": notice, |
| 608 | + }) |
| 609 | + } |
| 610 | + if !looksLikeEmail(addr) { |
| 611 | + render() |
| 612 | + return |
| 613 | + } |
| 614 | + em, err := h.q.GetUserEmailByAddress(r.Context(), h.d.Pool, addr) |
| 615 | + if err != nil || em.Verified { |
| 616 | + render() |
| 617 | + return |
| 618 | + } |
| 619 | + tokEnc, tokHash, err := token.New() |
| 620 | + if err != nil { |
| 621 | + render() |
| 622 | + return |
| 623 | + } |
| 624 | + expires := pgtype.Timestamptz{Time: time.Now().Add(24 * time.Hour), Valid: true} |
| 625 | + tx, err := h.d.Pool.Begin(r.Context()) |
| 626 | + if err != nil { |
| 627 | + render() |
| 628 | + return |
| 629 | + } |
| 630 | + defer func() { _ = tx.Rollback(r.Context()) }() |
| 631 | + if err := h.q.SetVerificationToken(r.Context(), tx, usersdb.SetVerificationTokenParams{ |
| 632 | + ID: em.ID, |
| 633 | + VerificationTokenHash: tokHash, |
| 634 | + }); err != nil { |
| 635 | + render() |
| 636 | + return |
| 637 | + } |
| 638 | + if _, err := h.q.CreateEmailVerification(r.Context(), tx, usersdb.CreateEmailVerificationParams{ |
| 639 | + UserEmailID: em.ID, TokenHash: tokHash, ExpiresAt: expires, |
| 640 | + }); err != nil { |
| 641 | + render() |
| 642 | + return |
| 643 | + } |
| 644 | + if err := tx.Commit(r.Context()); err != nil { |
| 645 | + render() |
| 646 | + return |
| 647 | + } |
| 648 | + |
| 649 | + user, err := h.q.GetUserByID(r.Context(), h.d.Pool, em.UserID) |
| 650 | + if err == nil { |
| 651 | + msg, err := email.VerifyMessage(h.d.Branding, addr, user.Username, tokEnc) |
| 652 | + if err == nil { |
| 653 | + _ = h.d.Email.Send(r.Context(), msg) |
| 654 | + } |
| 655 | + } |
| 656 | + render() |
| 657 | +} |
| 658 | + |
| 659 | +// ----------------------------- helpers --------------------------------- |
| 660 | + |
| 661 | +func (h *Handlers) renderPage(w http.ResponseWriter, r *http.Request, page string, data any) { |
| 662 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 663 | + if err := h.d.Render.Render(w, page, data); err != nil { |
| 664 | + h.d.Logger.ErrorContext(r.Context(), "render", "page", page, "error", err) |
| 665 | + } |
| 666 | +} |
| 667 | + |
| 668 | +func (h *Handlers) throttleSignup(r *http.Request) error { |
| 669 | + if err := h.d.Limiter.Hit(r.Context(), h.d.Pool, throttle.Limit{ |
| 670 | + Scope: "signup", Identifier: "ip:" + clientIP(r), |
| 671 | + Max: 5, Window: time.Hour, |
| 672 | + }); err != nil { |
| 673 | + return err |
| 674 | + } |
| 675 | + return nil |
| 676 | +} |
| 677 | + |
| 678 | +func (h *Handlers) writeRetryAfter(w http.ResponseWriter, err error) { |
| 679 | + var t *throttle.ErrThrottled |
| 680 | + if errors.As(err, &t) { |
| 681 | + w.Header().Set("Retry-After", strconv.Itoa(int(t.RetryAfter.Seconds()))) |
| 682 | + w.WriteHeader(http.StatusTooManyRequests) |
| 683 | + } |
| 684 | +} |
| 685 | + |
| 686 | +func (h *Handlers) lookupValidReset(ctx context.Context, encoded string) (usersdb.PasswordReset, error) { |
| 687 | + hash, err := token.HashOf(encoded) |
| 688 | + if err != nil { |
| 689 | + return usersdb.PasswordReset{}, err |
| 690 | + } |
| 691 | + row, err := h.q.GetPasswordResetByTokenHash(ctx, h.d.Pool, hash) |
| 692 | + if err != nil { |
| 693 | + return usersdb.PasswordReset{}, err |
| 694 | + } |
| 695 | + if row.UsedAt.Valid || time.Now().After(row.ExpiresAt.Time) { |
| 696 | + return usersdb.PasswordReset{}, errors.New("token expired or used") |
| 697 | + } |
| 698 | + return row, nil |
| 699 | +} |
| 700 | + |
| 701 | +// validateUsername returns a user-facing error string (suitable for |
| 702 | +// rendering in the form's flash slot) or "" when the name is acceptable. |
| 703 | +// Note: returns string, not error, because callers display these to the |
| 704 | +// end user — staticcheck's ST1005 rule for error capitalization doesn't |
| 705 | +// apply to UI copy. |
| 706 | +func validateUsername(name string) string { |
| 707 | + if name == "" { |
| 708 | + return "Username is required." |
| 709 | + } |
| 710 | + if len(name) > 39 { |
| 711 | + return "Username may be at most 39 characters." |
| 712 | + } |
| 713 | + if !usernameRE.MatchString(name) { |
| 714 | + return "Username may contain only lowercase letters, digits, and hyphens, and cannot start or end with a hyphen." |
| 715 | + } |
| 716 | + if authpkg.IsReserved(name) { |
| 717 | + return "That username is reserved. Please choose another." |
| 718 | + } |
| 719 | + return "" |
| 720 | +} |
| 721 | + |
| 722 | +func looksLikeEmail(s string) bool { |
| 723 | + if len(s) < 3 || len(s) > 254 { |
| 724 | + return false |
| 725 | + } |
| 726 | + at := strings.IndexByte(s, '@') |
| 727 | + if at <= 0 || at == len(s)-1 { |
| 728 | + return false |
| 729 | + } |
| 730 | + if strings.IndexByte(s, ' ') >= 0 { |
| 731 | + return false |
| 732 | + } |
| 733 | + return true |
| 734 | +} |
| 735 | + |
| 736 | +// hashPassword wraps password.Hash so callers don't need to import the |
| 737 | +// stdlib package alongside the local one. |
| 738 | +func hashPassword(pw string, p password.Params) (string, error) { |
| 739 | + return password.Hash(pw, p) |
| 740 | +} |
| 741 | + |
| 742 | +func clientIP(r *http.Request) string { |
| 743 | + if ip := middleware.RealIPFromContext(r.Context(), r); ip != "" { |
| 744 | + return ip |
| 745 | + } |
| 746 | + if h, _, err := splitHostPort(r.RemoteAddr); err == nil { |
| 747 | + return h |
| 748 | + } |
| 749 | + return r.RemoteAddr |
| 750 | +} |
| 751 | + |
| 752 | +func splitHostPort(addr string) (string, string, error) { |
| 753 | + i := strings.LastIndexByte(addr, ':') |
| 754 | + if i < 0 { |
| 755 | + return addr, "", nil |
| 756 | + } |
| 757 | + return addr[:i], addr[i+1:], nil |
| 758 | +} |
| 759 | + |
| 760 | +// isUniqueViolation matches Postgres SQLSTATE 23505. We use an interface |
| 761 | +// shim so the auth package doesn't import pgconn directly. |
| 762 | +func isUniqueViolation(err error) bool { |
| 763 | + type sqlStater interface { |
| 764 | + SQLState() string |
| 765 | + } |
| 766 | + if errors.Is(err, pgx.ErrNoRows) { |
| 767 | + return false |
| 768 | + } |
| 769 | + for cur := err; cur != nil; cur = errors.Unwrap(cur) { |
| 770 | + if s, ok := cur.(sqlStater); ok && s.SQLState() == "23505" { |
| 771 | + return true |
| 772 | + } |
| 773 | + } |
| 774 | + return false |
| 775 | +} |