Repository navigation
Use secret value for client_secret in OAuth client - #1198
Conversation
secret field in OAuth2AuthCodeFlowProviderConfig type was changed to SerializableSecretStr. In case we don't update authentication handler it failed with error
```
2025-11-21 13:18:01 - ERROR - talk_to_data.check_user_permissions_function:194 - Authentication error: Authentication callback failed: Authentication failed: cannot convert 'SecretStr' object to bytes
Traceback (most recent call last):
File "/Users/dzmitryv/work/nemo-agent-toolkit/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider.py", line 118, in authenticate
authenticated_context = await auth_callback(self.config, AuthFlowType.OAUTH2_AUTHORIZATION_CODE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dzmitryv/work/nemo-agent-toolkit/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py", line 61, in authenticate
return await self._handle_oauth2_auth_code_flow(config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dzmitryv/work/nemo-agent-toolkit/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py", line 132, in _handle_oauth2_auth_code_flow
token = await asyncio.wait_for(flow_state.future, timeout=300)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
return await fut
^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/futures.py", line 289, in __await__
yield self # This tells Task to wait for completion.
^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/tasks.py", line 385, in __wakeup
future.result()
File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/futures.py", line 202, in result
raise self._exception.with_traceback(self._exception_tb)
RuntimeError: Authentication failed: cannot convert 'SecretStr' object to bytes
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/dzmitryv/work/nemo-agent-toolkit/examples/dmo-talk-to-data/src/talk_to_data/check_user_permissions_function.py", line 182, in _inner
auth_result = await auth_provider.authenticate()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dzmitryv/work/nemo-agent-toolkit/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider.py", line 120, in authenticate
raise RuntimeError(f"Authentication callback failed: {e}") from e
RuntimeError: Authentication callback failed: Authentication failed: cannot convert 'SecretStr' object to bytes
{"timestamp": "2025-11-21T13:18:01.674832", "level": "ERROR", "logger": "ta lk_to_data.check_user_permissions_function", "message": "Authentication err or: Authentication callback failed: Authentication failed: cannot convert ' SecretStr' object to bytes", "module": "check_user_permissions_function", " function": "_inner", "line": 194, "thread": 8438401024, "thread_name": "Mai nThread", "conversation_id": "b3265b5d-8a2e-4e9e-800c-717a11c16fab", "user_ message_id": "54a0ebea-d237-4660-ba37-288827fae1b0", "active_function": "us er_authentication", "function_id": "ce84d305-ebd8-4b54-89fc-574f71935009", "exception": "Traceback (most recent call last):\n File \"/Users/dzmitryv/ work/nemo-agent-toolkit/src/nat/authentication/oauth2/oauth2_auth_code_flow _provider.py\", line 118, in authenticate\n authenticated_context = awai t auth_callback(self.config, AuthFlowType.OAUTH2_AUTHORIZATION_CODE)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^\n File \"/Users/dzmitryv/work/nemo-agent-toolkit/src/ nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py\", line 61, in authenticate\n return await self._handle_oauth2_auth_code_flow(c onfig)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/dzmitryv/work/nemo-agent-toolkit/src/nat/front_ends/fastapi/auth_ flow_handlers/websocket_flow_handler.py\", line 132, in _handle_oauth2_auth _code_flow\n token = await asyncio.wait_for(flow_state.future, timeout=3 00)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/ Versions/3.12/lib/python3.12/asyncio/tasks.py\", line 520, in wait_for\n return await fut\n ^^^^^^^^^\n File \"/opt/homebrew/Cellar/pyth [email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asy ncio/futures.py\", line 289, in __await__\n yield self # This tells Tas k to wait for completion.\n ^^^^^^^^^^\n File \"/opt/homebrew/Cellar/py [email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/a syncio/tasks.py\", line 385, in __wakeup\n future.result()\n File \"/op t/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3 .12/lib/python3.12/asyncio/futures.py\", line 202, in result\n raise sel f._exception.with_traceback(self._exception_tb)\nRuntimeError: Authenticati on failed: cannot convert 'SecretStr' object to bytes\n\nThe above exceptio n was the direct cause of the following exception:\n\nTraceback (most recen t call last):\n File \"/Users/dzmitryv/work/nemo-agent-toolkit/examples/dm o-talk-to-data/src/talk_to_data/check_user_permissions_function.py\", line 182, in _inner\n auth_result = await auth_provider.authenticate()\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/dzmitryv/w ork/nemo-agent-toolkit/src/nat/authentication/oauth2/oauth2_auth_code_flow_ flow_handlers/websocket_flow_handler.py\", line 132, in _handle_oauth2_auth _code_flow\n token = await asyncio.wait_for(flow_state.future, timeout=3 00)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/ Versions/3.12/lib/python3.12/asyncio/tasks.py\", line 520, in wait_for\n return await fut\n ^^^^^^^^^\n File \"/opt/homebrew/Cellar/pyth [email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asy ncio/futures.py\", line 289, in __await__\n yield self # This tells Tas k to wait for completion.\n ^^^^^^^^^^\n File \"/opt/homebrew/Cellar/py [email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/tasks.py\", line 385, in __wakeup\n future.result()\n File \"/op t/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3 .12/lib/python3.12/asyncio/futures.py\", line 202, in result\n raise sel f._exception.with_traceback(self._exception_tb)\nRuntimeError: Authenticati on failed: cannot convert 'SecretStr' object to bytes\n\nThe above exceptio n was the direct cause of the following exception:\n\nTraceback (most recen t call last):\n File \"/Users/dzmitryv/work/nemo-agent-toolkit/examples/dm o-talk-to-data/src/talk_to_data/check_user_permissions_function.py\", line 182, in _inner\n auth_result = await auth_provider.authenticate()\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/dzmitryv/w ork/nemo-agent-toolkit/src/nat/authentication/oauth2/oauth2_auth_code_flow_ provider.py\", line 120, in authenticate\n raise RuntimeError(f\"Authent
(.venv) dzmitryv@dzmitryv-mlt nemo-agent-toolkit % cd /Users/dzmitryv/work/nemo-agent-toolkit ; /usr/bin/env /Users/dzm
itryv/work/nemo-agent-toolkit/.venv/bin/python /Users/dzmitryv/.cursor/extensions/ms-python.debugpy-2025.14.1-darwin-arm
64/bundled/libs/debugpy/adapter/../../debugpy/launcher 59201 -- /Users/dzmitryv/work/nemo-agent-toolkit/.venv/bin/nat se
rve --config_file=/Users/dzmitryv/work/nemo-agent-toolkit/examples/dmo-talk-to-data/configs/config.yml --port 8000
2025-11-21 13:23:50 - INFO - nat.cli.commands.start:192 - Starting NAT from config file: '/Users/dzmitryv/work/nemo-agent-toolkit/examples/dmo-talk-to-data/configs/config.yml'
2025-11-21 13:23:50 - WARNING - nat.front_ends.fastapi.fastapi_front_end_plugin:137 - Dask is not installed, async execution and evaluation will not be available.```
Signed-off-by: dzmitryv111111 <[email protected]>
WalkthroughModified OAuth2 client creation in the WebSocket flow handler to extract the secret value via Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5–10 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (2)**/*⚙️ CodeRabbit configuration file
Files:
src/nat/**/*⚙️ CodeRabbit configuration file
Files:
🧬 Code graph analysis (1)src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py (1)
🔇 Additional comments (2)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
/ok to test 709378e |
|
/merge |
Closes ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/NeMo-Agent-Toolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. ## Summary by CodeRabbit * **Bug Fixes** * Fixed OAuth2 client credential handling to properly extract and supply secret values during authentication flow. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> Authors: - Dzmitry Viarzhbitski (https://github.com/dzmitryv111111) Approvers: - David Gardner (https://github.com/dagardner-nv) URL: NVIDIA#1198 Signed-off-by: Sangharsh Aglave <[email protected]>
Closes ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/NeMo-Agent-Toolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. ## Summary by CodeRabbit * **Bug Fixes** * Fixed OAuth2 client credential handling to properly extract and supply secret values during authentication flow. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> Authors: - Dzmitry Viarzhbitski (https://github.com/dzmitryv111111) Approvers: - David Gardner (https://github.com/dagardner-nv) URL: NVIDIA#1198
`OAuth2AuthCodeFlowProvider._attempt_token_refresh` passed `config.client_secret` to Authlib's `OAuth2Client` as the `SecretStr` itself. Authlib builds the client credentials from `str(client_secret)`, which is `**********`, so a refresh for a client with a secret authenticated with the wrong secret. When the token endpoint rejected it, the refresh returned `None` and `authenticate()` fell back to the full authorization code flow on every token expiry. The secret is now passed with `get_secret_value()`, the same way the console, HTTP and websocket flow handlers already do. This is the refresh-path counterpart of #1198. Clients with an empty secret are unaffected before and after: an empty `SecretStr` is falsy, so Authlib uses `none` auth either way. Closes #2625 ## Testing `test_refresh_passes_plain_client_secret` in `test_oauth_exchanger.py` records the keyword arguments the provider gives `OAuth2Client` during a refresh. On develop it fails with `AssertionError: assert SecretStr('**********') == 'secret'`; with this change it passes. The existing refresh tests use a stub that ignores the constructor arguments, which is why they did not catch it. The rest of `test_oauth_exchanger.py` passes, and ruff and yapf are clean on the changed files. If you would rather add the secret to the refresh request some other way, I am happy to adjust this however you prefer. ## Summary by CodeRabbit * **Bug Fixes** * Fixed OAuth token refresh so authentication can use the configured client secret when renewing an expired token. * Fixed bearer-token verification to use the configured client secret correctly, while preserving behavior when no secret is configured. * Added test coverage for both authentication flows, including refresh-path behavior and handling of client secrets. Authors: - https://github.com/dfedoryshchev Approvers: - Atishay Kasliwal (https://github.com/atishay-kasliwal) - Will Killian (https://github.com/willkill07) URL: #2626
secret field in OAuth2AuthCodeFlowProviderConfig type was changed to SerializableSecretStr. In case we don't update authentication handler it failed with error