feat(v1): header auth for all endpoints
This commit is contained in:
parent
e2cd445aeb
commit
f6dc33edb4
22
main.go
22
main.go
@ -1,20 +1,34 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.odit.services/lfk/document-server/docs" // Correct import path for docs
|
"git.odit.services/lfk/document-server/docs" // Correct import path for docs
|
||||||
"git.odit.services/lfk/document-server/handlers"
|
"git.odit.services/lfk/document-server/handlers"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
||||||
"github.com/gofiber/swagger"
|
"github.com/gofiber/swagger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
port = flag.String("port", ":3000", "Port to listen on")
|
port = flag.String("port", ":3000", "Port to listen on")
|
||||||
prod = flag.Bool("prod", false, "Enable prefork in Production")
|
prod = flag.Bool("prod", false, "Enable prefork in Production")
|
||||||
|
apiKey = flag.String("apikey", "lfk", "API key for incoming authentication")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func validateAPIKey(c *fiber.Ctx, key string) (bool, error) {
|
||||||
|
hashedAPIKey := sha256.Sum256([]byte(*apiKey))
|
||||||
|
hashedKey := sha256.Sum256([]byte(key))
|
||||||
|
|
||||||
|
if subtle.ConstantTimeCompare(hashedAPIKey[:], hashedKey[:]) == 1 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, keyauth.ErrMissingOrMalformedAPIKey
|
||||||
|
}
|
||||||
|
|
||||||
// @title LfK Document Server API
|
// @title LfK Document Server API
|
||||||
// @description This is the API documentation for the LfK Document Server - a tool for pdf generation.
|
// @description This is the API documentation for the LfK Document Server - a tool for pdf generation.
|
||||||
func main() {
|
func main() {
|
||||||
@ -30,6 +44,10 @@ func main() {
|
|||||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||||
|
|
||||||
v1 := app.Group("/v1")
|
v1 := app.Group("/v1")
|
||||||
|
v1.Use(keyauth.New(keyauth.Config{
|
||||||
|
KeyLookup: "header:Authorization",
|
||||||
|
Validator: validateAPIKey,
|
||||||
|
}))
|
||||||
|
|
||||||
v1.Get("/", func(c *fiber.Ctx) error {
|
v1.Get("/", func(c *fiber.Ctx) error {
|
||||||
return c.SendString("Hello, World!")
|
return c.SendString("Hello, World!")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user