added key auth support for rw operations (upload, non-expiring download)

This commit is contained in:
2023-03-06 19:46:06 +01:00
parent 2a2e41126d
commit cd0939cbc8
13 changed files with 124 additions and 20 deletions

View File

@@ -21,6 +21,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/gofiber/keyauth/v2"
"github.com/tlinden/up/upd/cfg"
)
@@ -47,30 +48,49 @@ func Runserver(cfg *cfg.Config, args []string) error {
}
defer db.Close()
AuthSetEndpoints(cfg.ApiPrefix, ApiVersion, []string{"/file"})
AuthSetApikeys(cfg.Apikeys)
auth := keyauth.New(keyauth.Config{
Validator: validateAPIKey,
})
shallExpire := true
api := router.Group(cfg.ApiPrefix + ApiVersion)
{
api.Post("/file/", func(c *fiber.Ctx) error {
// authenticated routes
api.Post("/file/", auth, func(c *fiber.Ctx) error {
msg, err := FilePut(c, cfg, db)
return SendResponse(c, msg, err)
})
api.Get("/file/:id/:file", func(c *fiber.Ctx) error {
api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error {
return FileGet(c, cfg, db)
})
api.Get("/file/:id/", func(c *fiber.Ctx) error {
api.Get("/file/:id/", auth, func(c *fiber.Ctx) error {
return FileGet(c, cfg, db)
})
api.Delete("/file/:id/", func(c *fiber.Ctx) error {
api.Delete("/file/:id/", auth, func(c *fiber.Ctx) error {
return FileDelete(c, cfg, db)
})
}
// public routes
router.Get("/", func(c *fiber.Ctx) error {
return c.Send([]byte("welcome to upload api, use /api enpoint!"))
})
router.Get("/download/:id/:file", func(c *fiber.Ctx) error {
return FileGet(c, cfg, db, shallExpire)
})
router.Get("/download/:id/", func(c *fiber.Ctx) error {
return FileGet(c, cfg, db, shallExpire)
})
return router.Listen(cfg.Listen)
}