diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/db/user_cache.go | 20 | ||||
-rw-r--r-- | internal/route/user/auth.go | 14 | ||||
-rw-r--r-- | internal/userutil/userutil.go | 10 | ||||
-rw-r--r-- | internal/userutil/userutil_test.go | 10 |
4 files changed, 27 insertions, 27 deletions
diff --git a/internal/db/user_cache.go b/internal/db/user_cache.go deleted file mode 100644 index f4fa8172..00000000 --- a/internal/db/user_cache.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2018 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package db - -import ( - "fmt" -) - -// MailResendCacheKey returns key used for cache mail resend. -func (u *User) MailResendCacheKey() string { - return fmt.Sprintf("MailResend_%d", u.ID) -} - -// TwoFactorCacheKey returns key used for cache two factor passcode. -// e.g. TwoFactor_1_012664 -func (u *User) TwoFactorCacheKey(passcode string) string { - return fmt.Sprintf("TwoFactor_%d_%s", u.ID, passcode) -} diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go index a893f8b5..cf6fa5e3 100644 --- a/internal/route/user/auth.go +++ b/internal/route/user/auth.go @@ -235,12 +235,12 @@ func LoginTwoFactorPost(c *context.Context) { } // Prevent same passcode from being reused - if c.Cache.IsExist(u.TwoFactorCacheKey(passcode)) { + if c.Cache.IsExist(userutil.TwoFactorCacheKey(u.ID, passcode)) { c.Flash.Error(c.Tr("settings.two_factor_reused_passcode")) c.RedirectSubpath("/user/login/two_factor") return } - if err = c.Cache.Put(u.TwoFactorCacheKey(passcode), 1, 60); err != nil { + if err = c.Cache.Put(userutil.TwoFactorCacheKey(u.ID, passcode), 1, 60); err != nil { log.Error("Failed to put cache 'two factor passcode': %v", err) } @@ -374,7 +374,7 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) { c.Data["Hours"] = conf.Auth.ActivateCodeLives / 60 c.Success(ACTIVATE) - if err := c.Cache.Put(u.MailResendCacheKey(), 1, 180); err != nil { + if err := c.Cache.Put(userutil.MailResendCacheKey(u.ID), 1, 180); err != nil { log.Error("Failed to put cache key 'mail resend': %v", err) } return @@ -393,13 +393,13 @@ func Activate(c *context.Context) { } // Resend confirmation email. if conf.Auth.RequireEmailConfirmation { - if c.Cache.IsExist(c.User.MailResendCacheKey()) { + if c.Cache.IsExist(userutil.MailResendCacheKey(c.User.ID)) { c.Data["ResendLimited"] = true } else { c.Data["Hours"] = conf.Auth.ActivateCodeLives / 60 email.SendActivateAccountMail(c.Context, db.NewMailerUser(c.User)) - if err := c.Cache.Put(c.User.MailResendCacheKey(), 1, 180); err != nil { + if err := c.Cache.Put(userutil.MailResendCacheKey(c.User.ID), 1, 180); err != nil { log.Error("Failed to put cache key 'mail resend': %v", err) } } @@ -496,14 +496,14 @@ func ForgotPasswdPost(c *context.Context) { return } - if c.Cache.IsExist(u.MailResendCacheKey()) { + if c.Cache.IsExist(userutil.MailResendCacheKey(u.ID)) { c.Data["ResendLimited"] = true c.Success(FORGOT_PASSWORD) return } email.SendResetPasswordMail(c.Context, db.NewMailerUser(u)) - if err = c.Cache.Put(u.MailResendCacheKey(), 1, 180); err != nil { + if err = c.Cache.Put(userutil.MailResendCacheKey(u.ID), 1, 180); err != nil { log.Error("Failed to put cache key 'mail resend': %v", err) } diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go index d8a8b031..6d324ff6 100644 --- a/internal/userutil/userutil.go +++ b/internal/userutil/userutil.go @@ -122,3 +122,13 @@ func ValidatePassword(encoded, salt, password string) bool { got := EncodePassword(password, salt) return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1 } + +// MailResendCacheKey returns the key used for caching mail resend. +func MailResendCacheKey(userID int64) string { + return fmt.Sprintf("mailResend::%d", userID) +} + +// TwoFactorCacheKey returns the key used for caching two factor passcode. +func TwoFactorCacheKey(userID int64, passcode string) string { + return fmt.Sprintf("twoFactor::%d::%s", userID, passcode) +} diff --git a/internal/userutil/userutil_test.go b/internal/userutil/userutil_test.go index 895f7e26..e46546bd 100644 --- a/internal/userutil/userutil_test.go +++ b/internal/userutil/userutil_test.go @@ -181,3 +181,13 @@ func TestValidatePassword(t *testing.T) { }) } } + +func TestMailResendCacheKey(t *testing.T) { + got := MailResendCacheKey(1) + assert.Equal(t, "mailResend::1", got) +} + +func TestTwoFactorCacheKey(t *testing.T) { + got := TwoFactorCacheKey(1, "113654") + assert.Equal(t, "twoFactor::1::113654", got) +} |