mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-10 23:24:32 +08:00
add set auth secret
This commit is contained in:
parent
4d001f0d27
commit
f41cef5da5
2 changed files with 19 additions and 18 deletions
16
auth/auth.go
16
auth/auth.go
|
@ -75,9 +75,9 @@ func InitializeAuthProvider() string {
|
||||||
if functions == nil {
|
if functions == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var _, err = fetchPassValue(logic.RandomString(64))
|
var err = logic.SetAuthSecret(auth_key, logic.RandomString(64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log(0, err.Error())
|
logger.FatalLog("failed to set auth_secret", err.Error())
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var authInfo = servercfg.GetAuthProviderInfo()
|
var authInfo = servercfg.GetAuthProviderInfo()
|
||||||
|
@ -280,16 +280,8 @@ func fetchPassValue(newValue string) (string, error) {
|
||||||
type valueHolder struct {
|
type valueHolder struct {
|
||||||
Value string `json:"value" bson:"value"`
|
Value string `json:"value" bson:"value"`
|
||||||
}
|
}
|
||||||
var b64NewValue = base64.StdEncoding.EncodeToString([]byte(newValue))
|
newValueHolder := valueHolder{}
|
||||||
var newValueHolder = &valueHolder{
|
var currentValue, err = logic.FetchAuthSecret(auth_key)
|
||||||
Value: b64NewValue,
|
|
||||||
}
|
|
||||||
var data, marshalErr = json.Marshal(newValueHolder)
|
|
||||||
if marshalErr != nil {
|
|
||||||
return "", marshalErr
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentValue, err = logic.FetchAuthSecret(auth_key, string(data))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -284,15 +285,23 @@ func DeleteUser(user string) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetAuthSecret(key, secret string) error {
|
||||||
|
type valueHolder struct {
|
||||||
|
Value string `json:"value" bson:"value"`
|
||||||
|
}
|
||||||
|
var b64NewValue = base64.StdEncoding.EncodeToString([]byte(secret))
|
||||||
|
newValueHolder := valueHolder{
|
||||||
|
Value: b64NewValue,
|
||||||
|
}
|
||||||
|
d, _ := json.Marshal(newValueHolder)
|
||||||
|
return database.Insert(key, string(d), database.GENERATED_TABLE_NAME)
|
||||||
|
}
|
||||||
|
|
||||||
// FetchAuthSecret - manages secrets for oauth
|
// FetchAuthSecret - manages secrets for oauth
|
||||||
func FetchAuthSecret(key string, secret string) (string, error) {
|
func FetchAuthSecret(key string) (string, error) {
|
||||||
var record, err = database.FetchRecord(database.GENERATED_TABLE_NAME, key)
|
var record, err = database.FetchRecord(database.GENERATED_TABLE_NAME, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err = database.Insert(key, secret, database.GENERATED_TABLE_NAME); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
} else {
|
|
||||||
return secret, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue