feat: rename jwt to session & introduce upload keys
This commit is contained in:
parent
3fc792fd53
commit
3b02f4931e
7 changed files with 98 additions and 15 deletions
52
internal/auth/ukey/ukey.go
Normal file
52
internal/auth/ukey/ukey.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package ukey
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
|
||||
"github.com/cristalhq/base64"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"stereo.cat/backend/internal/auth"
|
||||
"stereo.cat/backend/internal/types"
|
||||
)
|
||||
|
||||
func GenerateUploadKey(cfg *types.StereoConfig, user *auth.User, c *gin.Context) []byte {
|
||||
length := 32
|
||||
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789@#!&*%~?"
|
||||
|
||||
key := make([]byte, length)
|
||||
for i := range length {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
|
||||
if err != nil {
|
||||
types.ErrorInvalidParams.Throw(c, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
key[i] = chars[num.Int64()]
|
||||
}
|
||||
|
||||
hasher, err := blake2b.New512(nil)
|
||||
if err != nil {
|
||||
types.ErrorInvalidParams.Throw(c, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = hasher.Write(key)
|
||||
if err != nil {
|
||||
types.ErrorInvalidParams.Throw(c, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
hashed := base64.RawStdEncoding.EncodeToString(hasher.Sum(nil))
|
||||
|
||||
user.HashedApiKey = hashed
|
||||
|
||||
err = cfg.Database.Updates(user).Error
|
||||
if err != nil {
|
||||
types.ErrorDatabase.Throw(c, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue