From 131be6e074039e590488892d7a99ebdbe6eb4668 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Mon, 24 Oct 2022 23:45:31 +0800 Subject: refactor(db): migrate helpers off `user_cache.go` (#7214) --- internal/userutil/userutil.go | 10 ++++++++++ internal/userutil/userutil_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+) (limited to 'internal/userutil') 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) +} -- cgit v1.2.3