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
+1
View File
@@ -71,6 +71,7 @@ func (m *MCPManager) GetOrCreateClient(ctx context.Context, service *types.MCPSe
if service.AuthConfig.IsOAuth() {
tenantID, _ = types.TenantIDFromContext(ctx)
principal, _ = types.PrincipalFromContext(ctx)
principal = types.MCPOAuthPrincipalFromContext(ctx)
if !principal.Valid() {
return nil, fmt.Errorf("principal context is required to connect to OAuth MCP service %s", service.Name)
}
+9
View File
@@ -3,6 +3,7 @@ package mcp
import (
"context"
"fmt"
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
@@ -15,6 +16,11 @@ import (
// registration (RFC 7591).
const clientRegistrationName = "WeKnora"
// oauthCallbackTimeout bounds token exchange after the browser lands on the
// public callback route. The Gin request context is canceled once the client
// receives the redirect, so CompleteAuthorization must detach from it.
const oauthCallbackTimeout = 60 * time.Second
// OAuthManager orchestrates the MCP OAuth2 authorization-code flow:
// discovery, dynamic client registration, the authorize redirect, and the
// callback code exchange. Tokens are persisted per (tenant, principal, service);
@@ -143,6 +149,9 @@ func (m *OAuthManager) StartAuthorization(
func (m *OAuthManager) CompleteAuthorization(
ctx context.Context, state, code string,
) (frontendRedirect string, err error) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), oauthCallbackTimeout)
defer cancel()
st, err := m.states.Take(ctx, state)
if err != nil {
return "", err