databag/net/server/internal/api_admin.go

195 lines
5.3 KiB
Go
Raw Normal View History

2022-01-11 06:20:32 +00:00
/*
* DataBag
*
* DataBag provides storage for decentralized identity based self-hosting apps. It is intended to support sharing of personal data and hosting group conversations.
*
* API version: 0.0.1
* Contact: roland.osborne@gmail.com
2022-01-13 05:00:52 +00:00
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
2022-01-11 06:20:32 +00:00
*/
package databag
import (
2022-01-12 21:12:40 +00:00
"log"
"encoding/json"
2022-01-11 06:20:32 +00:00
"net/http"
2022-01-12 07:04:27 +00:00
"gorm.io/gorm"
2022-01-12 21:12:40 +00:00
"golang.org/x/crypto/bcrypt"
"databag/internal/store"
2022-01-11 06:20:32 +00:00
)
2022-01-13 07:04:43 +00:00
func adminLogin(r *http.Request) bool {
2022-01-15 04:45:53 +00:00
// extract request auth
username, password, ok := r.BasicAuth();
if !ok || username == "" || password == "" {
return false
}
// nothing to do if not configured
if !getBoolConfigValue(CONFIG_CONFIGURED, false) {
2022-01-13 07:04:43 +00:00
return false;
}
2022-01-15 04:45:53 +00:00
// compare username
if getStrConfigValue(CONFIG_USERNAME, "") != username {
2022-01-13 07:04:43 +00:00
return false
}
2022-01-15 04:45:53 +00:00
// compare password
p := getBinConfigValue(CONFIG_PASSWORD, nil);
if bcrypt.CompareHashAndPassword(p, []byte(password)) != nil {
2022-01-13 07:04:43 +00:00
return false
}
return true;
}
2022-01-11 06:20:32 +00:00
func GetNodeAccountImage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func GetNodeAccounts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func GetNodeClaimable(w http.ResponseWriter, r *http.Request) {
2022-01-12 07:04:27 +00:00
2022-01-15 04:45:53 +00:00
c := getBoolConfigValue(CONFIG_CONFIGURED, false);
2022-01-15 05:27:43 +00:00
body, err := json.Marshal(!c);
if err != nil {
log.Println("GetNodeClaimable - failed to marshal response");
}
2022-01-13 18:06:19 +00:00
w.Write(body);
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
2022-01-11 06:20:32 +00:00
}
func GetNodeConfig(w http.ResponseWriter, r *http.Request) {
2022-01-15 05:27:43 +00:00
// validate login
if !adminLogin(r) {
log.Printf("SetNodeConfig - invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized);
return
}
// get node config fields
var config NodeConfig;
config.Domain = getStrConfigValue(CONFIG_DOMAIN, "");
config.PublicLimit = getNumConfigValue(CONFIG_PUBLICLIMIT, 0);
config.AccountStorage = getNumConfigValue(CONFIG_STORAGE, 0);
body, err := json.Marshal(config);
if err != nil {
log.Println("GetNodeConfig - failed to marshal response");
}
w.Write(body);
2022-01-11 06:20:32 +00:00
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
2022-01-13 05:00:52 +00:00
func ImportAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
2022-01-11 06:20:32 +00:00
func RemoveNodeAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetNodeAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
2022-01-12 21:12:40 +00:00
2022-01-13 07:04:43 +00:00
// confirm node hasn't been configured
2022-01-15 04:45:53 +00:00
if getBoolConfigValue(CONFIG_CONFIGURED, false) {
2022-01-12 21:12:40 +00:00
w.WriteHeader(http.StatusUnauthorized)
return
}
// extract credentials
username, password, ok := r.BasicAuth();
2022-01-13 07:04:43 +00:00
if !ok || username == "" || password == "" {
log.Printf("SetNodeClaim - invalid credenitals");
2022-01-12 21:12:40 +00:00
w.WriteHeader(http.StatusBadRequest)
return
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Printf("SetNodeClaim - failed to hash password");
w.WriteHeader(http.StatusInternalServerError)
return
}
// store credentials
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
}
if res := tx.Create(&store.Config{ConfigId: CONFIG_PASSWORD, BinValue: hashedPassword}).Error; res != nil {
return res
}
2022-01-15 04:45:53 +00:00
if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil {
return res
}
2022-01-12 21:12:40 +00:00
return nil;
})
if(err != nil) {
log.Printf("SetNodeCalim - failed to store credentials");
w.WriteHeader(http.StatusInternalServerError)
return
}
2022-01-11 06:20:32 +00:00
w.WriteHeader(http.StatusOK)
}
func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
2022-01-12 21:12:40 +00:00
2022-01-13 07:04:43 +00:00
// validate login
if !adminLogin(r) {
2022-01-12 21:12:40 +00:00
log.Printf("SetNodeConfig - invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized);
return
}
// parse node config
r.Body = http.MaxBytesReader(w, r.Body, CONFIG_BODYLIMIT)
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
var config NodeConfig;
res := dec.Decode(&config);
if res != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// store credentials
err := store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Create(&store.Config{ConfigId: CONFIG_DOMAIN, StrValue: config.Domain}).Error; res != nil {
return res
}
if res := tx.Create(&store.Config{ConfigId: CONFIG_PUBLICLIMIT, NumValue: config.PublicLimit}).Error; res != nil {
return res
}
if res := tx.Create(&store.Config{ConfigId: CONFIG_STORAGE, NumValue: config.AccountStorage}).Error; res != nil {
return res
}
return nil;
})
if(err != nil) {
log.Printf("SetNodeConfig - failed to store config");
w.WriteHeader(http.StatusInternalServerError)
return
}
2022-01-11 06:20:32 +00:00
w.WriteHeader(http.StatusOK)
}
2022-01-13 07:04:43 +00:00