mirror of https://github.com/gogs/gogs.git
refactor(db): migrate helpers off `user_cache.go` (#7214)
parent
49be63abbf
commit
131be6e074
|
@ -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)
|
|
||||||
}
|
|
|
@ -235,12 +235,12 @@ func LoginTwoFactorPost(c *context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent same passcode from being reused
|
// 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.Flash.Error(c.Tr("settings.two_factor_reused_passcode"))
|
||||||
c.RedirectSubpath("/user/login/two_factor")
|
c.RedirectSubpath("/user/login/two_factor")
|
||||||
return
|
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)
|
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.Data["Hours"] = conf.Auth.ActivateCodeLives / 60
|
||||||
c.Success(ACTIVATE)
|
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)
|
log.Error("Failed to put cache key 'mail resend': %v", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -393,13 +393,13 @@ func Activate(c *context.Context) {
|
||||||
}
|
}
|
||||||
// Resend confirmation email.
|
// Resend confirmation email.
|
||||||
if conf.Auth.RequireEmailConfirmation {
|
if conf.Auth.RequireEmailConfirmation {
|
||||||
if c.Cache.IsExist(c.User.MailResendCacheKey()) {
|
if c.Cache.IsExist(userutil.MailResendCacheKey(c.User.ID)) {
|
||||||
c.Data["ResendLimited"] = true
|
c.Data["ResendLimited"] = true
|
||||||
} else {
|
} else {
|
||||||
c.Data["Hours"] = conf.Auth.ActivateCodeLives / 60
|
c.Data["Hours"] = conf.Auth.ActivateCodeLives / 60
|
||||||
email.SendActivateAccountMail(c.Context, db.NewMailerUser(c.User))
|
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)
|
log.Error("Failed to put cache key 'mail resend': %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -496,14 +496,14 @@ func ForgotPasswdPost(c *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Cache.IsExist(u.MailResendCacheKey()) {
|
if c.Cache.IsExist(userutil.MailResendCacheKey(u.ID)) {
|
||||||
c.Data["ResendLimited"] = true
|
c.Data["ResendLimited"] = true
|
||||||
c.Success(FORGOT_PASSWORD)
|
c.Success(FORGOT_PASSWORD)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
email.SendResetPasswordMail(c.Context, db.NewMailerUser(u))
|
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)
|
log.Error("Failed to put cache key 'mail resend': %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -122,3 +122,13 @@ func ValidatePassword(encoded, salt, password string) bool {
|
||||||
got := EncodePassword(password, salt)
|
got := EncodePassword(password, salt)
|
||||||
return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue