Skip to content

Commit 2353353

Browse files
committed
feat(auth): Add native support for id_token in OAuth2 credentials
**Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.** ### Link to Issue or Description of Change **2. Or, if no issue exists, describe the change:** ## Problem When performing authentication flows via `OAUTH2` or `OPEN_ID_CONNECT`, the native `OAuth2Token` response from identity providers (like Google OAuth) often includes an `id_token` alongside the `access_token` and `refresh_token`. However, the ADK's `update_credential_with_tokens` utility explicitly drops the `id_token`, preventing agents and tools from verifying user identity or extracting OIDC claims securely. Furthermore, the `OAuth2Auth` model does not have a designated field for `id_token`. ## Solution 1. Added an `id_token: Optional[str] = None` field to the `OAuth2Auth` pydantic model in `auth_credential.py`. 2. Updated `update_credential_with_tokens` in `oauth2_credential_util.py` to correctly extract and map `tokens.get("id_token")` into the `OAuth2Auth` credential object. 3. Updated the relevant unit tests to ensure `id_token` is asserted and preserved during credential updates. ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [x] All unit tests pass locally. Summary of passed `pytest` results: ```bash $ pytest tests/unittests/auth/test_oauth2_credential_util.py ======================= test session starts ======================= platform darwin -- Python 3.11.9, pytest-9.0.1, pluggy-1.6.0 collected 9 items tests/unittests/auth/test_oauth2_credential_util.py ......... [100%] ======================== 9 passed in 0.05s ========================
1 parent a2e43aa commit 2353353

3 files changed

Lines changed: 4 additions & 0 deletions

File tree

src/google/adk/auth/auth_credential.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class OAuth2Auth(BaseModelWithConfig):
7979
auth_code: Optional[str] = None
8080
access_token: Optional[str] = None
8181
refresh_token: Optional[str] = None
82+
id_token: Optional[str] = None
8283
expires_at: Optional[int] = None
8384
expires_in: Optional[int] = None
8485
audience: Optional[str] = None

src/google/adk/auth/oauth2_credential_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def update_credential_with_tokens(
109109
"""
110110
auth_credential.oauth2.access_token = tokens.get("access_token")
111111
auth_credential.oauth2.refresh_token = tokens.get("refresh_token")
112+
auth_credential.oauth2.id_token = tokens.get("id_token")
112113
auth_credential.oauth2.expires_at = (
113114
int(tokens.get("expires_at")) if tokens.get("expires_at") else None
114115
)

tests/unittests/auth/test_oauth2_credential_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def test_update_credential_with_tokens(self):
222222
tokens = OAuth2Token({
223223
"access_token": "new_access_token",
224224
"refresh_token": "new_refresh_token",
225+
"id_token": "new_id_token",
225226
"expires_at": expected_expires_at,
226227
"expires_in": 3600,
227228
})
@@ -230,5 +231,6 @@ def test_update_credential_with_tokens(self):
230231

231232
assert credential.oauth2.access_token == "new_access_token"
232233
assert credential.oauth2.refresh_token == "new_refresh_token"
234+
assert credential.oauth2.id_token == "new_id_token"
233235
assert credential.oauth2.expires_at == expected_expires_at
234236
assert credential.oauth2.expires_in == 3600

0 commit comments

Comments
 (0)