make sure auth secret is set only once

This commit is contained in:
abhishek9686 2024-04-04 12:07:33 +05:30
parent 825caf4b60
commit ba33ed02aa
2 changed files with 15 additions and 4 deletions

View file

@ -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
}

View file

@ -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
}