135 lines
3.0 KiB
Go
135 lines
3.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
"github.com/beego/beego/v2/server/web/context"
|
|
)
|
|
|
|
const planMappingSeparator = ":"
|
|
|
|
var (
|
|
errPlanNoToken = errors.New("未提供认证信息")
|
|
errPlanBadToken = errors.New("认证信息格式错误")
|
|
|
|
planOnce sync.Once
|
|
planMode = AuthModeWarn
|
|
planRoutes []planRoute
|
|
)
|
|
|
|
type planRoute struct {
|
|
prefix string
|
|
module string
|
|
}
|
|
|
|
func planInit() {
|
|
planOnce.Do(func() {
|
|
if v, err := beego.AppConfig.String("plan_enforce"); err == nil {
|
|
switch strings.ToLower(strings.TrimSpace(v)) {
|
|
case AuthModeOff, AuthModeWarn, AuthModeOn:
|
|
planMode = strings.ToLower(strings.TrimSpace(v))
|
|
}
|
|
}
|
|
|
|
raw, _ := beego.AppConfig.String("plan_module_paths")
|
|
for _, item := range strings.Split(raw, ",") {
|
|
item = strings.TrimSpace(item)
|
|
if item == "" {
|
|
continue
|
|
}
|
|
parts := strings.SplitN(item, planMappingSeparator, 2)
|
|
if len(parts) != 2 {
|
|
log.Printf("[plan] 忽略无效映射(应为 前缀:模块编码): %s", item)
|
|
continue
|
|
}
|
|
prefix := strings.TrimSpace(parts[0])
|
|
module := services.NormalizeModuleCode(parts[1])
|
|
if prefix == "" || module == "" {
|
|
log.Printf("[plan] 忽略无效映射(前缀或模块编码为空): %s", item)
|
|
continue
|
|
}
|
|
planRoutes = append(planRoutes, planRoute{prefix: prefix, module: module})
|
|
}
|
|
|
|
sort.Slice(planRoutes, func(i, j int) bool {
|
|
return len(planRoutes[i].prefix) > len(planRoutes[j].prefix)
|
|
})
|
|
log.Printf("[plan] 模块套餐鉴权模式=%s,接口映射=%d 条", planMode, len(planRoutes))
|
|
})
|
|
}
|
|
|
|
func planModuleOf(path string) string {
|
|
for _, r := range planRoutes {
|
|
if strings.HasPrefix(path, r.prefix) {
|
|
return r.module
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func PlanAuthMiddleware() beego.FilterFunc {
|
|
return func(ctx *context.Context) {
|
|
planInit()
|
|
if planMode == AuthModeOff || len(planRoutes) == 0 {
|
|
return
|
|
}
|
|
if ctx.Input.Method() == http.MethodOptions {
|
|
return
|
|
}
|
|
|
|
path := cleanPath(ctx.Request.RequestURI)
|
|
if !isProtected(path) || isWhitelisted(path) {
|
|
return
|
|
}
|
|
|
|
module := planModuleOf(path)
|
|
if module == "" {
|
|
return
|
|
}
|
|
|
|
claims, err := planClaims(ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if claims.TenantId <= 0 {
|
|
return
|
|
}
|
|
if services.IsModuleEnabled(uint64(claims.TenantId), module) {
|
|
return
|
|
}
|
|
|
|
reason := "模块未开通: " + module
|
|
if planMode == AuthModeWarn {
|
|
warnLog(path, reason)
|
|
return
|
|
}
|
|
|
|
ctx.Output.SetStatus(http.StatusForbidden)
|
|
_ = ctx.Output.JSON(map[string]interface{}{
|
|
"code": 403,
|
|
"msg": "该功能未开通,请先购买对应套餐",
|
|
}, false, false)
|
|
}
|
|
}
|
|
|
|
func planClaims(ctx *context.Context) (*jwtutil.Claims, error) {
|
|
authHeader := ctx.Request.Header.Get("Authorization")
|
|
if authHeader == "" {
|
|
return nil, errPlanNoToken
|
|
}
|
|
parts := strings.SplitN(authHeader, " ", 2)
|
|
if len(parts) != 2 || parts[0] != "Bearer" {
|
|
return nil, errPlanBadToken
|
|
}
|
|
return jwtutil.ParseToken(parts[1])
|
|
}
|