Skip to content

Commit

Permalink
move JWT private key generation to init()
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchennn committed Jul 17, 2023
1 parent 387053f commit f60dc9a
Show file tree
Hide file tree
Showing 3 changed files with 1,908 additions and 2,309 deletions.
30 changes: 9 additions & 21 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -40,10 +39,7 @@ const (
msisdnSupiMapColl = "subscriptionData.msisdnSupiMap"
)

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

var httpsClient *http.Client

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

randomBytes := make([]byte, 256)
_, err := rand.Read(randomBytes)
if err != nil {
logger.ProcLog.Warnln("Generate JWT Private Key error.")
} else {
jwtKey = string(randomBytes)
}
}

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

mu.Lock()

if jwtKey == "" {
for i := 0; i < 256; i++ {
randomBytes := make([]byte, 128)
_, err := rand.Read(randomBytes)
if err != nil {
logger.ProcLog.Warnln("Generate JWT Private Key error.")
} else {
jwtKey = string(randomBytes)
}
}
}
mu.Unlock()

tokenString, err := token.SignedString([]byte(jwtKey))
if err != nil {
logger.ProcLog.Errorf("JWT err: %+v", err)
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.2",
"autoprefixer": "7.1.2",
"axios": "latest",
"axios": "^1.4.0",
"babel-core": "6.26.3",
"babel-eslint": "10.1.0",
"babel-jest": "20.0.3",
Expand Down
Loading

0 comments on commit f60dc9a

Please sign in to comment.