fix(auth): 修复历史分销用户登录后立即退出
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user