Skip to content

Commit

Permalink
fix session key
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza0 committed May 23, 2024
1 parent 55a6d78 commit 4900c14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 5 additions & 5 deletions backend/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var defaultValueMap = map[string]string{
"webListen": "",
"webDomain": "",
"webPort": "2095",
"webSecret": common.Random(32),
"secret": common.Random(32),
"webCertFile": "",
"webKeyFile": "",
"webPath": "/app/",
Expand Down Expand Up @@ -191,11 +191,11 @@ func (s *SettingService) SetWebPath(webPath string) error {
}

func (s *SettingService) GetSecret() ([]byte, error) {
secret, err := s.getString("webSecret")
if secret == defaultValueMap["webSecret"] {
err := s.saveSetting("webSecret", secret)
secret, err := s.getString("secret")
if secret == defaultValueMap["secret"] {
err := s.saveSetting("secret", secret)
if err != nil {
logger.Warning("save webSecret failed:", err)
logger.Warning("save secret failed:", err)
}
}
return []byte(secret), err
Expand Down
19 changes: 16 additions & 3 deletions backend/util/common/random.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package common

import "math/rand"
import (
"math/rand"
"time"
)

var allSeq [62]rune
var (
allSeq []rune
rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
)

func init() {
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for _, char := range chars {
allSeq = append(allSeq, char)
}
}

func Random(n int) string {
runes := make([]rune, n)
for i := 0; i < n; i++ {
runes[i] = allSeq[rand.Intn(len(allSeq))]
runes[i] = allSeq[rnd.Intn(len(allSeq))]
}
return string(runes)
}

0 comments on commit 4900c14

Please sign in to comment.