From 2eb6865a27c7ce9f22fac12eb0e060e22e8d6f02 Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 5 Dec 2023 05:35:27 -0500 Subject: [PATCH] Fix: API Key / JSON Fast Follow #1 (#1388) fast follow fix #1 - imports, final loop, one last chance to skip Co-authored-by: lunamidori5 <118759930+lunamidori5@users.noreply.github.com> --- api/api.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/api/api.go b/api/api.go index 4970623e..99d52435 100644 --- a/api/api.go +++ b/api/api.go @@ -1,10 +1,10 @@ package api import ( + "encoding/json" "errors" "fmt" - "encoding/json" - "io/ioutil" + "os" "strings" config "github.com/go-skynet/LocalAI/api/config" @@ -151,7 +151,7 @@ func App(opts ...options.AppOption) (*fiber.App, error) { } // Check for api_keys.json file - fileContent, err := ioutil.ReadFile("api_keys.json") + fileContent, err := os.ReadFile("api_keys.json") if err == nil { // Parse JSON content from the file var fileKeys []string @@ -164,6 +164,10 @@ func App(opts ...options.AppOption) (*fiber.App, error) { options.ApiKeys = append(options.ApiKeys, fileKeys...) } + if len(options.ApiKeys) == 0 { + return c.Next() + } + authHeader := c.Get("Authorization") if authHeader == "" { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Authorization header missing"}) @@ -174,20 +178,16 @@ func App(opts ...options.AppOption) (*fiber.App, error) { } apiKey := authHeaderParts[1] - validApiKey := false for _, key := range options.ApiKeys { if apiKey == key { - validApiKey = true + return c.Next() } } - if !validApiKey { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid API key"}) - } - return c.Next() + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid API key"}) + } - if options.CORS { var c func(ctx *fiber.Ctx) error if options.CORSAllowOrigins == "" {