Skip to content

Commit 8306f34

Browse files
committed
parse Authorization header first for OpenAPI
1 parent 42faae2 commit 8306f34

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

pkg/server/server.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -661,28 +661,28 @@ func (s *ClickHouseJWEServer) OpenAPIHandler(w http.ResponseWriter, r *http.Requ
661661
return
662662
}
663663

664-
// Try to extract token from URL path first
665-
pathParts := strings.Split(r.URL.Path, "/")
666664
var token string
667-
for i, part := range pathParts {
668-
if part == "openapi" && i > 0 {
669-
token = pathParts[i-1]
670-
break
671-
}
665+
// try get token from Authorization header
666+
authHeader := r.Header.Get("Authorization")
667+
if strings.HasPrefix(authHeader, "Bearer ") {
668+
token = strings.TrimPrefix(authHeader, "Bearer ")
669+
} else if strings.HasPrefix(authHeader, "Basic ") {
670+
token = strings.TrimPrefix(authHeader, "Basic ")
672671
}
673672

674-
// If no token from path or token from OpenAI GPT tester, try other sources
675-
if token == "" || token == "default" {
676-
authHeader := r.Header.Get("Authorization")
677-
if strings.HasPrefix(authHeader, "Bearer ") {
678-
token = strings.TrimPrefix(authHeader, "Bearer ")
679-
} else if strings.HasPrefix(authHeader, "Basic ") {
680-
token = strings.TrimPrefix(authHeader, "Basic ")
681-
}
673+
// Try x-altinity-mcp-key header
674+
if token == "" {
675+
token = r.Header.Get("x-altinity-mcp-key")
676+
}
682677

683-
// Try x-altinity-mcp-key header
684-
if token == "" {
685-
token = r.Header.Get("x-altinity-mcp-key")
678+
// Try to extract token from URL path first
679+
if token == "" {
680+
pathParts := strings.Split(r.URL.Path, "/")
681+
for i, part := range pathParts {
682+
if part == "openapi" && i > 0 {
683+
token = pathParts[i-1]
684+
break
685+
}
686686
}
687687
}
688688

@@ -713,11 +713,11 @@ func (s *ClickHouseJWEServer) OpenAPIHandler(w http.ResponseWriter, r *http.Requ
713713
s.handleExecuteQueryOpenAPI(w, r, token)
714714
default:
715715
// Serve OpenAPI schema by default
716-
s.serveOpenAPISchema(w, r, hostURL, token)
716+
s.serveOpenAPISchema(w, r, hostURL)
717717
}
718718
}
719719

720-
func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.Request, hostURL, token string) {
720+
func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.Request, hostURL string) {
721721
schema := map[string]interface{}{
722722
"openapi": "3.1.0",
723723
"info": map[string]interface{}{
@@ -749,7 +749,7 @@ func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.
749749
"type": "string",
750750
},
751751
"x-oai-meta": map[string]interface{}{"securityType": "user_api_key"},
752-
"default": token,
752+
"default": "default",
753753
},
754754
{
755755
"name": "database",
@@ -806,7 +806,7 @@ func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.
806806
"type": "string",
807807
},
808808
"x-oai-meta": map[string]interface{}{"securityType": "user_api_key"},
809-
"default": token,
809+
"default": "default",
810810
},
811811
{
812812
"name": "query",
@@ -819,7 +819,7 @@ func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.
819819
"name": "limit",
820820
"in": "query",
821821
"required": false,
822-
"description": "Max rows to return (default 1000, max 10000).",
822+
"description": "Max rows to return (default 1000, max 100000).",
823823
"schema": map[string]interface{}{"type": "integer"},
824824
},
825825
},
@@ -849,7 +849,7 @@ func (s *ClickHouseJWEServer) serveOpenAPISchema(w http.ResponseWriter, _ *http.
849849
"type": "string",
850850
},
851851
"x-oai-meta": map[string]interface{}{"securityType": "user_api_key"},
852-
"default": token,
852+
"default": "default",
853853
},
854854
{
855855
"name": "database",
@@ -989,8 +989,8 @@ func (s *ClickHouseJWEServer) handleExecuteQueryOpenAPI(w http.ResponseWriter, r
989989
http.Error(w, "Invalid limit parameter", http.StatusBadRequest)
990990
return
991991
}
992-
if limit > 10000 {
993-
http.Error(w, "Limit cannot exceed 10000", http.StatusBadRequest)
992+
if limit > s.Config.ClickHouse.Limit {
993+
http.Error(w, fmt.Sprintf("Limit cannot exceed %s", s.Config.ClickHouse.Limit), http.StatusBadRequest)
994994
return
995995
}
996996
}

0 commit comments

Comments
 (0)