fix(auth): 修复历史分销用户登录后立即退出
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/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
@@ -217,7 +218,11 @@ func userTokenTenantMatches(c *gin.Context, tokenTenantID uint64) bool {
|
||||
if tokenTenantID == 0 {
|
||||
return true
|
||||
}
|
||||
return requestTenantID > 0 && requestTenantID == tokenTenantID
|
||||
if requestTenantID > 0 {
|
||||
return requestTenantID == tokenTenantID
|
||||
}
|
||||
// 历史分销用户的 tenant_id 可能仍指向大锤,具体归属由令牌校验时的站点过滤确认。
|
||||
return sitecode.BoundSiteCodeFromContext(c.Request.Context()) != ""
|
||||
}
|
||||
|
||||
func AdminAuth(jwtManager *auth.JWTManager, validate AdminTokenValidatorFunc) gin.HandlerFunc {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/modules/auth"
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -20,6 +21,14 @@ func useRequestTenant(tenantID uint64) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func useBoundSiteCode(code string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := sitecode.WithBoundSiteCode(c.Request.Context(), code)
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAuthRejectsQueryToken(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
manager := auth.NewJWTManager("test-jwt-secret-for-query-token-rejection")
|
||||
@@ -111,3 +120,39 @@ func TestUserAndAdminTokensRejectCrossTenantRequests(t *testing.T) {
|
||||
t.Fatalf("cross-tenant admin token status = %d, want 401", adminResponse.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAuthAllowsLegacyTenantTokenOnBoundDistributionSite(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
manager := auth.NewJWTManager("legacy-distribution-auth-test-secret")
|
||||
pair, err := manager.GenerateTenantPairWithVersion(1, "13800000000", "user", 1, 101)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
newEngine := func(boundSiteCode string) *gin.Engine {
|
||||
engine := gin.New()
|
||||
if boundSiteCode != "" {
|
||||
engine.Use(useBoundSiteCode(boundSiteCode))
|
||||
}
|
||||
engine.GET("/protected", Auth(manager, nil), func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
return engine
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
request.Header.Set("Authorization", "Bearer "+pair.AccessToken)
|
||||
boundResponse := httptest.NewRecorder()
|
||||
newEngine("legacy_site_a").ServeHTTP(boundResponse, request)
|
||||
if boundResponse.Code != http.StatusNoContent {
|
||||
t.Fatalf("bound distribution token status = %d, want 204", boundResponse.Code)
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
request.Header.Set("Authorization", "Bearer "+pair.AccessToken)
|
||||
unboundResponse := httptest.NewRecorder()
|
||||
newEngine("").ServeHTTP(unboundResponse, request)
|
||||
if unboundResponse.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unbound distribution token status = %d, want 401", unboundResponse.Code)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +77,22 @@ func SiteDomainFallback(resolver SiteDomainResolver, logger *zap.Logger) gin.Han
|
||||
}
|
||||
// 已登记的站点域名是权威来源。即使前端带着旧站点头或 query,
|
||||
// 也必须先确认当前 Host 属于哪个分销站点,否则后续 known-host 校验会误拒绝请求。
|
||||
if code := resolveSiteCodeByHost(c, resolver, cache, logger); code != "" {
|
||||
hostCode := resolveSiteCodeByHost(c, resolver, cache, logger)
|
||||
code := hostCode
|
||||
if code != "" {
|
||||
c.Request.Header.Del(sitecode.LegacyHeaderName)
|
||||
c.Request.Header.Set(sitecode.HeaderName, code)
|
||||
c.Request = c.Request.WithContext(sitecode.WithDomainScope(c.Request.Context()))
|
||||
} else {
|
||||
// 未登记域名仍允许通过显式站点参数访问预览或开发环境,
|
||||
// 但后续鉴权会继续校验令牌用户是否确实属于该站点。
|
||||
code = sitecode.FromRequest(c)
|
||||
}
|
||||
if code != "" {
|
||||
ctx := sitecode.WithBoundSiteCode(c.Request.Context(), code)
|
||||
if hostCode != "" {
|
||||
ctx = sitecode.WithDomainScope(ctx)
|
||||
}
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
||||
@@ -71,6 +71,24 @@ func TestSiteDomainFallbackResolvesByHost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSiteDomainFallbackBindsResolvedSiteCodeToContext(t *testing.T) {
|
||||
resolver := &fakeSiteDomainResolver{byDomain: map[string]string{"tysh.hao858.com": "gz_dist"}}
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(SiteDomainFallback(resolver, nil))
|
||||
engine.GET("/probe", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, sitecode.BoundSiteCodeFromContext(c.Request.Context()))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
|
||||
req.Host = "tysh.hao858.com"
|
||||
recorder := httptest.NewRecorder()
|
||||
engine.ServeHTTP(recorder, req)
|
||||
if got := recorder.Body.String(); got != "gz_dist" {
|
||||
t.Fatalf("bound site code = %q, want gz_dist", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSiteDomainFallbackKnownHostOverridesExplicitSiteCode(t *testing.T) {
|
||||
resolver := &fakeSiteDomainResolver{byDomain: map[string]string{"tysh.hao858.com": "gz_dist"}}
|
||||
engine := newSiteFallbackRouter(resolver)
|
||||
|
||||
Reference in New Issue
Block a user