mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
more code layout changes
This commit is contained in:
parent
1b10c82ecc
commit
9324463839
34
net/server/internal/adminLogin.go
Normal file
34
net/server/internal/adminLogin.go
Normal file
@ -0,0 +1,34 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func adminLogin(r *http.Request) bool {
|
||||
|
||||
// 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) {
|
||||
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
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -10,41 +10,9 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"log"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func adminLogin(r *http.Request) bool {
|
||||
|
||||
// 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) {
|
||||
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
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
func GetNodeAccountImage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@ -55,42 +23,6 @@ func GetNodeAccounts(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetNodeClaimable(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
c := getBoolConfigValue(CONFIG_CONFIGURED, false);
|
||||
body, err := json.Marshal(!c);
|
||||
if err != nil {
|
||||
log.Println("GetNodeClaimable - failed to marshal response");
|
||||
}
|
||||
w.Write(body);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 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);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func ImportAccount(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@ -106,89 +38,3 @@ func SetNodeAccount(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// confirm node hasn't been configured
|
||||
if getBoolConfigValue(CONFIG_CONFIGURED, false) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// extract credentials
|
||||
username, password, ok := r.BasicAuth();
|
||||
if !ok || username == "" || password == "" {
|
||||
log.Printf("SetNodeClaim - invalid credenitals");
|
||||
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
|
||||
}
|
||||
if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
return nil;
|
||||
})
|
||||
if(err != nil) {
|
||||
log.Printf("SetNodeCalim - failed to store credentials");
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// validate login
|
||||
if !adminLogin(r) {
|
||||
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
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
|
20
net/server/internal/getNodeClaimable.endpoint.go
Normal file
20
net/server/internal/getNodeClaimable.endpoint.go
Normal file
@ -0,0 +1,20 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"log"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetNodeClaimable(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
c := getBoolConfigValue(CONFIG_CONFIGURED, false);
|
||||
body, err := json.Marshal(!c);
|
||||
if err != nil {
|
||||
log.Println("GetNodeClaimable - failed to marshal response");
|
||||
}
|
||||
w.Write(body);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
32
net/server/internal/getNodeConfig.endpoint.go
Normal file
32
net/server/internal/getNodeConfig.endpoint.go
Normal file
@ -0,0 +1,32 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"log"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 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);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
54
net/server/internal/setNodeClaim.endpoint.go
Normal file
54
net/server/internal/setNodeClaim.endpoint.go
Normal file
@ -0,0 +1,54 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"databag/internal/store"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// confirm node hasn't been configured
|
||||
if getBoolConfigValue(CONFIG_CONFIGURED, false) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// extract credentials
|
||||
username, password, ok := r.BasicAuth();
|
||||
if !ok || username == "" || password == "" {
|
||||
log.Printf("SetNodeClaim - invalid credenitals");
|
||||
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
|
||||
}
|
||||
if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
return nil;
|
||||
})
|
||||
if(err != nil) {
|
||||
log.Printf("SetNodeCalim - failed to store credentials");
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
52
net/server/internal/setNodeConfig.endpoint.go
Normal file
52
net/server/internal/setNodeConfig.endpoint.go
Normal file
@ -0,0 +1,52 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"log"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// validate login
|
||||
if !adminLogin(r) {
|
||||
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
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user