-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_email.go
More file actions
650 lines (570 loc) · 21.5 KB
/
Copy pathauth_email.go
File metadata and controls
650 lines (570 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package servex
import (
"context"
"errors"
"fmt"
"net/http"
"net/mail"
"net/smtp"
"net/url"
"strings"
"time"
"github.com/maxbolgarin/lang"
"golang.org/x/crypto/bcrypt"
)
// EmailAuthDatabase is a sub-interface of AuthDatabase required when email verification
// or password reset features are enabled. It provides email-based user lookup
// in addition to the standard AuthDatabase methods.
type EmailAuthDatabase interface {
FindByEmail(ctx context.Context, email string) (User, bool, error)
}
// SMTPEmailSender implements VerificationEmailSender, PasswordResetEmailSender,
// and TwoFactorEmailSender using net/smtp. A single SMTPEmailSender instance can be
// passed to all three flow configurations for convenience.
type SMTPEmailSender struct {
cfg SMTPConfig
verificationMode EmailVerificationMode
}
// NewSMTPEmailSender creates a new SMTPEmailSender with the provided SMTP configuration.
// The verificationMode controls how verification emails are formatted:
// - EmailVerificationCodeMode: the email body shows the code directly
// - EmailVerificationTokenMode: the email body includes a clickable link with the token
func NewSMTPEmailSender(cfg SMTPConfig, verificationMode EmailVerificationMode) *SMTPEmailSender {
return &SMTPEmailSender{cfg: cfg, verificationMode: verificationMode}
}
// SendVerificationEmail sends an email verification code or token to the user.
// In code mode, the email body contains the code directly.
// In token mode, the email body contains a verification link with the token as a query parameter.
func (s *SMTPEmailSender) SendVerificationEmail(ctx context.Context, to string, codeOrToken string) error {
subject := lang.Check(s.cfg.VerificationSubject, "Verify your email")
var body string
if s.verificationMode == EmailVerificationTokenMode {
body = fmt.Sprintf("Please verify your email by clicking the following link:\n\n%s?token=%s", s.cfg.VerificationURL, url.QueryEscape(codeOrToken))
} else {
body = fmt.Sprintf("Your email verification code is: %s", codeOrToken)
}
return s.send(to, subject, body)
}
// SendPasswordResetEmail sends a password reset link to the user.
// The token is appended as a query parameter to the configured PasswordResetURL.
func (s *SMTPEmailSender) SendPasswordResetEmail(ctx context.Context, to string, token string) error {
subject := lang.Check(s.cfg.PasswordResetSubject, "Reset your password")
body := fmt.Sprintf("To reset your password, click the following link:\n\n%s?token=%s", s.cfg.PasswordResetURL, url.QueryEscape(token))
return s.send(to, subject, body)
}
// SendTwoFactorCodeEmail sends a 2FA verification code to the user.
func (s *SMTPEmailSender) SendTwoFactorCodeEmail(ctx context.Context, to string, code string) error {
subject := lang.Check(s.cfg.TwoFactorCodeSubject, "Your verification code")
body := fmt.Sprintf("Your verification code is: %s", code)
return s.send(to, subject, body)
}
func (s *SMTPEmailSender) send(to, subject, body string) error {
addr := fmt.Sprintf("%s:%d", s.cfg.Host, s.cfg.Port)
auth := smtp.PlainAuth("", s.cfg.Username, s.cfg.Password, s.cfg.Host)
msg := fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=\"utf-8\"\r\n\r\n%s",
s.cfg.From, to, subject, body)
return smtp.SendMail(addr, auth, s.cfg.From, []string{to}, []byte(msg))
}
// ForgotPasswordRequest represents the request body for initiating a password reset.
type ForgotPasswordRequest struct {
Identifier string `json:"identifier"` // email address or username
}
// Validate checks if the ForgotPasswordRequest is valid.
func (req ForgotPasswordRequest) Validate() error {
if req.Identifier == "" {
return errors.New("identifier is required")
}
if strings.Contains(req.Identifier, "@") {
if _, err := mail.ParseAddress(req.Identifier); err != nil {
return errors.New("invalid email address format")
}
if strings.ContainsAny(req.Identifier, "\r\n") {
return errors.New("invalid email address format")
}
}
return nil
}
// ResetPasswordRequest represents the request body for completing a password reset.
type ResetPasswordRequest struct {
Token string `json:"token"`
Password string `json:"password"`
}
// Validate checks if the ResetPasswordRequest is valid.
func (req ResetPasswordRequest) Validate() error {
if req.Token == "" {
return errors.New("token is required")
}
if req.Password == "" {
return errors.New("password is required")
}
return nil
}
// verifyEmailRequest represents the request body for email verification.
// In code mode, both Code and Email fields are required.
// In token mode, only the Token field is required.
type verifyEmailRequest struct {
// Token is the verification token (token mode only). Format: "userID:randomHex".
Token string `json:"token,omitempty"`
// Code is the verification code (code mode only). E.g. "123456".
Code string `json:"code,omitempty"`
// Email is the user's email address (code mode only). Used to look up the user.
Email string `json:"email,omitempty"`
}
// validateForMode checks if the verifyEmailRequest has the required fields
// based on the configured verification mode.
func (req verifyEmailRequest) validateForMode(mode EmailVerificationMode) error {
if mode == EmailVerificationTokenMode {
if req.Token == "" {
return errors.New("token is required")
}
return nil
}
// Code mode
if req.Code == "" {
return errors.New("code is required")
}
if req.Email == "" {
return errors.New("email is required")
}
return nil
}
// generateEmailToken generates a random token for email verification (token mode) or password reset.
// It returns the raw token (userID:randomHex format), the bcrypt hash of the random portion,
// and any error encountered.
func generateEmailToken(userID string) (rawToken string, hash string, err error) {
randomPart, err := generateRandomHex(32)
if err != nil {
return "", "", fmt.Errorf("generating random token: %w", err)
}
hashBytes, err := bcrypt.GenerateFromPassword([]byte(randomPart), bcrypt.DefaultCost)
if err != nil {
return "", "", fmt.Errorf("hashing email token: %w", err)
}
return userID + ":" + randomPart, string(hashBytes), nil
}
// generateVerificationCode generates a verification code using the configured CodeGenerator,
// or falls back to the built-in numeric generator with the configured number of digits.
// Returns the plaintext code and its bcrypt hash.
func generateVerificationCode(cfg EmailVerificationConfig) (code string, hash string, err error) {
var rawCode string
if cfg.CodeGenerator != nil {
rawCode, err = cfg.CodeGenerator.Generate()
} else {
digits := lang.Check(cfg.CodeDigits, 6)
rawCode, err = generateNumericEmailCode(digits)
}
if err != nil {
return "", "", fmt.Errorf("generating verification code: %w", err)
}
hashBytes, err := bcrypt.GenerateFromPassword([]byte(rawCode), bcrypt.DefaultCost)
if err != nil {
return "", "", fmt.Errorf("hashing verification code: %w", err)
}
return rawCode, string(hashBytes), nil
}
// VerifyEmailHandler handles the HTTP request for email verification.
// In token mode: validates the token, marks the email as verified, and clears the token fields.
// In code mode: validates the code and email, looks up the user by email, and marks as verified.
func (h *AuthManager) VerifyEmailHandler(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
var req verifyEmailRequest
if err := ctx.ReadJSON(&req); err != nil {
ctx.BadRequest(err, "invalid request body")
return
}
mode := h.service.cfg.EmailVerification.Mode
if err := req.validateForMode(mode); err != nil {
ctx.BadRequest(err, err.Error())
return
}
if mode == EmailVerificationTokenMode {
h.verifyEmailByToken(ctx, r, req.Token)
} else {
h.verifyEmailByCode(ctx, r, req.Code, req.Email)
}
}
// verifyEmailByToken handles email verification using a long token (token mode).
func (h *AuthManager) verifyEmailByToken(ctx *Context, r *http.Request, token string) {
parts := strings.SplitN(token, ":", 2)
if len(parts) != 2 {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, "", false, map[string]any{
"reason": "invalid_token_format",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
userID := parts[0]
tokenPart := parts[1]
user, exists, err := h.service.db.FindByID(r.Context(), userID)
if err != nil {
ctx.InternalServerError(err, "failed to find user")
return
}
if !exists {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, userID, false, map[string]any{
"reason": "user_not_found",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
// Check token hash
if err := bcrypt.CompareHashAndPassword([]byte(user.EmailVerifyTokenHash), []byte(tokenPart)); err != nil {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, userID, false, map[string]any{
"reason": "token_mismatch",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
// Check expiry
if time.Now().After(user.EmailVerifyTokenExpiresAt) {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, userID, false, map[string]any{
"reason": "token_expired",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
h.markEmailVerified(ctx, r, userID, user.Email)
}
// verifyEmailByCode handles email verification using a short code (code mode).
func (h *AuthManager) verifyEmailByCode(ctx *Context, r *http.Request, code, email string) {
emailDB, ok := h.service.db.(EmailAuthDatabase)
if !ok {
ctx.InternalServerError(errors.New("database does not support email lookup"), "internal error")
return
}
user, found, err := emailDB.FindByEmail(r.Context(), email)
if err != nil {
ctx.InternalServerError(err, "failed to find user")
return
}
if !found {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, "", false, map[string]any{
"reason": "user_not_found",
"email": email,
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
// Check code hash (stored in EmailVerifyTokenHash)
if err := bcrypt.CompareHashAndPassword([]byte(user.EmailVerifyTokenHash), []byte(code)); err != nil {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, user.ID, false, map[string]any{
"reason": "code_mismatch",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
// Check expiry
if time.Now().After(user.EmailVerifyTokenExpiresAt) {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerifyFailed, r, user.ID, false, map[string]any{
"reason": "code_expired",
})
}
ctx.BadRequest(errInvalidVerifyToken, errInvalidVerifyToken.Error())
return
}
h.markEmailVerified(ctx, r, user.ID, user.Email)
}
// markEmailVerified marks the user's email as verified, clears the verification token/code fields,
// generates access and refresh tokens, and returns a login response so the user is auto-logged-in.
func (h *AuthManager) markEmailVerified(ctx *Context, r *http.Request, userID, email string) {
if err := h.service.db.UpdateUser(r.Context(), userID, &UserDiff{
EmailVerified: lang.Ptr(true),
EmailVerifyTokenHash: lang.Ptr(""),
EmailVerifyTokenExpiresAt: lang.Ptr(time.Time{}),
}); err != nil {
ctx.InternalServerError(err, "failed to update user")
return
}
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventEmailVerified, r, userID, true, map[string]any{
"email": email,
})
}
// Auto-login: generate tokens so the user doesn't have to log in manually after verification.
user, exists, err := h.service.db.FindByID(r.Context(), userID)
if err != nil || !exists {
ctx.InternalServerError(err, "failed to find user after verification")
return
}
accessToken, refreshToken, refreshTokenExpiresAt, err := h.service.generateTokens(r.Context(), user)
if err != nil {
ctx.InternalServerError(err, "failed to generate tokens after verification")
return
}
h.setAuthCookie(ctx, refreshToken, refreshTokenExpiresAt)
ctx.Response(http.StatusOK, UserLoginResponse{
AccessToken: accessToken,
ID: user.ID,
Username: user.Username,
Roles: user.Roles,
})
}
// ResendVerificationHandler handles the HTTP request for resending a verification email.
// It requires authentication and respects the configured resend cooldown.
// In code mode, it generates a new code. In token mode, it generates a new token.
func (h *AuthManager) ResendVerificationHandler(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
userID := getValueFromContext[string](r, UserContextKey{})
if userID == "" {
ctx.Unauthorized(errUnauthorized, "not authenticated")
return
}
user, exists, err := h.service.db.FindByID(r.Context(), userID)
if err != nil {
ctx.InternalServerError(err, "failed to find user")
return
}
if !exists {
ctx.Unauthorized(errUnauthorized, "user not found")
return
}
if user.EmailVerified {
ctx.BadRequest(errors.New("email already verified"), "email already verified")
return
}
if user.Email == "" {
ctx.BadRequest(errors.New("no email address"), "no email address on account")
return
}
// Check cooldown
if !user.EmailVerifyLastSentAt.IsZero() && time.Since(user.EmailVerifyLastSentAt) < h.service.cfg.EmailVerification.ResendCooldown {
ctx.TooManyRequests(errVerifyCooldown, errVerifyCooldown.Error())
return
}
cfg := h.service.cfg.EmailVerification
now := time.Now()
var sendValue string // code or token to send via email
var hashValue string // bcrypt hash to store
if cfg.Mode == EmailVerificationTokenMode {
rawToken, tokenHash, err := generateEmailToken(userID)
if err != nil {
ctx.InternalServerError(err, "failed to generate verification token")
return
}
sendValue = rawToken
hashValue = tokenHash
} else {
code, codeHash, err := generateVerificationCode(cfg)
if err != nil {
ctx.InternalServerError(err, "failed to generate verification code")
return
}
sendValue = code
hashValue = codeHash
}
var expiresAt time.Time
if cfg.Mode == EmailVerificationTokenMode {
expiresAt = now.Add(cfg.TokenDuration)
} else {
expiresAt = now.Add(cfg.CodeDuration)
}
if err := h.service.db.UpdateUser(r.Context(), userID, &UserDiff{
EmailVerifyTokenHash: lang.Ptr(hashValue),
EmailVerifyTokenExpiresAt: lang.Ptr(expiresAt),
EmailVerifyLastSentAt: lang.Ptr(now),
}); err != nil {
ctx.InternalServerError(err, "failed to update verification token")
return
}
// Send verification email
if cfg.Sender != nil {
if err := cfg.Sender.SendVerificationEmail(r.Context(), user.Email, sendValue); err != nil {
ctx.InternalServerError(err, "failed to send verification email")
return
}
}
ctx.Response(http.StatusOK, map[string]string{"message": "verification email sent"})
}
// ForgotPasswordHandler handles the HTTP request for initiating a password reset.
// It always returns 200 regardless of whether the user exists, to prevent user enumeration.
func (h *AuthManager) ForgotPasswordHandler(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
var req ForgotPasswordRequest
if err := ctx.ReadAndValidate(&req); err != nil {
ctx.BadRequest(err, "invalid request body")
return
}
const successMessage = "if the account exists, a reset link has been sent"
// Find user by email or username
var user User
var found bool
var findErr error
if strings.Contains(req.Identifier, "@") {
emailDB, ok := h.service.db.(EmailAuthDatabase)
if !ok {
// Should not happen if validated at startup, but handle gracefully
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
user, found, findErr = emailDB.FindByEmail(r.Context(), req.Identifier)
} else {
user, found, findErr = h.service.db.FindByUsername(r.Context(), req.Identifier)
}
if findErr != nil {
// Log error but return success to prevent enumeration
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, "", false, map[string]any{
"reason": "db_error",
"error": findErr.Error(),
})
}
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
if !found || user.Email == "" {
// No user or no email — still return success
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
// Enforce resend cooldown
if h.service.cfg.PasswordReset.ResendCooldown > 0 && !user.PasswordResetLastSentAt.IsZero() {
if time.Since(user.PasswordResetLastSentAt) < h.service.cfg.PasswordReset.ResendCooldown {
// Still within cooldown — return success to prevent enumeration
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
}
// Generate reset token
rawToken, tokenHash, err := generateEmailToken(user.ID)
if err != nil {
// Log error but return success
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, user.ID, false, map[string]any{
"reason": "token_generation_error",
})
}
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
now := time.Now()
expiresAt := now.Add(h.service.cfg.PasswordReset.TokenDuration)
if err := h.service.db.UpdateUser(r.Context(), user.ID, &UserDiff{
PasswordResetTokenHash: lang.Ptr(tokenHash),
PasswordResetTokenExpiresAt: lang.Ptr(expiresAt),
PasswordResetLastSentAt: lang.Ptr(now),
}); err != nil {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, user.ID, false, map[string]any{
"reason": "db_update_error",
})
}
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
return
}
// Send reset email
if h.service.cfg.PasswordReset.Sender != nil {
if err := h.service.cfg.PasswordReset.Sender.SendPasswordResetEmail(r.Context(), user.Email, rawToken); err != nil {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, user.ID, false, map[string]any{
"reason": "email_send_error",
})
}
}
}
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetReq, r, user.ID, true, map[string]any{
"email": user.Email,
})
}
ctx.Response(http.StatusOK, map[string]string{"message": successMessage})
}
// ResetPasswordHandler handles the HTTP request for completing a password reset.
// It validates the reset token, updates the password, and forces re-login by clearing the refresh token.
func (h *AuthManager) ResetPasswordHandler(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
var req ResetPasswordRequest
if err := ctx.ReadAndValidate(&req); err != nil {
ctx.BadRequest(err, "invalid request body")
return
}
if h.service.cfg.MinPasswordLength > 0 && len(req.Password) < h.service.cfg.MinPasswordLength {
ctx.BadRequest(errPasswordTooShort, fmt.Sprintf("password must be at least %d characters", h.service.cfg.MinPasswordLength))
return
}
if len(req.Password) > 128 {
ctx.BadRequest(nil, "password must be 128 characters or fewer")
return
}
parts := strings.SplitN(req.Token, ":", 2)
if len(parts) != 2 {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, "", false, map[string]any{
"reason": "invalid_token_format",
})
}
ctx.BadRequest(errInvalidResetToken, errInvalidResetToken.Error())
return
}
userID := parts[0]
tokenPart := parts[1]
user, exists, err := h.service.db.FindByID(r.Context(), userID)
if err != nil {
ctx.InternalServerError(err, "failed to find user")
return
}
if !exists {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, userID, false, map[string]any{
"reason": "user_not_found",
})
}
ctx.BadRequest(errInvalidResetToken, errInvalidResetToken.Error())
return
}
// Check token hash
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordResetTokenHash), []byte(tokenPart)); err != nil {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, userID, false, map[string]any{
"reason": "token_mismatch",
})
}
ctx.BadRequest(errInvalidResetToken, errInvalidResetToken.Error())
return
}
// Check expiry
if time.Now().After(user.PasswordResetTokenExpiresAt) {
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetFailed, r, userID, false, map[string]any{
"reason": "token_expired",
})
}
ctx.BadRequest(errInvalidResetToken, errInvalidResetToken.Error())
return
}
// Hash new password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
ctx.InternalServerError(err, "failed to hash password")
return
}
// Update password, clear reset token, clear refresh token (force re-login)
if err := h.service.db.UpdateUser(r.Context(), userID, &UserDiff{
PasswordHash: lang.Ptr(string(hashedPassword)),
PasswordResetTokenHash: lang.Ptr(""),
PasswordResetTokenExpiresAt: lang.Ptr(time.Time{}),
RefreshTokenHash: lang.Ptr(""),
RefreshTokenExpiresAt: lang.Ptr(time.Time{}),
}); err != nil {
ctx.InternalServerError(err, "failed to update password")
return
}
if h.auditLogger != nil {
h.auditLogger.LogAuthenticationEvent(AuditEventPasswordResetDone, r, userID, true, map[string]any{
"email": user.Email,
})
}
ctx.Response(http.StatusOK, map[string]string{"message": "password reset successfully"})
}