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:
@@ -57,6 +57,21 @@ func (r *sessionRepository) Get(ctx context.Context, tenantID uint64, userID str
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
// GetByID retrieves a session by tenant and id without user scoping.
|
||||
func (r *sessionRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.Session, error) {
|
||||
var session types.Session
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
First(&session).Error
|
||||
if err != nil {
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, apperrors.ErrSessionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
// GetByTenantID retrieves all sessions for a tenant
|
||||
func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint64, userID string) ([]*types.Session, error) {
|
||||
var sessions []*types.Session
|
||||
@@ -263,6 +278,18 @@ func (r *sessionRepository) Update(ctx context.Context, session *types.Session,
|
||||
return res.RowsAffected, res.Error
|
||||
}
|
||||
|
||||
// SetOwnerID assigns sessions.user_id for a tenant-scoped row.
|
||||
func (r *sessionRepository) SetOwnerID(ctx context.Context, tenantID uint64, id, ownerID string) (int64, error) {
|
||||
res := r.db.WithContext(ctx).
|
||||
Model(&types.Session{}).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
Updates(map[string]interface{}{
|
||||
"user_id": ownerID,
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
return res.RowsAffected, res.Error
|
||||
}
|
||||
|
||||
// UpdateLastRequestState writes only the agent_config column (used here to
|
||||
// store SessionLastRequestState) and bumps updated_at. We deliberately bypass
|
||||
// the regular Update path so the call doesn't perturb title/description and
|
||||
|
||||
@@ -69,8 +69,7 @@ func sessionUserIDForLookup(ctx context.Context) string {
|
||||
// Shared-agent pipelines resolve the session owner tenant first; keep that internal lookup tenant-scoped.
|
||||
return ""
|
||||
}
|
||||
userID, _ := types.UserIDFromContext(ctx)
|
||||
return userID
|
||||
return types.SessionOwnerIDFromContext(ctx)
|
||||
}
|
||||
|
||||
// CreateMessage creates a new message within an existing session
|
||||
|
||||
@@ -19,8 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func sessionUserIDFromContext(ctx context.Context) string {
|
||||
userID, _ := types.UserIDFromContext(ctx)
|
||||
return userID
|
||||
return types.SessionOwnerIDFromContext(ctx)
|
||||
}
|
||||
|
||||
// generateEventID generates a unique event ID with type suffix for better traceability
|
||||
@@ -134,6 +133,32 @@ func (s *sessionService) GetSession(ctx context.Context, id string) (*types.Sess
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// GetSessionByID loads a session by tenant and id without user scoping.
|
||||
func (s *sessionService) GetSessionByID(ctx context.Context, tenantID uint64, id string) (*types.Session, error) {
|
||||
if id == "" {
|
||||
return nil, stderrors.New("session id is required")
|
||||
}
|
||||
if tenantID == 0 {
|
||||
return nil, stderrors.New("tenant id is required")
|
||||
}
|
||||
return s.sessionRepo.GetByID(ctx, tenantID, id)
|
||||
}
|
||||
|
||||
// SetSessionOwnerID assigns sessions.user_id for the given session row.
|
||||
func (s *sessionService) SetSessionOwnerID(ctx context.Context, tenantID uint64, sessionID, ownerID string) error {
|
||||
if sessionID == "" || ownerID == "" || tenantID == 0 {
|
||||
return stderrors.New("tenant id, session id and owner id are required")
|
||||
}
|
||||
affected, err := s.sessionRepo.SetOwnerID(ctx, tenantID, sessionID, ownerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return apperrors.ErrSessionNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSessionsByTenant retrieves all sessions for the current tenant
|
||||
func (s *sessionService) GetSessionsByTenant(ctx context.Context) ([]*types.Session, error) {
|
||||
// Get tenant ID from context
|
||||
@@ -186,7 +211,7 @@ func (s *sessionService) ListSessions(
|
||||
query = &types.SessionListQuery{}
|
||||
}
|
||||
query.TenantID = types.MustTenantIDFromContext(ctx)
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok {
|
||||
if uid := types.SessionOwnerIDFromContext(ctx); uid != "" {
|
||||
query.UserID = uid
|
||||
}
|
||||
|
||||
|
||||
@@ -99,8 +99,8 @@ func (s *sessionService) KnowledgeQA(
|
||||
len(searchTargets),
|
||||
)
|
||||
|
||||
// Get UserID from context
|
||||
userID, _ := types.UserIDFromContext(ctx)
|
||||
// Scope memory and pipeline attribution to the same owner as the session.
|
||||
userID := types.SessionOwnerIDFromContext(ctx)
|
||||
|
||||
chatManage := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{
|
||||
@@ -734,7 +734,7 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
|
||||
}
|
||||
|
||||
// Create default retrieval parameters — prefer tenant RetrievalConfig, fallback to built-in defaults
|
||||
userID, _ := types.UserIDFromContext(ctx)
|
||||
userID := types.SessionOwnerIDFromContext(ctx)
|
||||
|
||||
// Load tenant-level retrieval config (nil is safe — GetEffective* methods handle nil receiver)
|
||||
var rc *types.RetrievalConfig
|
||||
|
||||
@@ -20,6 +20,15 @@ func testSessionScopeContext(tenantID uint64, userID string) context.Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
func testAPISessionScopeContext(tenantID uint64, externalUserID string) context.Context {
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, tenantID)
|
||||
ctx = context.WithValue(ctx, types.UserIDContextKey, "system-7")
|
||||
return types.WithPrincipal(ctx, types.Principal{
|
||||
Type: types.PrincipalAPIExternalUser,
|
||||
ID: externalUserID,
|
||||
})
|
||||
}
|
||||
|
||||
func newTestSessionService(t *testing.T) (*sessionService, *gorm.DB) {
|
||||
t.Helper()
|
||||
|
||||
@@ -95,3 +104,39 @@ func TestUpdateSessionIsScopedToCurrentUserAndAllowsNoOp(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetSessionIsScopedToAPIExternalUser(t *testing.T) {
|
||||
svc, db := newTestSessionService(t)
|
||||
aliceSession := &types.Session{
|
||||
TenantID: 1,
|
||||
UserID: "api_external_user:7:alice",
|
||||
Title: "alice api session",
|
||||
}
|
||||
require.NoError(t, db.Create(aliceSession).Error)
|
||||
bobSession := &types.Session{
|
||||
TenantID: 1,
|
||||
UserID: "api_external_user:7:bob",
|
||||
Title: "bob api session",
|
||||
}
|
||||
require.NoError(t, db.Create(bobSession).Error)
|
||||
tenantSession := &types.Session{
|
||||
TenantID: 1,
|
||||
UserID: "system-7",
|
||||
Title: "tenant api session",
|
||||
}
|
||||
require.NoError(t, db.Create(tenantSession).Error)
|
||||
|
||||
_, err := svc.GetSession(testAPISessionScopeContext(1, "7:alice"), bobSession.ID)
|
||||
require.ErrorIs(t, err, apperrors.ErrSessionNotFound)
|
||||
|
||||
got, err := svc.GetSession(testAPISessionScopeContext(1, "7:alice"), aliceSession.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, aliceSession.ID, got.ID)
|
||||
|
||||
_, err = svc.GetSession(testAPISessionScopeContext(1, "7:alice"), tenantSession.ID)
|
||||
require.ErrorIs(t, err, apperrors.ErrSessionNotFound)
|
||||
|
||||
got, err = svc.GetSession(testSessionScopeContext(1, "system-7"), tenantSession.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tenantSession.ID, got.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user