feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/modules/auth"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -78,6 +79,11 @@ func Auth(jwtManager *auth.JWTManager, validate UserTokenValidatorFunc) gin.Hand
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !userTokenTenantMatches(c, claims.TenantID) {
|
||||
response.Unauthorized(c, "访问令牌不属于当前站点")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if validate != nil {
|
||||
if err := validate(c.Request.Context(), claims.UserID, claims.TokenVersion); err != nil {
|
||||
if errors.Is(err, auth.ErrDependencyUnavailable) {
|
||||
@@ -114,6 +120,10 @@ func OptionalAuth(jwtManager *auth.JWTManager, validate UserTokenValidatorFunc)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if !userTokenTenantMatches(c, claims.TenantID) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if validate != nil {
|
||||
if err := validate(c.Request.Context(), claims.UserID, claims.TokenVersion); err != nil {
|
||||
c.Next()
|
||||
@@ -132,26 +142,30 @@ func AuthUserOrAdmin(jwtManager *auth.JWTManager, userValidate UserTokenValidato
|
||||
// 1. 优先尝试管理员鉴权(Header Bearer 或 Admin Cookie)
|
||||
if tokenText, tokenSource := extractAdminToken(c); tokenText != "" {
|
||||
if claims, err := jwtManager.ParseSubject(tokenText, "access", "admin"); err == nil {
|
||||
username := claims.Phone
|
||||
passwordMustChange := false
|
||||
validAdmin := true
|
||||
if adminValidate != nil {
|
||||
tokenContext, err := adminValidate(c.Request.Context(), claims.UserID, claims.TokenVersion)
|
||||
if err != nil {
|
||||
RecordAdminAuthFailure(c, "invalid", tokenSource, 0, 0)
|
||||
validAdmin = false
|
||||
} else {
|
||||
username = tokenContext.Username
|
||||
passwordMustChange = tokenContext.PasswordMustChange
|
||||
if !adminTokenTenantMatches(c, claims.TenantID) {
|
||||
RecordAdminAuthFailure(c, "tenant_mismatch", tokenSource, 0, 0)
|
||||
} else {
|
||||
username := claims.Phone
|
||||
passwordMustChange := false
|
||||
validAdmin := true
|
||||
if adminValidate != nil {
|
||||
tokenContext, err := adminValidate(c.Request.Context(), claims.UserID, claims.TokenVersion)
|
||||
if err != nil {
|
||||
RecordAdminAuthFailure(c, "invalid", tokenSource, 0, 0)
|
||||
validAdmin = false
|
||||
} else {
|
||||
username = tokenContext.Username
|
||||
passwordMustChange = tokenContext.PasswordMustChange
|
||||
}
|
||||
}
|
||||
if validAdmin {
|
||||
c.Set(ContextAdminID, claims.UserID)
|
||||
c.Request = c.Request.WithContext(logging.WithAdminID(c.Request.Context(), claims.UserID))
|
||||
c.Set(ContextUsername, username)
|
||||
c.Set(ContextPasswordMustChange, passwordMustChange)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
if validAdmin {
|
||||
c.Set(ContextAdminID, claims.UserID)
|
||||
c.Request = c.Request.WithContext(logging.WithAdminID(c.Request.Context(), claims.UserID))
|
||||
c.Set(ContextUsername, username)
|
||||
c.Set(ContextPasswordMustChange, passwordMustChange)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,6 +189,11 @@ func AuthUserOrAdmin(jwtManager *auth.JWTManager, userValidate UserTokenValidato
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !userTokenTenantMatches(c, claims.TenantID) {
|
||||
response.Unauthorized(c, "访问令牌不属于当前站点")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if userValidate != nil {
|
||||
if err := userValidate(c.Request.Context(), claims.UserID, claims.TokenVersion); err != nil {
|
||||
if errors.Is(err, auth.ErrDependencyUnavailable) {
|
||||
@@ -193,6 +212,14 @@ func AuthUserOrAdmin(jwtManager *auth.JWTManager, userValidate UserTokenValidato
|
||||
}
|
||||
}
|
||||
|
||||
func userTokenTenantMatches(c *gin.Context, tokenTenantID uint64) bool {
|
||||
requestTenantID := tenancy.IDFromContext(c.Request.Context())
|
||||
if tokenTenantID == 0 {
|
||||
return true
|
||||
}
|
||||
return requestTenantID > 0 && requestTenantID == tokenTenantID
|
||||
}
|
||||
|
||||
func AdminAuth(jwtManager *auth.JWTManager, validate AdminTokenValidatorFunc) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
tokenText, tokenSource := extractAdminToken(c)
|
||||
@@ -210,6 +237,12 @@ func AdminAuth(jwtManager *auth.JWTManager, validate AdminTokenValidatorFunc) gi
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !adminTokenTenantMatches(c, claims.TenantID) {
|
||||
RecordAdminAuthFailure(c, "tenant_mismatch", tokenSource, 0, 0)
|
||||
response.Unauthorized(c, "后台访问令牌不属于当前站点")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Set(ContextAdminID, claims.UserID)
|
||||
c.Request = c.Request.WithContext(logging.WithAdminID(c.Request.Context(), claims.UserID))
|
||||
username := claims.Phone
|
||||
@@ -238,6 +271,14 @@ func AdminAuth(jwtManager *auth.JWTManager, validate AdminTokenValidatorFunc) gi
|
||||
}
|
||||
}
|
||||
|
||||
func adminTokenTenantMatches(c *gin.Context, tokenTenantID uint64) bool {
|
||||
requestTenantID := tenancy.IDFromContext(c.Request.Context())
|
||||
if tokenTenantID == 0 {
|
||||
return true
|
||||
}
|
||||
return requestTenantID > 0 && requestTenantID == tokenTenantID
|
||||
}
|
||||
|
||||
type authFailureReasonCarrier interface {
|
||||
AuthFailureReason() string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user