-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy path_request_context.py
More file actions
63 lines (48 loc) · 2.05 KB
/
_request_context.py
File metadata and controls
63 lines (48 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
"""Request context management for MCP server HTTP/SSE modes.
This module provides ContextVars for storing per-request authentication values
extracted from HTTP headers. These values are scoped to a single request and
do not pollute the global environment.
"""
from __future__ import annotations
from contextvars import ContextVar
from airbyte.cloud.auth import (
resolve_cloud_client_id,
resolve_cloud_client_secret,
resolve_cloud_workspace_id,
)
from airbyte.secrets import SecretString
CLOUD_CLIENT_ID_CVAR: ContextVar[str | SecretString | None] = ContextVar(
"cloud_client_id", default=None
)
CLOUD_CLIENT_SECRET_CVAR: ContextVar[str | SecretString | None] = ContextVar(
"cloud_client_secret", default=None
)
CLOUD_WORKSPACE_ID_CVAR: ContextVar[str | None] = ContextVar("cloud_workspace_id", default=None)
def get_effective_cloud_client_id() -> SecretString:
"""Get the effective cloud client ID from request context or environment.
Returns:
Client ID from HTTP headers (if set), otherwise from environment variables
"""
header_value = CLOUD_CLIENT_ID_CVAR.get()
if header_value is not None:
return SecretString(header_value)
return resolve_cloud_client_id()
def get_effective_cloud_client_secret() -> SecretString:
"""Get the effective cloud client secret from request context or environment.
Returns:
Client secret from HTTP headers (if set), otherwise from environment variables
"""
header_value = CLOUD_CLIENT_SECRET_CVAR.get()
if header_value is not None:
return SecretString(header_value)
return resolve_cloud_client_secret()
def get_effective_cloud_workspace_id() -> str:
"""Get the effective cloud workspace ID from request context or environment.
Returns:
Workspace ID from HTTP headers (if set), otherwise from environment variables
"""
header_value = CLOUD_WORKSPACE_ID_CVAR.get()
if header_value is not None:
return str(header_value)
return resolve_cloud_workspace_id()