-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Added Check API KEYs file to API.go #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ package api | |
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "strings" | ||
|
|
||
| config "github.com/go-skynet/LocalAI/api/config" | ||
|
|
@@ -144,30 +146,48 @@ func App(opts ...options.AppOption) (*fiber.App, error) { | |
|
|
||
| // Auth middleware checking if API key is valid. If no API key is set, no auth is required. | ||
| auth := func(c *fiber.Ctx) error { | ||
| if len(options.ApiKeys) > 0 { | ||
| authHeader := c.Get("Authorization") | ||
| if authHeader == "" { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Authorization header missing"}) | ||
| } | ||
| authHeaderParts := strings.Split(authHeader, " ") | ||
| if len(authHeaderParts) != 2 || authHeaderParts[0] != "Bearer" { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid Authorization header format"}) | ||
| } | ||
| if len(options.ApiKeys) == 0 { | ||
| return c.Next() | ||
| } | ||
|
|
||
| apiKey := authHeaderParts[1] | ||
| validApiKey := false | ||
| for _, key := range options.ApiKeys { | ||
| if apiKey == key { | ||
| validApiKey = true | ||
| } | ||
| // Check for api_keys.json file | ||
| fileContent, err := ioutil.ReadFile("api_keys.json") | ||
| if err == nil { | ||
| // Parse JSON content from the file | ||
| var fileKeys []string | ||
| err := json.Unmarshal(fileContent, &fileKeys) | ||
| if err != nil { | ||
| return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error parsing api_keys.json"}) | ||
| } | ||
| if !validApiKey { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid API key"}) | ||
|
|
||
| // Add file keys to options.ApiKeys | ||
| options.ApiKeys = append(options.ApiKeys, fileKeys...) | ||
| } | ||
|
|
||
| authHeader := c.Get("Authorization") | ||
| if authHeader == "" { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Authorization header missing"}) | ||
| } | ||
| authHeaderParts := strings.Split(authHeader, " ") | ||
| if len(authHeaderParts) != 2 || authHeaderParts[0] != "Bearer" { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid Authorization header format"}) | ||
| } | ||
|
|
||
| apiKey := authHeaderParts[1] | ||
| validApiKey := false | ||
| for _, key := range options.ApiKeys { | ||
| if apiKey == key { | ||
| validApiKey = true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can move the c.Next() up here - no need to check further api keys if one passes the test - at least until we need to support per-endpoint api keys or anything weird. |
||
| } | ||
| } | ||
| if !validApiKey { | ||
| return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid API key"}) | ||
| } | ||
|
|
||
| return c.Next() | ||
| } | ||
|
|
||
|
|
||
| if options.CORS { | ||
| var c func(ctx *fiber.Ctx) error | ||
| if options.CORSAllowOrigins == "" { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lunamidori5 - Do we know what the performance impact of
ioutil.ReadFileon every request will be? @mudler is more familiar with golang and would know if this is a concern or not - it might be worth some performance testing in the meantime.Personally, I would be inclined to use something like https://pkg.go.dev/github.com/fsnotify/fsnotify to watch the file and keep options.ApiKeys up-to-date - that way, we aren't hitting the filesystem on every single request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to keep it simple we can also just load the file once on start and pass the api keys down the line as a list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mudler needs to be reloaded on request so we dont have to restart the program everytime is the goal, this way we can add keys as its running
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess? I dont know much about GO...