-
Notifications
You must be signed in to change notification settings - Fork 9
Add auth_handler_name parameter to Authorization.exchange_token call #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/utility.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """ | ||
| Utility functions for Microsoft Agent 365 runtime operations. | ||
|
|
||
| This module provides utility functions for token handling, agent identity resolution, | ||
| and other common runtime operations. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import uuid | ||
| from typing import Any, Optional | ||
|
|
||
| import jwt | ||
|
|
||
|
|
||
| class Utility: | ||
| """ | ||
| Utility class providing common runtime operations for Agent 365. | ||
|
|
||
| This class contains static methods for token processing, agent identity resolution, | ||
| and other utility functions used across the Agent 365 runtime. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def get_app_id_from_token(token: Optional[str]) -> str: | ||
| """ | ||
| Decodes the current token and retrieves the App ID (appid or azp claim). | ||
|
|
||
| Args: | ||
| token: JWT token to decode. Can be None or empty. | ||
|
|
||
| Returns: | ||
| str: The App ID from the token's claims, or empty GUID if token is invalid. | ||
| Returns "00000000-0000-0000-0000-000000000000" if no valid App ID is found. | ||
| """ | ||
| if not token or not token.strip(): | ||
| return str(uuid.UUID(int=0)) | ||
|
|
||
| try: | ||
| # Decode the JWT token without verification (we only need the claims) | ||
| # Note: verify=False is used because we only need to extract claims, | ||
| # not verify the token's authenticity | ||
| decoded_payload = jwt.decode(token, options={"verify_signature": False}) | ||
|
|
||
| # Look for appid or azp claims (appid takes precedence) | ||
| app_id = decoded_payload.get("appid") or decoded_payload.get("azp") | ||
| return app_id if app_id else "" | ||
|
|
||
| except (jwt.DecodeError, jwt.InvalidTokenError): | ||
| # Token is malformed or invalid | ||
| return "" | ||
|
|
||
| @staticmethod | ||
| def resolve_agent_identity(context: Any, auth_token: Optional[str]) -> str: | ||
| """ | ||
| Resolves the agent identity from the turn context or auth token. | ||
|
|
||
| Args: | ||
| context: Turn context of the conversation turn. Expected to have an Activity | ||
| with methods like is_agentic_request() and get_agentic_instance_id(). | ||
| auth_token: Authentication token if available. | ||
|
|
||
| Returns: | ||
| str: The agent identity (App ID). Returns the agentic instance ID if the | ||
| request is agentic, otherwise returns the App ID from the auth token. | ||
| """ | ||
| try: | ||
| # App ID is required to pass to MCP server URL | ||
| # Try to get agentic instance ID if this is an agentic request | ||
| if context and context.activity and context.activity.is_agentic_request(): | ||
| agentic_id = context.activity.get_agentic_instance_id() | ||
| return agentic_id if agentic_id else "" | ||
|
|
||
| except (AttributeError, TypeError, Exception): | ||
pontemonti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Context/activity doesn't have the expected methods or properties | ||
| # or any other error occurred while accessing context/activity | ||
| pass | ||
|
|
||
| # Fallback to extracting App ID from the auth token | ||
| return Utility.get_app_id_from_token(auth_token) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.