diff --git a/api/api.go b/api/api.go index bbbc8cc..f1c390d 100644 --- a/api/api.go +++ b/api/api.go @@ -78,8 +78,6 @@ func New(db *gorm.DB) *WebApi { router := fasthttprouter.New() router.GET("/", LogRequestMiddleware(Index)) - router.POST("/client", LogRequestMiddleware(api.CreateClient)) - router.POST("/slot", LogRequestMiddleware(api.AllocateUploadSlot)) router.GET("/slot", LogRequestMiddleware(api.ReadUploadSlot)) router.GET("/upload", LogRequestMiddleware(api.Upload)) @@ -90,7 +88,6 @@ func New(db *gorm.DB) *WebApi { } api.db = db - db.AutoMigrate(&Client{}) db.AutoMigrate(&UploadSlot{}) api.setupMotd() diff --git a/api/auth.go b/api/auth.go index 36d013a..778f64e 100644 --- a/api/auth.go +++ b/api/auth.go @@ -1,62 +1 @@ package api - -import ( - "encoding/json" - "github.com/sirupsen/logrus" - "github.com/valyala/fasthttp" - "math/rand" -) - -func (api *WebApi) CreateClient(ctx *fasthttp.RequestCtx) { - - //TODO: auth - - req := &CreateClientRequest{} - err := json.Unmarshal(ctx.Request.Body(), req) - if err != nil { - ctx.Response.SetStatusCode(400) - Json(CreateClientResponse{ - Ok: false, - }, ctx) - return - } - - if !req.IsValid() { - ctx.Response.SetStatusCode(400) - Json(CreateClientResponse{ - Ok: false, - }, ctx) - return - } - - client := api.createClient(req) - - Json(CreateClientResponse{ - Ok: true, - Secret: client.Secret, - }, ctx) -} - -func (api *WebApi) createClient(req *CreateClientRequest) *Client { - - client := &Client{ - Alias: req.Alias, - Secret: genSecret(), - } - - api.db.Create(client) - - logrus.WithFields(logrus.Fields{ - "client": client, - }).Info("Created client") - - return client -} - -func genSecret() string { - bytes := make([]byte, 32) - for i := 0; i < 32; i++ { - bytes[i] = byte(48 + rand.Intn(122-48)) - } - return string(bytes) -} diff --git a/api/models.go b/api/models.go index e4f99f9..2492ce8 100644 --- a/api/models.go +++ b/api/models.go @@ -10,25 +10,6 @@ type GenericResponse struct { Ok bool `json:"ok"` } -type CreateClientRequest struct { - Alias string `json:"alias"` -} - -func (req *CreateClientRequest) IsValid() bool { - return len(req.Alias) > 3 -} - -type CreateClientResponse struct { - Ok bool `json:"ok"` - Secret string `json:"secret,omitempty"` -} - -type Client struct { - ID int64 - Alias string `json:"alias"` - Secret string `json:"secret"` -} - type AllocateUploadSlotRequest struct { Token string `json:"token"` MaxSize int64 `json:"max_size"` diff --git a/api/slot.go b/api/slot.go index 7bd1b77..1e1b9e1 100644 --- a/api/slot.go +++ b/api/slot.go @@ -22,6 +22,8 @@ var upgrader = websocket.FastHTTPUpgrader{ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) { + //todo auth + req := &AllocateUploadSlotRequest{} err := json.Unmarshal(ctx.Request.Body(), req) if err != nil { @@ -34,7 +36,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) { if !req.IsValid() { ctx.Response.Header.SetStatusCode(400) - Json(CreateClientResponse{ + Json(GenericResponse{ Ok: false, }, ctx) return @@ -42,7 +44,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) { api.allocateUploadSlot(req) - Json(CreateClientResponse{ + Json(GenericResponse{ Ok: true, }, ctx) } diff --git a/test/auth_test.go b/test/auth_test.go index ec3259e..56e5404 100644 --- a/test/auth_test.go +++ b/test/auth_test.go @@ -1,24 +1 @@ package test - -import ( - "github.com/simon987/ws_bucket/api" - "testing" -) - -func TestCreateClient(t *testing.T) { - - r := createClient(api.CreateClientRequest{ - Alias: "testcreateclient", - }) - - if r.Ok != true { - t.Error() - } -} - -func createClient(request api.CreateClientRequest) (ar *api.CreateClientResponse) { - - resp := Post("/client", request) - UnmarshalResponse(resp, &ar) - return -}