Skip to content

Commit 79cc1f2

Browse files
GWealecopybara-github
authored andcommitted
feat: forward the OAuth2 nonce to the authorization request
Some OIDC providers require a nonce parameter on the authorization request, but generate_auth_uri dropped the OAuth2Auth.nonce field even when a caller set it. Pass nonce through to create_authorization_url when present, leaving non-OIDC flows that omit it unchanged. Close #2067 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 951116662
1 parent 8f98bcd commit 79cc1f2

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/google/adk/auth/auth_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ def generate_auth_uri(
215215
}
216216
if auth_credential.oauth2.audience:
217217
params["audience"] = auth_credential.oauth2.audience
218+
if auth_credential.oauth2.nonce:
219+
params["nonce"] = auth_credential.oauth2.nonce
218220

219221
# If using PKCE with S256, ensure a code_verifier exists.
220222
# If not provided in the credential, generate a cryptographically secure

tests/unittests/auth/test_auth_handler.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,59 @@ def test_generate_auth_uri_pkce(
355355
assert "code_verifier" in kwargs
356356
assert kwargs["code_verifier"] == result.oauth2.code_verifier
357357

358+
@patch("google.adk.auth.auth_handler.OAuth2Session")
359+
def test_generate_auth_uri_with_nonce(
360+
self, mock_oauth2_session, oauth2_auth_scheme, oauth2_credentials
361+
):
362+
"""Test that a nonce is forwarded to the authorization request."""
363+
oauth2_credentials.oauth2.nonce = "test_nonce"
364+
exchanged = oauth2_credentials.model_copy(deep=True)
365+
366+
config = AuthConfig(
367+
auth_scheme=oauth2_auth_scheme,
368+
raw_auth_credential=oauth2_credentials,
369+
exchanged_auth_credential=exchanged,
370+
)
371+
372+
mock_client = Mock()
373+
mock_oauth2_session.return_value = mock_client
374+
mock_client.create_authorization_url.return_value = (
375+
"https://example.com/oauth2/authorize?nonce=test_nonce",
376+
"mock_state",
377+
)
378+
379+
handler = AuthHandler(config)
380+
handler.generate_auth_uri()
381+
382+
_, kwargs = mock_client.create_authorization_url.call_args
383+
assert kwargs["nonce"] == "test_nonce"
384+
385+
@patch("google.adk.auth.auth_handler.OAuth2Session")
386+
def test_generate_auth_uri_without_nonce(
387+
self, mock_oauth2_session, oauth2_auth_scheme, oauth2_credentials
388+
):
389+
"""Test that no nonce is sent when the credential has none."""
390+
exchanged = oauth2_credentials.model_copy(deep=True)
391+
392+
config = AuthConfig(
393+
auth_scheme=oauth2_auth_scheme,
394+
raw_auth_credential=oauth2_credentials,
395+
exchanged_auth_credential=exchanged,
396+
)
397+
398+
mock_client = Mock()
399+
mock_oauth2_session.return_value = mock_client
400+
mock_client.create_authorization_url.return_value = (
401+
"https://example.com/oauth2/authorize",
402+
"mock_state",
403+
)
404+
405+
handler = AuthHandler(config)
406+
handler.generate_auth_uri()
407+
408+
_, kwargs = mock_client.create_authorization_url.call_args
409+
assert "nonce" not in kwargs
410+
358411
def test_generate_auth_uri_unsupported_pkce_method(
359412
self, oauth2_auth_scheme, oauth2_credentials
360413
):

0 commit comments

Comments
 (0)