-
Notifications
You must be signed in to change notification settings - Fork 88
Authorization AsyncTokenCredential-returning variants #568
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
Open
Rodrigo Brandão (rodrigobr-msft)
wants to merge
8
commits into
main
Choose a base branch
from
users/robrandao/authorization-cred
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5db1f59
Authorization TokenCredential-returning variants
rodrigobr-msft ac9cbcf
Small improvements
rodrigobr-msft 2be89b2
Test improvements and changelog
rodrigobr-msft fcf562e
Fixing broken test
rodrigobr-msft 61d3515
Potential fix for pull request finding
rodrigobr-msft 6bfd41a
Potential fix for pull request finding
rodrigobr-msft afc0c61
Another commit
rodrigobr-msft a0d925e
removing unused imports
rodrigobr-msft 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
Some comments aren't visible on the classic Files Changed page.
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
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
66 changes: 66 additions & 0 deletions
66
...th-sidecar/microsoft_agents/authentication/entra_auth_sidecar/sidecar_token_credential.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,66 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import logging | ||
|
|
||
| from azure.core.credentials import AccessToken | ||
| from azure.core.credentials_async import AsyncTokenCredential | ||
|
|
||
| from microsoft_agents.hosting.core import AgentAuthConfiguration | ||
|
|
||
| from .sidecar_auth import SidecarAuth | ||
| from ._token_expiry import SidecarTokenExpiry | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _get_resource(scope: str) -> str: | ||
| """Extracts the resource by removing a trailing '/.default' from the scope. | ||
|
|
||
| :param scope: The scope string. | ||
| :return: The extracted resource string. | ||
| :rtype: str | ||
| """ | ||
| return scope.removesuffix("/.default") | ||
|
|
||
|
|
||
| class SidecarTokenCredential(AsyncTokenCredential): | ||
| """Provides an asynchronous Azure Core token credential using the Sidecar.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: AgentAuthConfiguration, | ||
| *, | ||
| provider: SidecarAuth | None = None, | ||
| ): | ||
| """Initializes the SidecarTokenCredential with the given configuration. | ||
|
|
||
| :param config: The agent authentication configuration. | ||
| :type config: :class:`microsoft_agents.hosting.core.AgentAuthConfiguration` | ||
| """ | ||
| self._config = config | ||
| self._provider: SidecarAuth | None = provider | ||
|
|
||
| async def get_token(self, *scopes: str, **kwargs) -> AccessToken: | ||
| """Acquire an access token for the specified scopes. | ||
|
|
||
| :param scopes: The scopes for which the access token is requested. | ||
| :param kwargs: Additional keyword arguments. | ||
|
|
||
| :return: The acquired access token. | ||
| :rtype: AccessToken | ||
| """ | ||
|
|
||
| logger.debug("get_token scope=%s", scopes) | ||
|
|
||
| if not scopes: | ||
| raise ValueError("At least one scope must be provided.") | ||
|
|
||
| if not self._provider: | ||
| self._provider = SidecarAuth(self._config) | ||
|
|
||
| resource = _get_resource(scopes[0]) | ||
|
|
||
| token = await self._provider.get_access_token(resource, list(scopes)) | ||
|
|
||
| return AccessToken(token=token, expires_on=SidecarTokenExpiry.resolve(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
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.