mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
9550fa6ff3
Replace per-request GetByKey calls with a singleflight-backed RuntimeSettings snapshot loaded via ListSystemConfigsByKeys. Invalidate snapshot on admin cap_* writes and via Redis pub/sub. Simplify VerifyMiddleware and update PERFORMANCE.md status.
36 lines
832 B
Go
36 lines
832 B
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cap
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/common/response"
|
|
)
|
|
|
|
// VerifyMiddleware returns a Gin middleware that checks and consumes the X-Cap-Token header.
|
|
func VerifyMiddleware(mgr *Manager, scope string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if !ProtectionEnabled(c.Request.Context()) {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
token := c.GetHeader("X-Cap-Token")
|
|
if token == "" {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, response.Err(errCapTokenMissing))
|
|
return
|
|
}
|
|
|
|
valid, err := mgr.VerifyToken(c.Request.Context(), token, scope)
|
|
if err != nil || !valid {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, response.Err(errCapTokenInvalidOrExpired))
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
} |