added multi-key support for optional auth logic
This commit is contained in:
+14
-9
@@ -14,8 +14,8 @@ import (
|
||||
|
||||
var httpClient = &http.Client{}
|
||||
|
||||
func checkAuth(expectedToken string, r *http.Request) bool {
|
||||
if expectedToken == "" {
|
||||
func checkAuth(expectedTokens []string, r *http.Request) bool {
|
||||
if expectedTokens == nil {
|
||||
return true
|
||||
}
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
@@ -23,7 +23,12 @@ func checkAuth(expectedToken string, r *http.Request) bool {
|
||||
return false
|
||||
}
|
||||
token := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
return token == expectedToken
|
||||
for _, expected := range expectedTokens {
|
||||
if token == expected {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func sendUnauthorized(w http.ResponseWriter) {
|
||||
@@ -39,7 +44,7 @@ func sendUnauthorized(w http.ResponseWriter) {
|
||||
})
|
||||
}
|
||||
|
||||
func handleModels(cm *ConfigManager, expectedToken string) http.HandlerFunc {
|
||||
func handleModels(cm *ConfigManager, expectedTokens []string) http.HandlerFunc {
|
||||
type ModelData struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
@@ -53,7 +58,7 @@ func handleModels(cm *ConfigManager, expectedToken string) http.HandlerFunc {
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkAuth(expectedToken, r) {
|
||||
if !checkAuth(expectedTokens, r) {
|
||||
sendUnauthorized(w)
|
||||
return
|
||||
}
|
||||
@@ -91,9 +96,9 @@ func handleModels(cm *ConfigManager, expectedToken string) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func handleChatCompletions(cm *ConfigManager, expectedToken string) http.HandlerFunc {
|
||||
func handleChatCompletions(cm *ConfigManager, expectedTokens []string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkAuth(expectedToken, r) {
|
||||
if !checkAuth(expectedTokens, r) {
|
||||
sendUnauthorized(w)
|
||||
return
|
||||
}
|
||||
@@ -324,9 +329,9 @@ func handleChatCompletions(cm *ConfigManager, expectedToken string) http.Handler
|
||||
}
|
||||
}
|
||||
|
||||
func handleImageGenerations(cm *ConfigManager, expectedToken string) http.HandlerFunc {
|
||||
func handleImageGenerations(cm *ConfigManager, expectedTokens []string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkAuth(expectedToken, r) {
|
||||
if !checkAuth(expectedTokens, r) {
|
||||
sendUnauthorized(w)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user