mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 06:16:37 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
39 lines
821 B
Go
39 lines
821 B
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cap
|
|
|
|
import (
|
|
"Wavelet/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
if mgr == nil {
|
|
response.AbortUnauthorized(c, errCapTokenInvalidOrExpired)
|
|
return
|
|
}
|
|
|
|
token := c.GetHeader("X-Cap-Token")
|
|
if token == "" {
|
|
response.AbortUnauthorized(c, errCapTokenMissing)
|
|
return
|
|
}
|
|
|
|
valid, err := mgr.VerifyToken(c.Request.Context(), token, scope)
|
|
if err != nil || !valid {
|
|
response.AbortUnauthorized(c, errCapTokenInvalidOrExpired)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|