feat(embed): scope OAuth and sessions per visitor principal

Add embed visitor principals (X-Embed-Visitor), widen sessions.user_id,
embed MCP OAuth/approval routes, OAuth callback context detachment, and
CloneContext principal preservation for async QA streams.
This commit is contained in:
wizardchen
2026-06-30 18:16:44 +08:00
committed by lyingbug
parent 168b1d4cfe
commit 5074344c16
23 changed files with 438 additions and 43 deletions
+93 -15
View File
@@ -26,11 +26,13 @@ import (
// EmbedChannelHandler manages web embed channel CRUD and public embed endpoints.
type EmbedChannelHandler struct {
embedSvc interfaces.EmbedChannelService
sessionService interfaces.SessionService
sessionHandler *session.Handler
messageHandler *MessageHandler
redis *redis.Client
embedSvc interfaces.EmbedChannelService
sessionService interfaces.SessionService
sessionHandler *session.Handler
messageHandler *MessageHandler
mcpOAuthHandler *MCPOAuthHandler
mcpServiceHandler *MCPServiceHandler
redis *redis.Client
}
func NewEmbedChannelHandler(
@@ -38,14 +40,18 @@ func NewEmbedChannelHandler(
sessionService interfaces.SessionService,
sessionHandler *session.Handler,
messageHandler *MessageHandler,
mcpOAuthHandler *MCPOAuthHandler,
mcpServiceHandler *MCPServiceHandler,
redisClient *redis.Client,
) *EmbedChannelHandler {
return &EmbedChannelHandler{
embedSvc: embedSvc,
sessionService: sessionService,
sessionHandler: sessionHandler,
messageHandler: messageHandler,
redis: redisClient,
embedSvc: embedSvc,
sessionService: sessionService,
sessionHandler: sessionHandler,
messageHandler: messageHandler,
mcpOAuthHandler: mcpOAuthHandler,
mcpServiceHandler: mcpServiceHandler,
redis: redisClient,
}
}
@@ -422,6 +428,12 @@ func (h *EmbedChannelHandler) CreateEmbedSession(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create session"})
return
}
ownerID := types.EmbedSessionPrincipal(tenantID, ch.ID, created.ID).StorageID()
if err := h.sessionService.SetSessionOwnerID(ctx, tenantID, created.ID, ownerID); err != nil {
logger.Warnf(ctx, "failed to assign embed session owner for %s: %v", created.ID, err)
} else {
created.UserID = ownerID
}
// Hand back a signed handle bound to this session; the widget must echo it
// (X-Embed-Session header) on every subsequent load/chat call.
sig := service.SignEmbedSessionHandle(ch, created.ID)
@@ -450,6 +462,61 @@ func (h *EmbedChannelHandler) EmbedStopSession(c *gin.Context) {
h.sessionHandler.StopSession(c)
}
func (h *EmbedChannelHandler) EmbedResolveMCPOAuth(c *gin.Context) {
if err := h.ensureEmbedSession(c); err != nil {
return
}
if h.mcpOAuthHandler == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "oauth handler unavailable"})
return
}
h.mcpOAuthHandler.ResolveMCPOAuth(c)
}
func (h *EmbedChannelHandler) EmbedCancelMCPOAuth(c *gin.Context) {
if err := h.ensureEmbedSession(c); err != nil {
return
}
if h.mcpOAuthHandler == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "oauth handler unavailable"})
return
}
h.mcpOAuthHandler.CancelMCPOAuth(c)
}
func (h *EmbedChannelHandler) EmbedMCPOAuthAuthorizeURL(c *gin.Context) {
if err := h.ensureEmbedSession(c); err != nil {
return
}
if h.mcpOAuthHandler == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "oauth handler unavailable"})
return
}
h.mcpOAuthHandler.AuthorizeURL(c)
}
func (h *EmbedChannelHandler) EmbedMCPOAuthStatus(c *gin.Context) {
if err := h.ensureEmbedSession(c); err != nil {
return
}
if h.mcpOAuthHandler == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "oauth handler unavailable"})
return
}
h.mcpOAuthHandler.Status(c)
}
func (h *EmbedChannelHandler) EmbedResolveToolApproval(c *gin.Context) {
if err := h.ensureEmbedSession(c); err != nil {
return
}
if h.mcpServiceHandler == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "tool approval handler unavailable"})
return
}
h.mcpServiceHandler.ResolveToolApproval(c)
}
type embedWebhookEventRequest struct {
Type string `json:"type"`
SessionID string `json:"session_id"`
@@ -535,7 +602,7 @@ func (h *EmbedChannelHandler) ensureEmbedSession(c *gin.Context) error {
c.JSON(http.StatusBadRequest, gin.H{"error": "session_id is required"})
return apperrors.NewBadRequestError("session_id is required")
}
sess, err := h.sessionService.GetSession(c.Request.Context(), sessionID)
sess, err := h.sessionService.GetSessionByID(c.Request.Context(), ch.TenantID, sessionID)
if err != nil || sess == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "session not found"})
return apperrors.NewNotFoundError("session not found")
@@ -545,6 +612,12 @@ func (h *EmbedChannelHandler) ensureEmbedSession(c *gin.Context) error {
c.JSON(http.StatusForbidden, gin.H{"error": "session not allowed for this embed channel"})
return apperrors.NewForbiddenError("session not allowed")
}
ownerID := types.EmbedSessionPrincipal(ch.TenantID, ch.ID, sessionID).StorageID()
if strings.TrimSpace(sess.UserID) == "" {
if err := h.sessionService.SetSessionOwnerID(c.Request.Context(), ch.TenantID, sessionID, ownerID); err != nil {
logger.Warnf(c.Request.Context(), "failed to backfill embed session owner for %s: %v", sessionID, err)
}
}
// Require the signed handle minted at creation. This is the per-visitor
// authorization secret: knowing the session id alone (e.g. from a leaked
// access log) is insufficient without the matching signature.
@@ -553,12 +626,17 @@ func (h *EmbedChannelHandler) ensureEmbedSession(c *gin.Context) error {
c.JSON(http.StatusForbidden, gin.H{"error": "session signature invalid"})
return apperrors.NewForbiddenError("session signature invalid")
}
principal := types.Principal{
Type: types.PrincipalEmbedSession,
ID: fmt.Sprintf("%d:%s:%s", ch.TenantID, ch.ID, sessionID),
principal := types.EmbedSessionPrincipal(ch.TenantID, ch.ID, sessionID)
ctx := c.Request.Context()
if visitorID := strings.TrimSpace(c.GetHeader(types.EmbedVisitorHeader)); visitorID != "" {
if err := types.ValidateEmbedVisitorID(visitorID); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid embed visitor id"})
return apperrors.NewBadRequestError("invalid embed visitor id")
}
ctx = types.WithEmbedVisitorID(ctx, visitorID)
}
c.Set(types.PrincipalContextKey.String(), principal)
c.Request = c.Request.WithContext(types.WithPrincipal(c.Request.Context(), principal))
c.Request = c.Request.WithContext(types.WithPrincipal(ctx, principal))
return nil
}