From 90935ac2739f9309678dce514f2544692f6baaab Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Tue, 19 Mar 2024 11:47:28 +0700 Subject: [PATCH] fix: invalidate email, phone OTPs on password change --- internal/models/user.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/models/user.go b/internal/models/user.go index 105c57a9cd..be70b13baa 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -301,7 +301,20 @@ func (u *User) SetPassword(ctx context.Context, password string) error { // UpdatePassword updates the user's password. Use SetPassword outside of a transaction first! func (u *User) UpdatePassword(tx *storage.Connection, sessionID *uuid.UUID) error { - if err := tx.UpdateOnly(u, "encrypted_password"); err != nil { + // These need to be reset because password change may mean the user no longer trusts the actions performed by the previous password. + u.ConfirmationToken = "" + u.ConfirmationSentAt = nil + u.RecoveryToken = "" + u.RecoverySentAt = nil + u.EmailChangeTokenCurrent = "" + u.EmailChangeTokenNew = "" + u.EmailChangeSentAt = nil + u.PhoneChangeToken = "" + u.PhoneChangeSentAt = nil + u.ReauthenticationToken = "" + u.ReauthenticationSentAt = nil + + if err := tx.UpdateOnly(u, "encrypted_password", "confirmation_token", "confirmation_sent_at", "recovery_token", "recovery_sent_at", "email_change_token_current", "email_change_token_new", "email_change_sent_at", "phone_change_token", "phone_change_sent_at", "reauthentication_token", "reauthentication_sent_at"); err != nil { return err }