mirror of
https://github.com/simon987/ws_bucket.git
synced 2025-04-20 02:36:46 +00:00
Remove multi-user auth
This commit is contained in:
parent
04f2ff8900
commit
e2ba51b77a
@ -78,8 +78,6 @@ func New(db *gorm.DB) *WebApi {
|
|||||||
router := fasthttprouter.New()
|
router := fasthttprouter.New()
|
||||||
router.GET("/", LogRequestMiddleware(Index))
|
router.GET("/", LogRequestMiddleware(Index))
|
||||||
|
|
||||||
router.POST("/client", LogRequestMiddleware(api.CreateClient))
|
|
||||||
|
|
||||||
router.POST("/slot", LogRequestMiddleware(api.AllocateUploadSlot))
|
router.POST("/slot", LogRequestMiddleware(api.AllocateUploadSlot))
|
||||||
router.GET("/slot", LogRequestMiddleware(api.ReadUploadSlot))
|
router.GET("/slot", LogRequestMiddleware(api.ReadUploadSlot))
|
||||||
router.GET("/upload", LogRequestMiddleware(api.Upload))
|
router.GET("/upload", LogRequestMiddleware(api.Upload))
|
||||||
@ -90,7 +88,6 @@ func New(db *gorm.DB) *WebApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
api.db = db
|
api.db = db
|
||||||
db.AutoMigrate(&Client{})
|
|
||||||
db.AutoMigrate(&UploadSlot{})
|
db.AutoMigrate(&UploadSlot{})
|
||||||
|
|
||||||
api.setupMotd()
|
api.setupMotd()
|
||||||
|
61
api/auth.go
61
api/auth.go
@ -1,62 +1 @@
|
|||||||
package api
|
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)
|
|
||||||
}
|
|
||||||
|
@ -10,25 +10,6 @@ type GenericResponse struct {
|
|||||||
Ok bool `json:"ok"`
|
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 {
|
type AllocateUploadSlotRequest struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
MaxSize int64 `json:"max_size"`
|
MaxSize int64 `json:"max_size"`
|
||||||
|
@ -22,6 +22,8 @@ var upgrader = websocket.FastHTTPUpgrader{
|
|||||||
|
|
||||||
func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
|
func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
|
||||||
|
|
||||||
|
//todo auth
|
||||||
|
|
||||||
req := &AllocateUploadSlotRequest{}
|
req := &AllocateUploadSlotRequest{}
|
||||||
err := json.Unmarshal(ctx.Request.Body(), req)
|
err := json.Unmarshal(ctx.Request.Body(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -34,7 +36,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
|
|||||||
|
|
||||||
if !req.IsValid() {
|
if !req.IsValid() {
|
||||||
ctx.Response.Header.SetStatusCode(400)
|
ctx.Response.Header.SetStatusCode(400)
|
||||||
Json(CreateClientResponse{
|
Json(GenericResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
}, ctx)
|
}, ctx)
|
||||||
return
|
return
|
||||||
@ -42,7 +44,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
|
|||||||
|
|
||||||
api.allocateUploadSlot(req)
|
api.allocateUploadSlot(req)
|
||||||
|
|
||||||
Json(CreateClientResponse{
|
Json(GenericResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
}, ctx)
|
}, ctx)
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1 @@
|
|||||||
package test
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user