Files
Fabric/internal/server/auth.go
Kayvan Sylvan c06c94f8b8 # CHANGES
- Add Swagger UI at `/swagger/index.html` endpoint
- Generate OpenAPI spec files (JSON and YAML)
- Document chat, patterns, and models endpoints
- Update contributing guide with Swagger annotation instructions
- Add swaggo dependencies to project
- Configure authentication bypass for Swagger documentation
- Add custom YAML handler for OpenAPI spec
- Update REST API documentation with Swagger links
- Add dictionary entries for new tools
2025-12-18 07:12:08 -08:00

39 lines
950 B
Go

package restapi
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
const APIKeyHeader = "X-API-Key"
// APIKeyMiddleware validates API key for protected endpoints.
// Swagger documentation endpoints (/swagger/*) are exempt from authentication
// to allow users to browse and test the API documentation freely.
func APIKeyMiddleware(apiKey string) gin.HandlerFunc {
return func(c *gin.Context) {
// Skip authentication for Swagger documentation endpoints
// This allows public access to API docs even when authentication is enabled
if strings.HasPrefix(c.Request.URL.Path, "/swagger/") {
c.Next()
return
}
headerApiKey := c.GetHeader(APIKeyHeader)
if headerApiKey == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Missing API Key"})
return
}
if headerApiKey != apiKey {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Wrong API Key"})
return
}
c.Next()
}
}