Update random id generation algorithm

This commit is contained in:
r 2020-01-26 07:45:02 +00:00
parent bf2cfaf0ed
commit c9842c65b4
3 changed files with 23 additions and 9 deletions

View File

@ -69,7 +69,10 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
s.CSRFToken = util.NewCSRFToken() s.CSRFToken, err = util.NewCSRFToken()
if err != nil {
log.Fatal(err)
}
err = sessionRepo.Add(s) err = sessionRepo.Add(s)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -106,8 +106,14 @@ func (svc *service) GetAuthUrl(ctx context.Context, instance string) (
instanceURL = "https://" + instance instanceURL = "https://" + instance
} }
sessionID = util.NewSessionId() sessionID, err = util.NewSessionId()
csrfToken := util.NewCSRFToken() if err != nil {
return
}
csrfToken, err := util.NewCSRFToken()
if err != nil {
return
}
session := model.Session{ session := model.Session{
ID: sessionID, ID: sessionID,
InstanceDomain: instance, InstanceDomain: instance,

View File

@ -1,7 +1,8 @@
package util package util
import ( import (
"math/rand" "crypto/rand"
"math/big"
) )
var ( var (
@ -9,18 +10,22 @@ var (
runes_length = len(runes) runes_length = len(runes)
) )
func NewRandId(n int) string { func NewRandId(n int) (string, error) {
data := make([]rune, n) data := make([]rune, n)
for i := range data { for i := range data {
data[i] = runes[rand.Intn(runes_length)] num, err := rand.Int(rand.Reader, big.NewInt(int64(runes_length)))
if err != nil {
return "", err
} }
return string(data) data[i] = runes[num.Int64()]
}
return string(data), nil
} }
func NewSessionId() string { func NewSessionId() (string, error) {
return NewRandId(24) return NewRandId(24)
} }
func NewCSRFToken() string { func NewCSRFToken() (string, error) {
return NewRandId(24) return NewRandId(24)
} }