24
24
URL = "https://toolbox-service-url"
25
25
async with ToolboxClient(
26
26
URL,
27
- client_headers={"Authorization": auth_methods.aget_google_id_token})
27
+ client_headers={"Authorization": auth_methods.aget_google_id_token})
28
28
as toolbox:
29
29
tools = await toolbox.load_toolset()
30
30
"""
31
31
32
32
import asyncio
33
33
from datetime import datetime , timedelta , timezone
34
- from typing import Any , Dict , Optional , Callable , Coroutine
34
+ from typing import Any , Callable , Coroutine , Dict , Optional
35
35
36
- import google .auth
37
36
from google .auth .exceptions import GoogleAuthError
38
37
from google .auth .transport .requests import AuthorizedSession , Request
39
38
from google .oauth2 import id_token
@@ -87,7 +86,8 @@ def _update_cache(new_token: str) -> None:
87
86
_token_cache ["token" ] = None
88
87
_token_cache ["expires_at" ] = datetime .min .replace (tzinfo = timezone .utc )
89
88
raise ValueError (f"Failed to validate and cache the new token: { e } " ) from e
90
-
89
+
90
+
91
91
def get_google_token_from_aud (audience : Optional [str ] = None ) -> str :
92
92
if _is_token_valid ():
93
93
return BEARER_TOKEN_PREFIX + _token_cache ["token" ]
@@ -140,13 +140,16 @@ def get_google_id_token(audience: Optional[str] = None) -> Callable[[], str]:
140
140
GoogleAuthError: If fetching credentials or the token fails.
141
141
ValueError: If the fetched token is invalid.
142
142
"""
143
+
143
144
def _token_getter () -> str :
144
145
return get_google_token_from_aud (audience )
145
146
146
147
return _token_getter
147
148
148
149
149
- def aget_google_id_token (audience : Optional [str ] = None ) -> Callable [[], Coroutine [Any , Any , str ]]:
150
+ def aget_google_id_token (
151
+ audience : Optional [str ] = None ,
152
+ ) -> Callable [[], Coroutine [Any , Any , str ]]:
150
153
"""
151
154
Returns an ASYNC function that, when called, fetches a Google ID token.
152
155
This function uses Application Default Credentials for local systems
@@ -163,7 +166,8 @@ def aget_google_id_token(audience: Optional[str] = None) -> Callable[[], Corouti
163
166
GoogleAuthError: If fetching credentials or the token fails.
164
167
ValueError: If the fetched token is invalid.
165
168
"""
169
+
166
170
async def _token_getter () -> str :
167
171
return await asyncio .to_thread (get_google_token_from_aud , audience )
168
-
172
+
169
173
return _token_getter
0 commit comments