chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,47 @@
|
||||
// Package middleware: authentication for browser WebSocket upgrades.
|
||||
//
|
||||
// Browsers cannot attach custom headers (Authorization / X-API-Key) to a
|
||||
// WebSocket handshake. The sandbox terminal mints a short-lived ticket over
|
||||
// a normal authenticated POST and presents it as the ticket query parameter;
|
||||
// AttachAuthenticatedUser then installs the same auth session Auth would,
|
||||
// given the user the handler already resolved from that ticket.
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"academy/internal/config"
|
||||
"academy/internal/types"
|
||||
"academy/internal/types/interfaces"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AttachAuthenticatedUser installs the same auth session the Auth middleware
|
||||
// would, given an already-resolved user and tenant. On success the request
|
||||
// carries the resolved auth session and true is returned; on failure the
|
||||
// response has already been written and false is returned.
|
||||
//
|
||||
// The tenant is taken from jwtTenantID unless the caller already set
|
||||
// X-Tenant-ID, mirroring how Auth honours that header. Membership and role
|
||||
// are still resolved from the database by authenticateJWTUser, so passing a
|
||||
// user here grants nothing on its own.
|
||||
func AttachAuthenticatedUser(
|
||||
c *gin.Context,
|
||||
tenantService interfaces.TenantService,
|
||||
memberService interfaces.TenantMemberService,
|
||||
cfg *config.Config,
|
||||
user *types.User,
|
||||
jwtTenantID uint64,
|
||||
) bool {
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized: invalid or expired token"})
|
||||
c.Abort()
|
||||
return false
|
||||
}
|
||||
if jwtTenantID != 0 && strings.TrimSpace(c.GetHeader("X-Tenant-ID")) == "" {
|
||||
c.Request.Header.Set("X-Tenant-ID", strconv.FormatUint(jwtTenantID, 10))
|
||||
}
|
||||
return authenticateJWTUser(c, tenantService, memberService, cfg, user, jwtTenantID)
|
||||
}
|
||||
Reference in new issue
Block a user