Переглянути джерело

make sure auth secret is set only once

abhishek9686 1 рік тому
батько
коміт
ba33ed02aa
2 змінених файлів з 15 додано та 4 видалено
  1. 1 2
      auth/auth.go
  2. 14 2
      logic/auth.go

+ 1 - 2
auth/auth.go

@@ -32,7 +32,6 @@ const (
 	github_provider_name   = "github"
 	oidc_provider_name     = "oidc"
 	verify_user            = "verifyuser"
-	auth_key               = "netmaker_auth"
 	user_signin_length     = 16
 	node_signin_length     = 64
 	headless_signin_length = 32
@@ -281,7 +280,7 @@ func fetchPassValue(newValue string) (string, error) {
 		Value string `json:"value" bson:"value"`
 	}
 	newValueHolder := valueHolder{}
-	var currentValue, err = logic.FetchAuthSecret(auth_key)
+	var currentValue, err = logic.FetchAuthSecret()
 	if err != nil {
 		return "", err
 	}

+ 14 - 2
logic/auth.go

@@ -16,6 +16,10 @@ import (
 	"github.com/gravitl/netmaker/models"
 )
 
+const (
+	auth_key = "netmaker_auth"
+)
+
 // HasSuperAdmin - checks if server has an superadmin/owner
 func HasSuperAdmin() (bool, error) {
 
@@ -289,6 +293,14 @@ func SetAuthSecret(key, secret string) error {
 	type valueHolder struct {
 		Value string `json:"value" bson:"value"`
 	}
+	record, err := FetchAuthSecret()
+	if err == nil {
+		v := valueHolder{}
+		json.Unmarshal([]byte(record), &v)
+		if v.Value != "" {
+			return nil
+		}
+	}
 	var b64NewValue = base64.StdEncoding.EncodeToString([]byte(secret))
 	newValueHolder := valueHolder{
 		Value: b64NewValue,
@@ -298,8 +310,8 @@ func SetAuthSecret(key, secret string) error {
 }
 
 // FetchAuthSecret - manages secrets for oauth
-func FetchAuthSecret(key string) (string, error) {
-	var record, err = database.FetchRecord(database.GENERATED_TABLE_NAME, key)
+func FetchAuthSecret() (string, error) {
+	var record, err = database.FetchRecord(database.GENERATED_TABLE_NAME, auth_key)
 	if err != nil {
 		return "", err
 	}