diff --git a/net/server/internal/addNodeAccount.endpoint.go b/net/server/internal/addNodeAccount.endpoint.go new file mode 100644 index 00000000..4abb3980 --- /dev/null +++ b/net/server/internal/addNodeAccount.endpoint.go @@ -0,0 +1,41 @@ +package databag + +import ( + "log" + "encoding/json" + "net/http" + "databag/internal/store" + "github.com/theckman/go-securerandom" +) + +func AddNodeAccount(w http.ResponseWriter, r *http.Request) { + + // validate login + if !adminLogin(r) { + log.Printf("AddNodeAccount - invalid admin credentials"); + w.WriteHeader(http.StatusUnauthorized); + return + } + + data, err := securerandom.Base64OfBytes(32) + if err != nil { + log.Println("AddNodeAccount - failed to generate token"); + w.WriteHeader(http.StatusInternalServerError); + return + } + + token := store.AccountToken{TokenType: "create", Token: data }; + if res := store.DB.Create(&token).Error; res != nil { + log.Println("AddNodeAccount - failed to store token"); + return + } + + body, err := json.Marshal(data); + 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) +} + diff --git a/net/server/internal/api_admin.go b/net/server/internal/api_admin.go index b7560275..37277ba1 100644 --- a/net/server/internal/api_admin.go +++ b/net/server/internal/api_admin.go @@ -16,7 +16,6 @@ import ( "gorm.io/gorm" "golang.org/x/crypto/bcrypt" "databag/internal/store" - "github.com/theckman/go-securerandom" ) func adminLogin(r *http.Request) bool { @@ -46,37 +45,6 @@ func adminLogin(r *http.Request) bool { return true; } -func AddNodeAccount(w http.ResponseWriter, r *http.Request) { - - // validate login - if !adminLogin(r) { - log.Printf("AddNodeAccount - invalid admin credentials"); - w.WriteHeader(http.StatusUnauthorized); - return - } - - data, err := securerandom.Base64OfBytes(32) - if err != nil { - log.Println("AddNodeAccount - failed to generate token"); - w.WriteHeader(http.StatusInternalServerError); - return - } - - token := store.AccountToken{TokenType: "create", Token: data }; - if res := store.DB.Create(&token).Error; res != nil { - log.Println("AddNodeAccount - failed to store token"); - return - } - - body, err := json.Marshal(data); - 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 GetNodeAccountImage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) diff --git a/net/server/internal/config_keys.go b/net/server/internal/configNames.go similarity index 100% rename from net/server/internal/config_keys.go rename to net/server/internal/configNames.go diff --git a/net/server/internal/model_account.go b/net/server/internal/model_account.go deleted file mode 100644 index a0eecbaf..00000000 --- a/net/server/internal/model_account.go +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 - * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) - */ -package databag - -type Account struct { - - AccountId string `json:"accountId"` - - Profile *Profile `json:"profile"` - - Disabled bool `json:"disabled"` -} diff --git a/net/server/internal/model_account_status.go b/net/server/internal/model_account_status.go deleted file mode 100644 index 4effd0a9..00000000 --- a/net/server/internal/model_account_status.go +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 - * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) - */ -package databag - -type AccountStatus struct { - - Disabled bool `json:"disabled"` - - StorageUsed float64 `json:"storageUsed"` - - StorageAvailable float64 `json:"storageAvailable"` - - ForwardingAddress string `json:"forwardingAddress"` -} - diff --git a/net/server/internal/models.go b/net/server/internal/models.go new file mode 100644 index 00000000..3de28557 --- /dev/null +++ b/net/server/internal/models.go @@ -0,0 +1,15 @@ +package databag + +type Account struct { + AccountId string `json:"accountId"` + Profile *Profile `json:"profile"` + Disabled bool `json:"disabled"` +} + +type AccountStatus struct { + Disabled bool `json:"disabled"` + StorageUsed float64 `json:"storageUsed"` + StorageAvailable float64 `json:"storageAvailable"` + ForwardingAddress string `json:"forwardingAddress"` +} +