Skip to content

Teams SSO Consent Required suported#380

Merged
axelsrz merged 4 commits intomainfrom
users/axsuarez/teams-sso-consent-required
May 1, 2026
Merged

Teams SSO Consent Required suported#380
axelsrz merged 4 commits intomainfrom
users/axsuarez/teams-sso-consent-required

Conversation

@axelsrz
Copy link
Copy Markdown
Member

@axelsrz axelsrz commented Apr 29, 2026

This pull request improves the OAuth token exchange flow to handle user consent scenarios more robustly and to provide clearer error signaling throughout the authentication process. The key changes introduce a new error tag for consent-related precondition failures, propagate this state through the OAuth flow, and update the response handling to better align with Microsoft Teams' expectations for token exchange failures.

Fixes #294

OAuth Token Exchange and Error Handling Improvements:

  • Added a new _FlowErrorTag.PRECONDITION_FAILED to represent cases where user consent is required before token exchange can proceed.
  • Updated _continue_from_invoke_token_exchange in _oauth_flow.py to catch consent-related errors, returning a tuple of (None, _FlowErrorTag.PRECONDITION_FAILED) when a 400 error with "Consent Required" is encountered, instead of raising an exception.
  • Modified continue_flow to propagate the new error tag, handle signin/failure activities, and avoid incrementing attempt counters for consent precondition failures. [1] [2]

User Authorization and Response Handling:

  • Enhanced _handle_flow_response in _user_authorization.py to detect the PRECONDITION_FAILED state and respond with a 412 InvokeResponse, prompting Teams to request user consent and retry the token exchange.
  • Changed the default response for missing invoke responses in channel_service_adapter.py from HTTP 501 (Not Implemented) to HTTP 200 (OK), ensuring better compatibility with Teams' expectations.

Logging and Error Propagation:

  • Improved error logging and propagation in user_token_client.py by raising a detailed ClientResponseError with the response body when token exchange fails (status code >= 300), aiding in debugging and error transparency.
  • Added missing imports and updated imports for new types used in the enhanced error handling and response logic. [1] [2]

Copilot AI review requested due to automatic review settings April 29, 2026 23:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Improves the OAuth token exchange flow to better handle consent-required scenarios by signaling a precondition failure state through the flow and returning a Teams-friendly invoke response behavior.

Changes:

  • Introduces _FlowErrorTag.PRECONDITION_FAILED and propagates it through the OAuth continuation logic.
  • Returns a 412 invoke response for token-exchange consent-required failures to prompt Teams to fall back to regular login.
  • Enhances token-exchange error reporting and adjusts default invoke response behavior in the adapter.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/user_token_client.py Improves token-exchange error detail by including response body in raised errors/logs.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/channel_service_adapter.py Changes default invoke response when none is set (501 → 200).
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/_user_authorization.py Adds handling to return a 412 invoke response for consent-required token exchange failures.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/_oauth/_oauth_flow.py Propagates consent-required failures as a tagged flow error and handles signin/failure invokes.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/_oauth/_flow_state.py Adds the new flow error tag enum value.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@axelsrz axelsrz changed the title Precondition failed working, continue activity pending Teams SSO Consent Required suported May 1, 2026
@axelsrz axelsrz marked this pull request as ready for review May 1, 2026 22:19
@axelsrz axelsrz requested a review from a team as a code owner May 1, 2026 22:19
Copilot AI review requested due to automatic review settings May 1, 2026 22:19
@axelsrz axelsrz merged commit 418f4e0 into main May 1, 2026
11 checks passed
@axelsrz axelsrz deleted the users/axsuarez/teams-sso-consent-required branch May 1, 2026 22:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

async def _continue_from_invoke_token_exchange(
self, activity: Activity
) -> TokenResponse:
) -> tuple[TokenResponse, _FlowErrorTag]:
connection_name=flow_state.connection,
failure_detail="The Agent is unable to exchange token. Proceed with regular login.",
),
).model_dump(exclude_unset=True),
Comment on lines +266 to +271
if not (
context.activity.channel_id.channel == Channels.ms_teams
and sign_in_state.continuation_activity
and context.activity.type == ActivityTypes.invoke
and context.activity.name
== SignInConstants.token_exchange_operation_name
axelsrz added a commit that referenced this pull request May 4, 2026
* Precondition failed working, continue activity pending

* Adding special handling of continuation activity

* Adding tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

User Consent flow fails in SSO with BizChat

3 participants