Bladeren bron

convert jwt duration to minutes

abhishek9686 4 maanden geleden
bovenliggende
commit
5c4ac120a6
3 gewijzigde bestanden met toevoegingen van 7 en 8 verwijderingen
  1. 0 2
      controllers/server.go
  2. 4 3
      logic/jwts.go
  3. 3 3
      logic/settings.go

+ 0 - 2
controllers/server.go

@@ -245,7 +245,6 @@ func getConfig(w http.ResponseWriter, r *http.Request) {
 func getSettings(w http.ResponseWriter, r *http.Request) {
 	scfg := logic.GetServerSettings()
 	scfg.ClientSecret = logic.Mask()
-	scfg.JwtValidityDuration = scfg.JwtValidityDuration / 60
 	logic.ReturnSuccessResponseWithJson(w, r, scfg, "fetched server settings successfully")
 }
 
@@ -266,7 +265,6 @@ func updateSettings(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("invalid settings"), "badrequest"))
 		return
 	}
-	req.JwtValidityDuration = req.JwtValidityDuration * 60
 	err := logic.UpsertServerSettings(req, force == "true")
 	if err != nil {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failed to udpate server settings "+err.Error()), "internal"))

+ 4 - 3
logic/jwts.go

@@ -4,11 +4,12 @@ import (
 	"context"
 	"errors"
 	"fmt"
-	"github.com/gravitl/netmaker/db"
-	"github.com/gravitl/netmaker/schema"
 	"strings"
 	"time"
 
+	"github.com/gravitl/netmaker/db"
+	"github.com/gravitl/netmaker/schema"
+
 	"github.com/golang-jwt/jwt/v4"
 
 	"github.com/gravitl/netmaker/logger"
@@ -83,7 +84,7 @@ func CreateUserAccessJwtToken(username string, role models.UserRoleID, d time.Ti
 // CreateUserJWT - creates a user jwt token
 func CreateUserJWT(username string, role models.UserRoleID) (response string, err error) {
 	settings := GetServerSettings()
-	expirationTime := time.Now().Add(time.Duration(settings.JwtValidityDuration) * time.Second)
+	expirationTime := time.Now().Add(time.Duration(settings.JwtValidityDuration) * time.Minute)
 	claims := &models.UserClaims{
 		UserName:       username,
 		Role:           role,

+ 3 - 3
logic/settings.go

@@ -82,7 +82,7 @@ func GetServerSettingsFromEnv() (s models.ServerSettings) {
 		AzureTenant:                servercfg.GetAzureTenant(),
 		Telemetry:                  servercfg.Telemetry(),
 		BasicAuth:                  servercfg.IsBasicAuthEnabled(),
-		JwtValidityDuration:        servercfg.GetJwtValidityDurationFromEnv(),
+		JwtValidityDuration:        servercfg.GetJwtValidityDurationFromEnv() / 60,
 		RacAutoDisable:             servercfg.GetRacAutoDisable(),
 		RacRestrictToSingleNetwork: servercfg.GetRacRestrictToSingleNetwork(),
 		EndpointDetection:          servercfg.IsEndpointDetectionEnabled(),
@@ -160,7 +160,7 @@ func GetServerConfig() config.ServerConfig {
 	if servercfg.IsPro {
 		cfg.IsPro = "yes"
 	}
-	cfg.JwtValidityDuration = time.Duration(settings.JwtValidityDuration) * time.Second
+	cfg.JwtValidityDuration = time.Duration(settings.JwtValidityDuration) * time.Minute
 	cfg.RacAutoDisable = settings.RacAutoDisable
 	cfg.RacRestrictToSingleNetwork = settings.RacRestrictToSingleNetwork
 	cfg.MetricInterval = settings.MetricInterval
@@ -222,7 +222,7 @@ func Telemetry() string {
 	return GetServerSettings().Telemetry
 }
 
-// GetJwtValidityDuration - returns the JWT validity duration in seconds
+// GetJwtValidityDuration - returns the JWT validity duration in minutes
 func GetJwtValidityDuration() time.Duration {
 	return GetServerConfig().JwtValidityDuration
 }