2222
2323from .credentials import CredentialConfig , CredentialType
2424
25- # Global ContextVar for User Identity (3LO) tokens to be injected per-request
2625USER_TOKEN_CONTEXT_VAR : ContextVar [Optional [str ]] = ContextVar (
2726 "toolbox_user_token" , default = None
2827)
@@ -53,11 +52,6 @@ def __init__(
5352 self ._credentials = credentials
5453 self ._additional_headers = additional_headers or {}
5554
56- # Prepare auth_token_getters for toolbox-core
57- # toolbox_core expects: dict[str, Callable[[], str | Awaitable[str]]]
58- # However, for general headers (like Authorization), we can pass them in client_headers
59- # if they are static or simpler. Toolbox-core supports `client_headers` which can be dynamic.
60-
6155 self ._core_client_headers : Dict [
6256 str , Union [str , Callable [[], str ], Callable [[], Awaitable [str ]]]
6357 ] = {}
@@ -85,8 +79,6 @@ def _configure_auth(self, creds: CredentialConfig) -> None:
8579 )
8680
8781 # Create an async capable token getter
88- # We wrap it to match the signature expected by toolbox-core headers
89- # (which accepts callables)
9082 self ._core_client_headers ["Authorization" ] = self ._create_adc_token_getter (
9183 creds .target_audience
9284 )
@@ -108,14 +100,10 @@ def _configure_auth(self, creds: CredentialConfig) -> None:
108100
109101 elif creds .type == CredentialType .USER_IDENTITY :
110102 # For USER_IDENTITY (3LO), the *Tool* handles the interactive flow at runtime.
111- # We use a ContextVar to inject the token per-request.
112103
113104 def get_user_token () -> str :
114105 token = USER_TOKEN_CONTEXT_VAR .get ()
115106 if not token :
116- # If this is called but no token is set in context, it means
117- # the tool wrapper failed to set it or we are in a context where
118- # we expected it. We return empty string which might cause 401.
119107 return ""
120108 return f"Bearer { token } "
121109
@@ -125,28 +113,19 @@ def _create_adc_token_getter(self, audience: str) -> Callable[[], str]:
125113 """Returns a callable that fetches a fresh ID token using ADC."""
126114
127115 def get_token () -> str :
128- # Note: This is a synchronous call. Toolbox-core supports sync callables in headers.
129- # Ideally we would use async but google-auth is primarily sync for these helpers.
130116 request = transport .requests .Request ()
131- # Try to get ID token directly (e.g. on Cloud Run)
132117 try :
133118 token = id_token .fetch_id_token (request , audience )
134119 return f"Bearer { token } "
135120 except Exception :
136- # Fallback to default credentials (e.g. local gcloud)
121+ # Fallback to default credentials
137122 creds , _ = google .auth .default ()
138123 if not creds .valid :
139124 creds .refresh (request )
140- # If specific ID token credentials, use them, otherwise this might be Access Token (scoped)
141- # For Toolbox we usually need ID Tokens.
142- # If the user is locally auth'd via `gcloud auth login`, fetch_id_token is preferred.
143- # If falling back to service account file:
125+
144126 if hasattr (creds , "id_token" ) and creds .id_token :
145127 return f"Bearer { creds .id_token } "
146128
147- # If we are here, we might need to manually sign via IAM or similar if it's a generic SA.
148- # For simplicity in this v1, we assume fetch_id_token works or standard creds work.
149- # Re-attempt fetch_id_token on the credentials object if possible
150129 curr_token = getattr (creds , "token" , None )
151130 return f"Bearer { curr_token } " if curr_token else ""
152131
0 commit comments