23
23
from functools import partial
24
24
25
25
URL = "https://toolbox-service-url"
26
- auth_token_provider = functools.partial(
27
- auth_methods.aget_google_id_token,
28
- URL
29
- )
30
26
async with ToolboxClient(
31
27
URL,
32
- client_headers={"Authorization": auth_token_provider},
33
- ) as toolbox:
28
+ client_headers={"Authorization": auth_methods.aget_google_id_token})
29
+ as toolbox:
34
30
tools = await toolbox.load_toolset()
35
31
"""
36
32
37
33
import asyncio
38
34
from datetime import datetime , timedelta , timezone
39
- from typing import Any , Dict , Optional
35
+ from typing import Any , Dict , Optional , Callable , Coroutine
40
36
41
37
import google .auth
42
38
from google .auth .exceptions import GoogleAuthError
@@ -92,25 +88,8 @@ def _update_cache(new_token: str) -> None:
92
88
_token_cache ["token" ] = None
93
89
_token_cache ["expires_at" ] = datetime .min .replace (tzinfo = timezone .utc )
94
90
raise ValueError (f"Failed to validate and cache the new token: { e } " ) from e
95
-
96
-
97
- def get_google_id_token (audience : Optional [str ] = None ) -> str :
98
- """
99
- Synchronously fetches a Google ID token for a specific audience.
100
- This function uses Application Default Credentials for local systems
101
- and standard google auth libraries for Google Cloud environments.
102
- It caches the token in memory.
103
-
104
- Args:
105
- audience: The audience for the ID token (e.g., a service URL or client ID).
106
-
107
- Returns:
108
- A string in the format "Bearer <google_id_token>".
109
-
110
- Raises:
111
- GoogleAuthError: If fetching credentials or the token fails.
112
- ValueError: If the fetched token is invalid.
113
- """
91
+
92
+ def get_google_token_from_aud (audience : Optional [str ] = None ) -> str :
114
93
if _is_token_valid ():
115
94
return BEARER_TOKEN_PREFIX + _token_cache ["token" ]
116
95
@@ -145,7 +124,30 @@ def get_google_id_token(audience: Optional[str] = None) -> str:
145
124
) from e
146
125
147
126
148
- async def aget_google_id_token (audience : Optional [str ] = None ) -> str :
127
+ def get_google_id_token (audience : Optional [str ] = None ) -> Callable [[], str ]:
128
+ """
129
+ Synchronously fetches a Google ID token for a specific audience.
130
+ This function uses Application Default Credentials for local systems
131
+ and standard google auth libraries for Google Cloud environments.
132
+ It caches the token in memory.
133
+
134
+ Args:
135
+ audience: The audience for the ID token (e.g., a service URL or client ID).
136
+
137
+ Returns:
138
+ A string in the format "Bearer <google_id_token>".
139
+
140
+ Raises:
141
+ GoogleAuthError: If fetching credentials or the token fails.
142
+ ValueError: If the fetched token is invalid.
143
+ """
144
+ def _token_getter () -> str :
145
+ return get_google_token_from_aud (audience )
146
+
147
+ return _token_getter
148
+
149
+
150
+ async def aget_google_id_token (audience : Optional [str ] = None ) -> Callable [[], Coroutine [Any , Any , str ]]:
149
151
"""
150
152
Asynchronously fetches a Google ID token for a specific audience.
151
153
This function uses Application Default Credentials for local systems
@@ -162,5 +164,7 @@ async def aget_google_id_token(audience: Optional[str] = None) -> str:
162
164
GoogleAuthError: If fetching credentials or the token fails.
163
165
ValueError: If the fetched token is invalid.
164
166
"""
165
- token = await asyncio .to_thread (get_google_id_token , audience )
166
- return token
167
+ async def _token_getter () -> str :
168
+ return await asyncio .to_thread (get_google_token_from_aud , audience )
169
+
170
+ return _token_getter
0 commit comments