Remove multi-user auth

This commit is contained in:
simon987 2019-03-09 12:44:15 -05:00
parent 04f2ff8900
commit e2ba51b77a
5 changed files with 4 additions and 108 deletions

View File

@ -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()

View File

@ -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)
}

View File

@ -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"`

View File

@ -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)
}

View File

@ -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
}