diff --git a/net/server/internal/api_admin.go b/net/server/internal/api_admin.go index 554c0f2c..0f3fe2da 100644 --- a/net/server/internal/api_admin.go +++ b/net/server/internal/api_admin.go @@ -20,19 +20,25 @@ import ( func adminLogin(r *http.Request) bool { - // check configured state - if !_configured || _adminUsername == "" || _adminPassword == nil { - return false; - } - - // validate imput + // extract request auth username, password, ok := r.BasicAuth(); if !ok || username == "" || password == "" { return false } - // compare credentials - if username != _adminUsername || bcrypt.CompareHashAndPassword(_adminPassword, []byte(password)) != nil { + // nothing to do if not configured + if !getBoolConfigValue(CONFIG_CONFIGURED, false) { + return false; + } + + // compare username + if getStrConfigValue(CONFIG_USERNAME, "") != username { + return false + } + + // compare password + p := getBinConfigValue(CONFIG_PASSWORD, nil); + if bcrypt.CompareHashAndPassword(p, []byte(password)) != nil { return false } @@ -56,7 +62,8 @@ func GetNodeAccounts(w http.ResponseWriter, r *http.Request) { func GetNodeClaimable(w http.ResponseWriter, r *http.Request) { - body, _ := json.Marshal(!_configured); + c := getBoolConfigValue(CONFIG_CONFIGURED, false); + body, _ := json.Marshal(!c); w.Write(body); w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) @@ -85,7 +92,7 @@ func SetNodeAccount(w http.ResponseWriter, r *http.Request) { func SetNodeClaim(w http.ResponseWriter, r *http.Request) { // confirm node hasn't been configured - if _configured { + if getBoolConfigValue(CONFIG_CONFIGURED, false) { w.WriteHeader(http.StatusUnauthorized) return } @@ -112,6 +119,9 @@ func SetNodeClaim(w http.ResponseWriter, r *http.Request) { if res := tx.Create(&store.Config{ConfigId: CONFIG_PASSWORD, BinValue: hashedPassword}).Error; res != nil { return res } + if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil { + return res + } return nil; }) if(err != nil) { @@ -120,11 +130,6 @@ func SetNodeClaim(w http.ResponseWriter, r *http.Request) { return } - // set global values - _adminUsername = username - _adminPassword = hashedPassword - _configured = true - w.WriteHeader(http.StatusOK) } @@ -167,11 +172,6 @@ func SetNodeConfig(w http.ResponseWriter, r *http.Request) { return } - // set global values - _nodeDomain = config.Domain - _publicLimit = config.PublicLimit - _accountStorage = config.AccountStorage - w.WriteHeader(http.StatusOK) } diff --git a/net/server/internal/context.go b/net/server/internal/config.go similarity index 65% rename from net/server/internal/context.go rename to net/server/internal/config.go index 4a9bbc69..546b186e 100644 --- a/net/server/internal/context.go +++ b/net/server/internal/config.go @@ -6,16 +6,9 @@ import ( "databag/internal/store" ) -var _configured bool -var _adminUsername string -var _adminPassword []byte -var _nodeDomain string -var _accountStorage int64 -var _publicLimit int64 - func getStrConfigValue(configId string, empty string) string { var config store.Config - err := store.DB.Where("config_id = ?", config).First(&config).Error + err := store.DB.Where("config_id = ?", configId).First(&config).Error if errors.Is(err, gorm.ErrRecordNotFound) { return empty } @@ -24,7 +17,7 @@ func getStrConfigValue(configId string, empty string) string { func getNumConfigValue(configId string, empty int64) int64 { var config store.Config - err := store.DB.Where("config_id = ?", config).First(&config).Error + err := store.DB.Where("config_id = ?", configId).First(&config).Error if errors.Is(err, gorm.ErrRecordNotFound) { return empty } @@ -33,7 +26,7 @@ func getNumConfigValue(configId string, empty int64) int64 { func getBoolConfigValue(configId string, empty bool) bool { var config store.Config - err := store.DB.Where("config_id = ?", config).First(&config).Error + err := store.DB.Where("config_id = ?", configId).First(&config).Error if errors.Is(err, gorm.ErrRecordNotFound) { return empty } @@ -42,7 +35,7 @@ func getBoolConfigValue(configId string, empty bool) bool { func getBinConfigValue(configId string, empty []byte) []byte { var config store.Config - err := store.DB.Where("config_id = ?", config).First(&config).Error + err := store.DB.Where("config_id = ?", configId).First(&config).Error if errors.Is(err, gorm.ErrRecordNotFound) { return empty } diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index fb6ce062..39268644 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -28,14 +28,6 @@ type Routes []Route func NewRouter() *mux.Router { - // populate context - _configured = getBoolConfigValue(CONFIG_CONFIGURED, false); - _adminUsername = getStrConfigValue(CONFIG_USERNAME, ""); - _adminPassword = getBinConfigValue(CONFIG_PASSWORD, nil); - _nodeDomain = getStrConfigValue(CONFIG_DOMAIN, ""); - _publicLimit = getNumConfigValue(CONFIG_PUBLICLIMIT, 0); - _accountStorage = getNumConfigValue(CONFIG_STORAGE, 0); - router := mux.NewRouter().StrictSlash(true) for _, route := range routes { var handler http.Handler