2022-01-15 21:45:37 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-01-16 07:25:43 +00:00
|
|
|
"errors"
|
2022-01-15 21:45:37 +00:00
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-01-16 07:25:43 +00:00
|
|
|
var config store.Config
|
|
|
|
err := store.DB.Where("config_id = ?", CONFIG_CONFIGURED).First(&config).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config.BoolValue {
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:42:17 +00:00
|
|
|
username, password, res := BasicCredentials(r);
|
2022-01-17 05:11:24 +00:00
|
|
|
if res != nil {
|
|
|
|
LogMsg("invalid credenitals");
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_USERNAME, StrValue: username}).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-01-17 05:11:24 +00:00
|
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_PASSWORD, BinValue: password}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
})
|
|
|
|
if(err != nil) {
|
2022-01-16 07:25:43 +00:00
|
|
|
LogMsg("SetNodeCalim - failed to store credentials");
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|