diff options
author | silverwind <me@silverwind.io> | 2021-05-10 08:45:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 07:45:17 +0100 |
commit | 1e6fa57acbe3c05cb996b789e8c2d381c953826f (patch) | |
tree | c4f1ce55b3423f97952b630462cef5b2035961ec /models | |
parent | 270aab429ef025df9a0b9bf9e3982729ae8df449 (diff) | |
download | gitea-1e6fa57acbe3c05cb996b789e8c2d381c953826f.tar.gz gitea-1e6fa57acbe3c05cb996b789e8c2d381c953826f.zip |
Use single shared random string generation function (#15741)
* Use single shared random string generation function
- Replace 3 functions that do the same with 1 shared one
- Use crypto/rand over math/rand for a stronger RNG
- Output only alphanumerical for URL compatibilty
Fixes: #15536
* use const string method
* Update modules/avatar/avatar.go
Co-authored-by: a1012112796 <1012112796@qq.com>
Co-authored-by: a1012112796 <1012112796@qq.com>
Diffstat (limited to 'models')
-rw-r--r-- | models/migrations/v71.go | 4 | ||||
-rw-r--r-- | models/migrations/v85.go | 4 | ||||
-rw-r--r-- | models/token.go | 4 | ||||
-rw-r--r-- | models/twofactor.go | 6 | ||||
-rw-r--r-- | models/user.go | 3 |
5 files changed, 10 insertions, 11 deletions
diff --git a/models/migrations/v71.go b/models/migrations/v71.go index 3012dd94f5..e4ed46a21a 100644 --- a/models/migrations/v71.go +++ b/models/migrations/v71.go @@ -8,8 +8,8 @@ import ( "crypto/sha256" "fmt" - "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "golang.org/x/crypto/pbkdf2" "xorm.io/xorm" @@ -53,7 +53,7 @@ func addScratchHash(x *xorm.Engine) error { for _, tfa := range tfas { // generate salt - salt, err := generate.GetRandomString(10) + salt, err := util.RandomString(10) if err != nil { return err } diff --git a/models/migrations/v85.go b/models/migrations/v85.go index 8c92f10b6e..bdbcebeb00 100644 --- a/models/migrations/v85.go +++ b/models/migrations/v85.go @@ -7,9 +7,9 @@ package migrations import ( "fmt" - "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/xorm" ) @@ -65,7 +65,7 @@ func hashAppToken(x *xorm.Engine) error { for _, token := range tokens { // generate salt - salt, err := generate.GetRandomString(10) + salt, err := util.RandomString(10) if err != nil { return err } diff --git a/models/token.go b/models/token.go index 1245098df0..4737dddda3 100644 --- a/models/token.go +++ b/models/token.go @@ -10,8 +10,8 @@ import ( "time" "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" gouuid "github.com/google/uuid" ) @@ -40,7 +40,7 @@ func (t *AccessToken) AfterLoad() { // NewAccessToken creates new access token. func NewAccessToken(t *AccessToken) error { - salt, err := generate.GetRandomString(10) + salt, err := util.RandomString(10) if err != nil { return err } diff --git a/models/twofactor.go b/models/twofactor.go index a84da8cdb5..c19c5d120f 100644 --- a/models/twofactor.go +++ b/models/twofactor.go @@ -11,10 +11,10 @@ import ( "encoding/base64" "fmt" - "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "github.com/pquerna/otp/totp" "golang.org/x/crypto/pbkdf2" @@ -34,11 +34,11 @@ type TwoFactor struct { // GenerateScratchToken recreates the scratch token the user is using. func (t *TwoFactor) GenerateScratchToken() (string, error) { - token, err := generate.GetRandomString(8) + token, err := util.RandomString(8) if err != nil { return "", err } - t.ScratchSalt, _ = generate.GetRandomString(10) + t.ScratchSalt, _ = util.RandomString(10) t.ScratchHash = hashToken(token, t.ScratchSalt) return token, nil } diff --git a/models/user.go b/models/user.go index 26cfc0804e..02ccfea47f 100644 --- a/models/user.go +++ b/models/user.go @@ -22,7 +22,6 @@ import ( "unicode/utf8" "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -746,7 +745,7 @@ func IsUserExist(uid int64, name string) (bool, error) { // GetUserSalt returns a random user salt token. func GetUserSalt() (string, error) { - return generate.GetRandomString(10) + return util.RandomString(10) } // NewGhostUser creates and returns a fake user for someone has deleted his/her account. |