diff options
author | Joe Chen <jc@unknwon.io> | 2022-11-05 17:55:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-05 17:55:05 +0800 |
commit | fd798b4197dc53df872d20f5d10edb8d73c32386 (patch) | |
tree | e59c8e5881f2162e211850ede549a1f57df64515 /internal/userutil | |
parent | a66c90462da24a916ee62afcb5a1f79d06ed8399 (diff) |
refactor(db): migrate methods off `user.go` (#7228)
Diffstat (limited to 'internal/userutil')
-rw-r--r-- | internal/userutil/userutil.go | 7 | ||||
-rw-r--r-- | internal/userutil/userutil_test.go | 8 |
2 files changed, 15 insertions, 0 deletions
diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go index 6d324ff6..bafdaf58 100644 --- a/internal/userutil/userutil.go +++ b/internal/userutil/userutil.go @@ -23,6 +23,7 @@ import ( "gogs.io/gogs/internal/avatar" "gogs.io/gogs/internal/conf" + "gogs.io/gogs/internal/strutil" "gogs.io/gogs/internal/tool" ) @@ -132,3 +133,9 @@ func MailResendCacheKey(userID int64) string { func TwoFactorCacheKey(userID int64, passcode string) string { return fmt.Sprintf("twoFactor::%d::%s", userID, passcode) } + +// RandomSalt returns randomly generated 10-character string that can be used as +// the user salt. +func RandomSalt() (string, error) { + return strutil.RandomChars(10) +} diff --git a/internal/userutil/userutil_test.go b/internal/userutil/userutil_test.go index e46546bd..6cdd6dd3 100644 --- a/internal/userutil/userutil_test.go +++ b/internal/userutil/userutil_test.go @@ -191,3 +191,11 @@ func TestTwoFactorCacheKey(t *testing.T) { got := TwoFactorCacheKey(1, "113654") assert.Equal(t, "twoFactor::1::113654", got) } + +func TestRandomSalt(t *testing.T) { + salt1, err := RandomSalt() + require.NoError(t, err) + salt2, err := RandomSalt() + require.NoError(t, err) + assert.NotEqual(t, salt1, salt2) +} |