Skip to content

Commit

Permalink
add Mutex to protect jwtKey random generation
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchennn committed Jul 13, 2023
1 parent 3e019d9 commit 7d6542e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"reflect"
"strconv"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -38,6 +40,11 @@ const (
msisdnSupiMapColl = "subscriptionData.msisdnSupiMap"
)

var (
jwtKey = "" // for generating JWT
mu *sync.Mutex
)

var httpsClient *http.Client

func init() {
Expand All @@ -46,6 +53,7 @@ func init() {
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
mu = new(sync.Mutex)
}

func mapToByte(data map[string]interface{}) (ret []byte) {
Expand Down Expand Up @@ -400,11 +408,19 @@ func JWT(email, userId, tenantId string) string {
claims["email"] = email
claims["tenantId"] = tenantId

tokenString, err := token.SignedString([]byte(""))
mu.Lock()
if jwtKey == "" {
rand.Seed(time.Now().UnixNano())
jwtKey = strconv.Itoa(rand.Intn(2 << 32))
}
mu.Unlock()

tokenString, err := token.SignedString([]byte(jwtKey))
if err != nil {
logger.ProcLog.Errorf("JWT err: %+v", err)
}

fmt.Println("tokenString: ", tokenString)
return tokenString
}

Expand Down Expand Up @@ -491,7 +507,7 @@ type AuthSub struct {
// Parse JWT
func ParseJWT(tokenStr string) (jwt.MapClaims, error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
return []byte(""), nil
return []byte(uniqKey), nil
})
if err != nil {
return nil, errors.Wrap(err, "ParseJWT error")
Expand Down

0 comments on commit 7d6542e

Please sign in to comment.