From c65e239b68f7bc57b7a1ebfef4bd86fab0426e05 Mon Sep 17 00:00:00 2001 From: Dennis Schoepf Date: Fri, 26 Apr 2024 22:46:31 +0200 Subject: [PATCH] Removes internal api package --- internal/api/api.go | 31 ------------------------ internal/api/auth.go | 56 ------------------------------------------- internal/api/users.go | 9 ------- 3 files changed, 96 deletions(-) delete mode 100644 internal/api/api.go delete mode 100644 internal/api/auth.go delete mode 100644 internal/api/users.go diff --git a/internal/api/api.go b/internal/api/api.go deleted file mode 100644 index df85c97..0000000 --- a/internal/api/api.go +++ /dev/null @@ -1,31 +0,0 @@ -package api - -import ( - "errors" - "os" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/keyauth" -) - -func Setup(app *fiber.App) error { - apiKey := os.Getenv("API_KEY") - - if apiKey == "" { - return errors.New("Could not read API_KEY from ENV file.") - } - - api := app.Group("/api", keyauth.New(keyauth.Config{ - SuccessHandler: successHandler, - ErrorHandler: errHandler, - KeyLookup: "header:x-api-key", - ContextKey: "apiKey", - Validator: apiKeyValidator(apiKey), - })) - - v1 := api.Group("/v1") - - v1.Get("/users", FetchAllUsersHandler) - - return nil -} diff --git a/internal/api/auth.go b/internal/api/auth.go deleted file mode 100644 index b8221f8..0000000 --- a/internal/api/auth.go +++ /dev/null @@ -1,56 +0,0 @@ -package api - -import ( - "crypto/sha256" - "crypto/subtle" - "regexp" - "strings" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/keyauth" -) - -var ( - apiKey = "willbechanged" - protectedURLs = []*regexp.Regexp{ - regexp.MustCompile("^/api$"), - } - errForbidden = &fiber.Error{ - Code: 403, - Message: "API Key is missing or invalid", - } -) - -func apiKeyValidator(apiKey string) func(*fiber.Ctx, string) (bool, error) { - return func(_ *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 - } -} - -func protectedRoutesFilter(ctx *fiber.Ctx) bool { - originalURL := strings.ToLower(ctx.OriginalURL()) - - for _, pattern := range protectedURLs { - if pattern.MatchString(originalURL) { - return false - } - } - return true -} - -func successHandler(ctx *fiber.Ctx) error { - return ctx.Next() -} - -func errHandler(ctx *fiber.Ctx, err error) error { - ctx.Status(fiber.StatusForbidden) - - return ctx.JSON(errForbidden) -} diff --git a/internal/api/users.go b/internal/api/users.go deleted file mode 100644 index cc3e4df..0000000 --- a/internal/api/users.go +++ /dev/null @@ -1,9 +0,0 @@ -package api - -import ( - "github.com/gofiber/fiber/v2" -) - -func FetchAllUsersHandler(ctx *fiber.Ctx) error { - return ctx.JSON(&fiber.Map{"users": "none"}) -}