Spaces:
Paused
Paused
fix: auth middleware now accepts key query param and handles multiple auth sources
Browse files
src/internal/api/handlers/management/handler.go
CHANGED
|
@@ -218,75 +218,87 @@ func (h *Handler) Middleware() gin.HandlerFunc {
|
|
| 218 |
xKeyHeader := c.GetHeader("X-Management-Key")
|
| 219 |
fmt.Printf("[DEBUG-AUTH] %s: AuthHeaderLen=%d, XKeyHeaderLen=%d\n", clientIP, len(authHeader), len(xKeyHeader))
|
| 220 |
|
| 221 |
-
//
|
| 222 |
-
var
|
|
|
|
|
|
|
| 223 |
if ah := c.GetHeader("Authorization"); ah != "" {
|
| 224 |
parts := strings.SplitN(ah, " ", 2)
|
| 225 |
if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
|
| 226 |
-
|
| 227 |
} else {
|
| 228 |
-
|
| 229 |
}
|
| 230 |
}
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
}
|
| 234 |
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
if !localClient {
|
| 237 |
-
fmt.Printf("[DEBUG-AUTH] %s: No key provided (Headers empty)\n", clientIP)
|
| 238 |
fail()
|
| 239 |
}
|
| 240 |
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing management key"})
|
| 241 |
return
|
| 242 |
}
|
| 243 |
-
|
| 244 |
-
fmt.Printf("[DEBUG-AUTH] %s: Key Provided (len=%d). Checking against Env (len=%d) and Hash (len=%d)\n", clientIP, len(provided), len(envSecret), len(secretHash))
|
| 245 |
-
|
| 246 |
-
if localClient {
|
| 247 |
-
if lp := h.localPassword; lp != "" {
|
| 248 |
-
if subtle.ConstantTimeCompare([]byte(provided), []byte(lp)) == 1 {
|
| 249 |
-
c.Next()
|
| 250 |
-
return
|
| 251 |
-
}
|
| 252 |
-
}
|
| 253 |
-
}
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
if envSecret != "" {
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
if ai := h.failedAttempts[clientIP]; ai != nil {
|
| 262 |
-
ai.count = 0
|
| 263 |
-
ai.blockedUntil = time.Time{}
|
| 264 |
-
}
|
| 265 |
-
h.attemptsMu.Unlock()
|
| 266 |
}
|
| 267 |
-
c.Next()
|
| 268 |
-
return
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
| 282 |
-
if
|
| 283 |
if !localClient {
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
}
|
| 286 |
-
c.
|
| 287 |
return
|
| 288 |
}
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
if !localClient {
|
| 291 |
h.attemptsMu.Lock()
|
| 292 |
if ai := h.failedAttempts[clientIP]; ai != nil {
|
|
|
|
| 218 |
xKeyHeader := c.GetHeader("X-Management-Key")
|
| 219 |
fmt.Printf("[DEBUG-AUTH] %s: AuthHeaderLen=%d, XKeyHeaderLen=%d\n", clientIP, len(authHeader), len(xKeyHeader))
|
| 220 |
|
| 221 |
+
// Collect all possible keys from Headers and Query
|
| 222 |
+
var candidates []string
|
| 223 |
+
|
| 224 |
+
// 1. Authorization: Bearer <key>
|
| 225 |
if ah := c.GetHeader("Authorization"); ah != "" {
|
| 226 |
parts := strings.SplitN(ah, " ", 2)
|
| 227 |
if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
|
| 228 |
+
candidates = append(candidates, parts[1])
|
| 229 |
} else {
|
| 230 |
+
candidates = append(candidates, ah)
|
| 231 |
}
|
| 232 |
}
|
| 233 |
+
|
| 234 |
+
// 2. X-Management-Key header
|
| 235 |
+
if xk := c.GetHeader("X-Management-Key"); xk != "" {
|
| 236 |
+
candidates = append(candidates, xk)
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// 3. Query parameter "key" (fallback for browser/curl usage)
|
| 240 |
+
if qk := c.Query("key"); qk != "" {
|
| 241 |
+
candidates = append(candidates, qk)
|
| 242 |
}
|
| 243 |
|
| 244 |
+
fmt.Printf("[DEBUG-AUTH] %s: Found %d candidate keys\n", clientIP, len(candidates))
|
| 245 |
+
|
| 246 |
+
if len(candidates) == 0 {
|
| 247 |
if !localClient {
|
| 248 |
+
fmt.Printf("[DEBUG-AUTH] %s: No key provided (Headers empty, Query empty)\n", clientIP)
|
| 249 |
fail()
|
| 250 |
}
|
| 251 |
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing management key"})
|
| 252 |
return
|
| 253 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
+
// Validate against all candidates
|
| 256 |
+
authenticated := false
|
| 257 |
+
|
| 258 |
+
// 1. Check Env Secret match
|
| 259 |
if envSecret != "" {
|
| 260 |
+
for _, provided := range candidates {
|
| 261 |
+
if subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 {
|
| 262 |
+
fmt.Printf("[DEBUG-AUTH] %s: Env Secret Match! (candidate len=%d)\n", clientIP, len(provided))
|
| 263 |
+
authenticated = true
|
| 264 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
}
|
|
|
|
|
|
|
| 266 |
}
|
| 267 |
}
|
| 268 |
|
| 269 |
+
// 2. Check Hash match (if Env didn't match)
|
| 270 |
+
if !authenticated && secretHash != "" {
|
| 271 |
+
for _, provided := range candidates {
|
| 272 |
+
err := bcrypt.CompareHashAndPassword([]byte(secretHash), []byte(provided))
|
| 273 |
+
if err == nil {
|
| 274 |
+
fmt.Printf("[DEBUG-AUTH] %s: Hash Match! (candidate len=%d)\n", clientIP, len(provided))
|
| 275 |
+
authenticated = true
|
| 276 |
+
break
|
| 277 |
+
}
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
+
if authenticated {
|
| 282 |
if !localClient {
|
| 283 |
+
h.attemptsMu.Lock()
|
| 284 |
+
if ai := h.failedAttempts[clientIP]; ai != nil {
|
| 285 |
+
ai.count = 0
|
| 286 |
+
ai.blockedUntil = time.Time{}
|
| 287 |
+
}
|
| 288 |
+
h.attemptsMu.Unlock()
|
| 289 |
}
|
| 290 |
+
c.Next()
|
| 291 |
return
|
| 292 |
}
|
| 293 |
|
| 294 |
+
// If we get here, all candidates failed
|
| 295 |
+
if !localClient {
|
| 296 |
+
fmt.Printf("[DEBUG-AUTH] %s: All %d candidates failed validation.\n", clientIP, len(candidates))
|
| 297 |
+
fail()
|
| 298 |
+
}
|
| 299 |
+
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid management key"})
|
| 300 |
+
return
|
| 301 |
+
|
| 302 |
if !localClient {
|
| 303 |
h.attemptsMu.Lock()
|
| 304 |
if ai := h.failedAttempts[clientIP]; ai != nil {
|