Skip to content

Modern Docker Management, Designed for Everyone

fix: api key validator not checking for user correctly for admin endpoints

Kyle Mendelldad577a4c07e
diff --git a/backend/internal/huma/huma.go b/backend/internal/huma/huma.go
index b7f3c7ae2..13751354b 100644
--- a/backend/internal/huma/huma.go
+++ b/backend/internal/huma/huma.go
@@ -186,7 +186,7 @@ func SetupAPI(router *gin.Engine, apiGroup *gin.RouterGroup, cfg *config.Config,
api := humagin.NewWithGroup(router, apiGroup, humaConfig)
// Add authentication middleware
- api.UseMiddleware(middleware.NewAuthBridge(svc.Auth, cfg))
+ api.UseMiddleware(middleware.NewAuthBridge(svc.Auth, svc.ApiKey, cfg))
// Register all Huma handlers
registerHandlers(api, svc)
diff --git a/backend/internal/huma/middleware/auth.go b/backend/internal/huma/middleware/auth.go
index ec2757169..031d6f412 100644
--- a/backend/internal/huma/middleware/auth.go
+++ b/backend/internal/huma/middleware/auth.go
@@ -88,8 +88,18 @@ func tryBearerAuth(ctx huma.Context, authService *services.AuthService) (*models
}
// tryApiKeyAuth checks if API key authentication should be allowed through.
-func tryApiKeyAuth(ctx huma.Context) bool {
- return ctx.Header(headerApiKey) != ""
+func tryApiKeyAuth(ctx huma.Context, apiKeyService *services.ApiKeyService) (*models.User, bool) {
+ apiKey := ctx.Header(headerApiKey)
+ if apiKey == "" {
+ return nil, false
+ }
+
+ user, err := apiKeyService.ValidateApiKey(ctx.Context(), apiKey)
+ if err != nil || user == nil {
+ return nil, false
+ }
+
+ return user, true
}
// tryAgentAuth checks if the request is from an authenticated agent.
@@ -128,7 +138,7 @@ func createAgentSudoUser() *models.User {
// NewAuthBridge creates a Huma middleware that validates JWT tokens and
// enforces security requirements defined on operations.
-func NewAuthBridge(authService *services.AuthService, cfg *config.Config) func(ctx huma.Context, next func(huma.Context)) {
+func NewAuthBridge(authService *services.AuthService, apiKeyService *services.ApiKeyService, cfg *config.Config) func(ctx huma.Context, next func(huma.Context)) {
return func(ctx huma.Context, next func(huma.Context)) {
if authService == nil {
next(ctx)
@@ -160,9 +170,13 @@ func NewAuthBridge(authService *services.AuthService, cfg *config.Config) func(c
}
}
- if reqs.apiKeyAuth && tryApiKeyAuth(ctx) {
- next(ctx)
- return
+ if reqs.apiKeyAuth {
+ if user, ok := tryApiKeyAuth(ctx, apiKeyService); ok {
+ newCtx := setUserInContext(ctx.Context(), user)
+ ctx = huma.WithContext(ctx, newCtx)
+ next(ctx)
+ return
+ }
}
// Write unauthorized response directly