Skip to content

Connections and AccessTokenProviderBase interface improvements - #484

Closed
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/get-token-provider
Closed

Connections and AccessTokenProviderBase interface improvements#484
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/get-token-provider

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

This pull request introduces several improvements and refactors to the authentication and authorization core, focusing on standardizing access to configuration, enhancing type safety, and improving the interface for retrieving access token providers. The most significant change is the addition of a unified configuration property to all access token providers, enabling consistent and type-safe access to provider configuration. Additionally, the codebase now supports retrieving a token provider directly from an activity, and several type annotations and interface contracts have been clarified.

Interface and API Improvements

  • Added an abstract configuration property to AccessTokenProviderBase, requiring all access token providers to expose their configuration in a consistent way.
  • Implemented the configuration property in MsalAuth and AnonymousTokenProvider, returning the provider's configuration (or a default/empty configuration for anonymous providers). [1] [2]
  • Updated the Connections protocol to require a get_token_provider_from_activity method, allowing retrieval of a token provider based on both ClaimsIdentity and Activity.

Type and Annotation Enhancements

  • Changed return types for several methods in AccessTokenProviderBase from Optional[str] to str | None for improved clarity and Python 3.10+ compatibility. [1] [2]
  • Improved type annotations and docstrings for connection retrieval methods, making expected parameters and return values clearer. [1] [2]

Refactoring and Consistency

  • Refactored _get_agentic_token in rest_channel_service_client_factory.py to use the new configuration property directly, removing legacy and provider-specific code paths.
  • Added get_token_provider_from_activity method to connection_manager.py to select the appropriate token provider based on the recipient role and configuration, supporting alternate blueprint IDs.

These changes collectively improve maintainability, type safety, and the extensibility of the authentication core.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the authentication/authorization core to standardize how token providers expose their configuration and to improve token-provider selection APIs. It introduces a unified configuration surface across providers and expands the Connections contract to allow selecting a provider directly from an Activity.

Changes:

  • Introduces AccessTokenProviderBase.configuration and implements it in MSAL and anonymous providers to enable provider-agnostic configuration access.
  • Adds Connections.get_token_provider_from_activity(...) and implements it in ConnectionManager to support activity-based provider selection (including agentic flows).
  • Simplifies agentic token retrieval in RestChannelServiceClientFactory to use connection.configuration directly.

Reviewed changes

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

Show a summary per file
File Description
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/rest_channel_service_client_factory.py Refactors agentic token retrieval to use the unified connection.configuration property.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/connections.py Extends the Connections protocol with get_token_provider_from_activity(...).
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/connection_manager.py Implements activity-based token provider selection and alternate blueprint routing.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/anonymous_token_provider.py Adds the configuration property for the anonymous provider.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/access_token_provider_base.py Converts the base contract to an ABC and adds the abstract configuration property plus updated return type annotations.
libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py Implements the configuration property for MSAL-backed providers.

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

Comment on lines +58 to +59
raise NotImplementedError(
)
Comment on lines +11 to +15
@property
@abstractmethod
def configuration(self) -> AgentAuthConfiguration:
"""
The configuration for the access token provider.
Comment on lines +206 to +213
:param claims_identity: The claims identity of the bot.
:type claims_identity: :class:`microsoft_agents.hosting.core.ClaimsIdentity`
:param activity: The activity of the bot.
:type activity: dict
:return: The OAuth token provider for the agent.
:rtype: :class:`microsoft_agents.hosting.core.AccessTokenProviderBase`
:raises ValueError: If no connection is found for the given audience and service URL.
"""
Comment on lines +214 to +230
connection: AccessTokenProviderBase | None = None
try:
connection = self.get_token_provider(claims_identity, activity.service_url)
finally:
if (connection is not None and (
activity.recipient.role == RoleTypes.agentic_identity or
activity.recipient.role == RoleTypes.agentic_user
)):
if connection.configuration.ALT_BLUEPRINT_ID:
connection = self.get_connection(connection.configuration.ALT_BLUEPRINT_ID)

if connection:
return connection

raise RuntimeError(
"The connection returned by get_token_provider is not compatible with the activity's recipient role."
)
Comment on lines +198 to +203
def get_token_provider_from_activity(
self,
claims_identity: ClaimsIdentity,
activity: Activity
) -> AccessTokenProviderBase:
"""
@rodrigobr-msft
Rodrigo Brandão (rodrigobr-msft) deleted the users/robrandao/get-token-provider branch July 31, 2026 22:09
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.

Add get_token_provider_from_activity to Connections interface

2 participants