Skip to content

Commit 2f829a0

Browse files
committed
fix sync method
1 parent 8d06de1 commit 2f829a0

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

packages/toolbox-core/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ dependencies = [
1313
"pydantic>=2.7.0,<3.0.0",
1414
"aiohttp>=3.8.6,<4.0.0",
1515
"deprecated>=1.2.15,<2.0.0",
16+
"google-auth>=2.0.0,<3.0.0",
17+
"requests>=2.19.0,<3.0.0"
1618
]
1719

1820
classifiers = [
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
aiohttp==3.12.14
22
pydantic==2.11.7
3-
deprecated==1.2.18
3+
deprecated==1.2.18
4+
requests==2.32.4
5+
google-auth==2.40.3

packages/toolbox-core/src/toolbox_core/auth_methods.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@
2323
from functools import partial
2424
2525
URL = "https://toolbox-service-url"
26-
auth_token_provider = functools.partial(
27-
auth_methods.aget_google_id_token,
28-
URL
29-
)
3026
async with ToolboxClient(
3127
URL,
32-
client_headers={"Authorization": auth_token_provider},
33-
) as toolbox:
28+
client_headers={"Authorization": auth_methods.aget_google_id_token})
29+
as toolbox:
3430
tools = await toolbox.load_toolset()
3531
"""
3632

3733
import asyncio
3834
from datetime import datetime, timedelta, timezone
39-
from typing import Any, Dict, Optional
35+
from typing import Any, Dict, Optional, Callable, Coroutine
4036

4137
import google.auth
4238
from google.auth.exceptions import GoogleAuthError
@@ -92,25 +88,8 @@ def _update_cache(new_token: str) -> None:
9288
_token_cache["token"] = None
9389
_token_cache["expires_at"] = datetime.min.replace(tzinfo=timezone.utc)
9490
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:
11493
if _is_token_valid():
11594
return BEARER_TOKEN_PREFIX + _token_cache["token"]
11695

@@ -145,7 +124,30 @@ def get_google_id_token(audience: Optional[str] = None) -> str:
145124
) from e
146125

147126

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]]:
149151
"""
150152
Asynchronously fetches a Google ID token for a specific audience.
151153
This function uses Application Default Credentials for local systems
@@ -162,5 +164,7 @@ async def aget_google_id_token(audience: Optional[str] = None) -> str:
162164
GoogleAuthError: If fetching credentials or the token fails.
163165
ValueError: If the fetched token is invalid.
164166
"""
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

Comments
 (0)