added account create token

This commit is contained in:
Roland Osborne 2022-01-15 00:07:00 -08:00
parent 986a161787
commit c3b142fcba
7 changed files with 85 additions and 41 deletions

View File

@ -8,6 +8,7 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/mattn/go-sqlite3 v1.14.9 // indirect
github.com/theckman/go-securerandom v0.1.1 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
gorm.io/driver/sqlite v1.2.6 // indirect
gorm.io/gorm v1.22.4 // indirect

View File

@ -14,6 +14,8 @@ github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/theckman/go-securerandom v0.1.1 h1:5KctSyM0D5KKFK+bsypIyLq7yik0CEaI5i2fGcUGcsQ=
github.com/theckman/go-securerandom v0.1.1/go.mod h1:bmkysLfBH6i891sBpcP4xRM3XIB7jMeiKJB31jlResI=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -16,6 +16,7 @@ import (
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
"databag/internal/store"
"github.com/theckman/go-securerandom"
)
func adminLogin(r *http.Request) bool {
@ -46,6 +47,32 @@ func adminLogin(r *http.Request) bool {
}
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)
}

View File

@ -1,8 +0,0 @@
package databag
import (
"testing"
)
func TestClaimable(t *testing.T) {
}

View File

@ -1,4 +1,4 @@
package main
package databag
import (
"strings"
@ -6,24 +6,26 @@ import (
"net/http/httptest"
"encoding/base64"
"encoding/json"
app "databag/internal"
"databag/internal/store"
)
func TestSetup(t *testing.T) {
func TestMain(m *testing.M) {
store.SetPath("file::memory:?cache=shared");
//store.SetPath("databag.db");
Claimable(t);
Claim(t);
SetConfig(t);
GetConfig(t);
Claimable();
Claim();
SetConfig();
GetConfig();
m.Run()
}
func Claimable(t *testing.T) {
func Claimable() {
r := httptest.NewRequest("GET", "/admin/claimable", nil)
w := httptest.NewRecorder()
app.GetNodeClaimable(w, r)
GetNodeClaimable(w, r)
//body, _ := ioutil.ReadAll(resp.Body)
resp := w.Result()
@ -31,58 +33,58 @@ func Claimable(t *testing.T) {
var res bool
err := dec.Decode(&res)
if err != nil {
t.Errorf("failed to get claimable response")
panic("failed to get claimable response")
}
if resp.StatusCode != 200 {
t.Errorf("server not initially claimable")
panic("server not initially claimable")
}
}
func Claim(t *testing.T) {
func Claim() {
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("PUT", "/admin/claim", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
app.SetNodeClaim(w, r)
SetNodeClaim(w, r)
if w.Code != 200 {
t.Errorf("server not initially claimable")
panic("server not initially claimable")
}
}
func SetConfig(t *testing.T) {
config := app.NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
func SetConfig() {
config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
body,_ := json.Marshal(config)
r := httptest.NewRequest("PUT", "/admin/config", strings.NewReader(string(body)))
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
app.SetNodeConfig(w, r);
SetNodeConfig(w, r);
if w.Code != 200 {
t.Errorf("failed to set node config")
panic("failed to set node config")
}
}
func GetConfig(t *testing.T) {
func GetConfig() {
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("GET", "/admin/config", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
app.GetNodeConfig(w, r);
GetNodeConfig(w, r);
resp := w.Result();
dec := json.NewDecoder(resp.Body);
var config app.NodeConfig;
var config NodeConfig;
dec.Decode(&config);
if resp.StatusCode != 200 {
t.Errorf("failed to get node config")
panic("failed to get node config")
}
if config.Domain != "example.com" {
t.Error("failed to set config domain");
panic("failed to set config domain");
}
if config.PublicLimit != 1024 {
t.Error("failed to set public limit");
panic("failed to set public limit");
}
if config.AccountStorage != 4096 {
t.Error("failed to set account storage");
panic("failed to set account storage");
}
}

View File

@ -0,0 +1,28 @@
package databag
import (
"fmt"
"testing"
"net/http/httptest"
"encoding/base64"
"encoding/json"
)
func TestAccount(t *testing.T) {
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
r := httptest.NewRequest("POST", "/admin/accounts", nil)
r.Header.Add("Authorization","Basic " + auth)
w := httptest.NewRecorder()
AddNodeAccount(w, r);
resp := w.Result();
dec := json.NewDecoder(resp.Body);
var token string;
dec.Decode(&token);
if resp.StatusCode != 200 {
t.Errorf("failed to create account")
}
fmt.Println(token);
}

View File

@ -1,8 +0,0 @@
package store
import (
"testing"
)
func Test(t *testing.T) {
}