mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 05:36:19 +01:00
Use random bytes to generate access token (#21959)
This commit is contained in:
parent
9607750b5e
commit
f047ee0a40
1 changed files with 6 additions and 3 deletions
|
@ -6,16 +6,15 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/modules/base"
|
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
gouuid "github.com/google/uuid"
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,8 +99,12 @@ func NewAccessToken(t *AccessToken) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
token, err := util.CryptoRandomBytes(20)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
t.TokenSalt = salt
|
t.TokenSalt = salt
|
||||||
t.Token = base.EncodeSha1(gouuid.New().String())
|
t.Token = hex.EncodeToString(token)
|
||||||
t.TokenHash = HashToken(t.Token, t.TokenSalt)
|
t.TokenHash = HashToken(t.Token, t.TokenSalt)
|
||||||
t.TokenLastEight = t.Token[len(t.Token)-8:]
|
t.TokenLastEight = t.Token[len(t.Token)-8:]
|
||||||
_, err = db.GetEngine(db.DefaultContext).Insert(t)
|
_, err = db.GetEngine(db.DefaultContext).Insert(t)
|
||||||
|
|
Loading…
Reference in a new issue