Bläddra i källkod

fix server settings types

abhishek9686 5 månader sedan
förälder
incheckning
a535539040
5 ändrade filer med 58 tillägg och 32 borttagningar
  1. 1 1
      controllers/server.go
  2. 4 3
      logic/jwts.go
  3. 6 2
      logic/settings.go
  4. 35 26
      models/settings.go
  5. 12 0
      servercfg/serverconf.go

+ 1 - 1
controllers/server.go

@@ -45,7 +45,7 @@ func serverHandlers(r *mux.Router) {
 		Methods(http.MethodGet)
 	r.HandleFunc("/api/server/settings", allowUsers(http.HandlerFunc(getSettings))).
 		Methods(http.MethodGet)
-	r.HandleFunc("/api/server/settings", allowUsers(http.HandlerFunc(updateSettings))).
+	r.HandleFunc("/api/server/settings", logic.SecurityCheck(true, http.HandlerFunc(updateSettings))).
 		Methods(http.MethodPut)
 	r.HandleFunc("/api/server/getserverinfo", logic.SecurityCheck(true, http.HandlerFunc(getServerInfo))).
 		Methods(http.MethodGet)

+ 4 - 3
logic/jwts.go

@@ -59,7 +59,7 @@ func CreateUserAccessJwtToken(username string, role models.UserRoleID, d time.Ti
 		Role:           role,
 		TokenType:      models.AccessTokenType,
 		Api:            servercfg.GetAPIHost(),
-		RacAutoDisable: servercfg.GetRacAutoDisable() && (role != models.SuperAdminRole && role != models.AdminRole),
+		RacAutoDisable: GetRacAutoDisable() && (role != models.SuperAdminRole && role != models.AdminRole),
 		RegisteredClaims: jwt.RegisteredClaims{
 			Issuer:    "Netmaker",
 			Subject:   fmt.Sprintf("user|%s", username),
@@ -79,12 +79,13 @@ 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) {
-	expirationTime := time.Now().Add(GetJwtValidityDuration())
+	settings := GetServerSettings()
+	expirationTime := time.Now().Add(time.Duration(settings.JwtValidityDuration) * time.Second)
 	claims := &models.UserClaims{
 		UserName:       username,
 		Role:           role,
 		TokenType:      models.UserIDTokenType,
-		RacAutoDisable: servercfg.GetRacAutoDisable() && (role != models.SuperAdminRole && role != models.AdminRole),
+		RacAutoDisable: settings.RacAutoDisable && (role != models.SuperAdminRole && role != models.AdminRole),
 		RegisteredClaims: jwt.RegisteredClaims{
 			Issuer:    "Netmaker",
 			Subject:   fmt.Sprintf("user|%s", username),

+ 6 - 2
logic/settings.go

@@ -38,6 +38,7 @@ func UpsertServerSettings(s models.ServerSettings) error {
 }
 
 func ValidateNewSettings(req models.ServerSettings) bool {
+
 	return true
 }
 
@@ -53,7 +54,7 @@ func GetServerSettingsFromEnv() (s models.ServerSettings) {
 		AzureTenant:                servercfg.GetAzureTenant(),
 		Telemetry:                  servercfg.Telemetry(),
 		BasicAuth:                  servercfg.IsBasicAuthEnabled(),
-		JwtValidityDuration:        servercfg.GetJwtValidityDuration(),
+		JwtValidityDuration:        servercfg.GetJwtValidityDurationFromEnv(),
 		RacAutoDisable:             servercfg.GetRacAutoDisable(),
 		RacRestrictToSingleNetwork: servercfg.GetRacRestrictToSingleNetwork(),
 		EndpointDetection:          servercfg.IsEndpointDetectionEnabled(),
@@ -69,6 +70,9 @@ func GetServerSettingsFromEnv() (s models.ServerSettings) {
 		DefaultDomain:              servercfg.GetDefaultDomain(),
 		Stun:                       servercfg.IsStunEnabled(),
 		StunServers:                servercfg.GetStunServers(),
+		TextSize:                   "16",
+		Theme:                      models.Dark,
+		ReducedMotion:              false,
 	}
 
 	return
@@ -128,7 +132,7 @@ func GetServerConfig() config.ServerConfig {
 	if servercfg.IsPro {
 		cfg.IsPro = "yes"
 	}
-	cfg.JwtValidityDuration = settings.JwtValidityDuration
+	cfg.JwtValidityDuration = time.Duration(settings.JwtValidityDuration) * time.Second
 	cfg.RacAutoDisable = settings.RacAutoDisable
 	cfg.RacRestrictToSingleNetwork = settings.RacRestrictToSingleNetwork
 	cfg.MetricInterval = settings.MetricInterval

+ 35 - 26
models/settings.go

@@ -1,31 +1,40 @@
 package models
 
-import "time"
+type Theme string
+
+const (
+	Dark   Theme = "dark"
+	Light  Theme = "light"
+	System Theme = "system"
+)
 
 type ServerSettings struct {
-	NetclientAutoUpdate        bool          `json:"netclientautoupdate"`
-	Verbosity                  int32         `json:"verbosity"`
-	AuthProvider               string        `json:"authprovider"`
-	OIDCIssuer                 string        `json:"oidcissuer"`
-	ClientID                   string        `json:"client_id"`
-	ClientSecret               string        `json:"client_secret"`
-	AzureTenant                string        `json:"azure_tenant"`
-	Telemetry                  string        `json:"telemetry"`
-	BasicAuth                  bool          `json:"basic_auth"`
-	JwtValidityDuration        time.Duration `json:"jwt_validity_duration" swaggertype:"primitive,integer" format:"int64"`
-	RacAutoDisable             bool          `json:"rac_auto_disable"`
-	RacRestrictToSingleNetwork bool          `json:"rac_restrict_to_single_network"`
-	EndpointDetection          bool          `json:"endpoint_detection"`
-	AllowedEmailDomains        string        `json:"allowed_email_domains"`
-	EmailSenderAddr            string        `json:"email_sender_addr"`
-	EmailSenderUser            string        `json:"email_sender_user"`
-	EmailSenderPassword        string        `json:"email_sender_password"`
-	SmtpHost                   string        `json:"smtp_host"`
-	SmtpPort                   int           `json:"smtp_port"`
-	MetricInterval             string        `json:"metric_interval"`
-	MetricsPort                int           `json:"metrics_port"`
-	ManageDNS                  bool          `json:"manage_dns"`
-	DefaultDomain              string        `json:"default_domain"`
-	Stun                       bool          `json:"stun"`
-	StunServers                string        `json:"stun_servers"`
+	NetclientAutoUpdate        bool   `json:"netclientautoupdate"`
+	Verbosity                  int32  `json:"verbosity"`
+	AuthProvider               string `json:"authprovider"`
+	OIDCIssuer                 string `json:"oidcissuer"`
+	ClientID                   string `json:"client_id"`
+	ClientSecret               string `json:"client_secret"`
+	AzureTenant                string `json:"azure_tenant"`
+	Telemetry                  string `json:"telemetry"`
+	BasicAuth                  bool   `json:"basic_auth"`
+	JwtValidityDuration        int    `json:"jwt_validity_duration"`
+	RacAutoDisable             bool   `json:"rac_auto_disable"`
+	RacRestrictToSingleNetwork bool   `json:"rac_restrict_to_single_network"`
+	EndpointDetection          bool   `json:"endpoint_detection"`
+	AllowedEmailDomains        string `json:"allowed_email_domains"`
+	EmailSenderAddr            string `json:"email_sender_addr"`
+	EmailSenderUser            string `json:"email_sender_user"`
+	EmailSenderPassword        string `json:"email_sender_password"`
+	SmtpHost                   string `json:"smtp_host"`
+	SmtpPort                   int    `json:"smtp_port"`
+	MetricInterval             string `json:"metric_interval"`
+	MetricsPort                int    `json:"metrics_port"`
+	ManageDNS                  bool   `json:"manage_dns"`
+	DefaultDomain              string `json:"default_domain"`
+	Stun                       bool   `json:"stun"`
+	StunServers                string `json:"stun_servers"`
+	Theme                      Theme  `json:"theme"`
+	TextSize                   string `json:"text_size"`
+	ReducedMotion              bool   `json:"reduced_motion"`
 }

+ 12 - 0
servercfg/serverconf.go

@@ -114,6 +114,18 @@ func GetJwtValidityDuration() time.Duration {
 	return defaultDuration
 }
 
+// GetJwtValidityDuration - returns the JWT validity duration in seconds
+func GetJwtValidityDurationFromEnv() int {
+	var defaultDuration = 43200
+	if os.Getenv("JWT_VALIDITY_DURATION") != "" {
+		t, err := strconv.Atoi(os.Getenv("JWT_VALIDITY_DURATION"))
+		if err == nil {
+			return t
+		}
+	}
+	return defaultDuration
+}
+
 // GetRacAutoDisable - returns whether the feature to autodisable RAC is enabled
 func GetRacAutoDisable() bool {
 	return os.Getenv("RAC_AUTO_DISABLE") == "true"