Skip to content

Commit 308764a

Browse files
committed
feat: Add toolbox auth methods module
1 parent 874f2f1 commit 308764a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# The tokens obtained by these functions are formatted as "Bearer" tokens
16+
# and are intended to be passed in the "Authorization" header of HTTP requests.
17+
#
18+
# Example User Experience:
19+
# from toolbox_core import toolbox_auth_methods
20+
#
21+
# auth_token_provider = toolbox_auth_methods.aget_google_id_token
22+
# toolbox = ToolboxClient(
23+
# URL,
24+
# client_headers={"Authorization": auth_token_provider},
25+
# )
26+
# tools = await toolbox.load_toolset()
27+
28+
129
from functools import partial
230

331
import google.auth
@@ -8,6 +36,16 @@
836

937

1038
async def aget_google_id_token():
39+
"""
40+
Asynchronously fetches a Google ID token.
41+
42+
The token is formatted as a 'Bearer' token string and is suitable for use
43+
in an HTTP Authorization header. This function uses Application Default
44+
Credentials.
45+
46+
Returns:
47+
A string in the format "Bearer <google_id_token>".
48+
"""
1149
creds, _ = default_async()
1250
await creds.refresh(_aiohttp_requests.Request())
1351
creds.before_request = partial(Credentials.before_request, creds)
@@ -16,6 +54,16 @@ async def aget_google_id_token():
1654

1755

1856
def get_google_id_token():
57+
"""
58+
Synchronously fetches a Google ID token.
59+
60+
The token is formatted as a 'Bearer' token string and is suitable for use
61+
in an HTTP Authorization header. This function uses Application Default
62+
Credentials.
63+
64+
Returns:
65+
A string in the format "Bearer <google_id_token>".
66+
"""
1967
credentials, _ = google.auth.default()
2068
session = AuthorizedSession(credentials)
2169
request = Request(session)

0 commit comments

Comments
 (0)