-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added ManagedIdentity * Missing ConfigurationServiceClientCredentialFactory awaits * ManagedIdentityAppCredentials needs ManagedIdentity dict * Added missing PermissionError descriptions * Black reformatting in botbuilder-core --------- Co-authored-by: Tracy Boehrer <[email protected]>
- Loading branch information
1 parent
d7cd937
commit 579888d
Showing
10 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
56 changes: 56 additions & 0 deletions
56
...ies/botframework-connector/botframework/connector/auth/managedidentity_app_credentials.py
This file contains 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,56 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from abc import ABC | ||
|
||
import msal | ||
import requests | ||
|
||
from .app_credentials import AppCredentials | ||
from .microsoft_app_credentials import MicrosoftAppCredentials | ||
|
||
|
||
class ManagedIdentityAppCredentials(AppCredentials, ABC): | ||
""" | ||
AppCredentials implementation using application ID and password. | ||
""" | ||
|
||
global_token_cache = msal.TokenCache() | ||
|
||
def __init__(self, app_id: str, oauth_scope: str = None): | ||
# super will set proper scope and endpoint. | ||
super().__init__( | ||
app_id=app_id, | ||
oauth_scope=oauth_scope, | ||
) | ||
|
||
self._managed_identity = {"ManagedIdentityIdType": "ClientId", "Id": app_id} | ||
|
||
self.app = None | ||
|
||
@staticmethod | ||
def empty(): | ||
return MicrosoftAppCredentials("", "") | ||
|
||
def get_access_token(self, force_refresh: bool = False) -> str: | ||
""" | ||
Implementation of AppCredentials.get_token. | ||
:return: The access token for the given app id and password. | ||
""" | ||
|
||
# Firstly, looks up a token from cache | ||
# Since we are looking for token for the current app, NOT for an end user, | ||
# notice we give account parameter as None. | ||
auth_token = self.__get_msal_app().acquire_token_for_client( | ||
resource=self.oauth_scope | ||
) | ||
return auth_token["access_token"] | ||
|
||
def __get_msal_app(self): | ||
if not self.app: | ||
self.app = msal.ManagedIdentityClient( | ||
self._managed_identity, | ||
http_client=requests.Session(), | ||
token_cache=ManagedIdentityAppCredentials.global_token_cache, | ||
) | ||
return self.app |
39 changes: 39 additions & 0 deletions
39
...onnector/botframework/connector/auth/managedidentity_service_client_credential_factory.py
This file contains 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,39 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from logging import Logger | ||
|
||
from msrest.authentication import Authentication | ||
|
||
from .managedidentity_app_credentials import ManagedIdentityAppCredentials | ||
from .microsoft_app_credentials import MicrosoftAppCredentials | ||
from .service_client_credentials_factory import ServiceClientCredentialsFactory | ||
|
||
|
||
class ManagedIdentityServiceClientCredentialsFactory(ServiceClientCredentialsFactory): | ||
def __init__(self, app_id: str = None, *, logger: Logger = None) -> None: | ||
self.app_id = app_id | ||
self._logger = logger | ||
|
||
async def is_valid_app_id(self, app_id: str) -> bool: | ||
return app_id == self.app_id | ||
|
||
async def is_authentication_disabled(self) -> bool: | ||
return not self.app_id | ||
|
||
async def create_credentials( | ||
self, | ||
app_id: str, | ||
oauth_scope: str, | ||
login_endpoint: str, | ||
validate_authority: bool, | ||
) -> Authentication: | ||
if await self.is_authentication_disabled(): | ||
return MicrosoftAppCredentials.empty() | ||
|
||
if not await self.is_valid_app_id(app_id): | ||
raise Exception("Invalid app_id") | ||
|
||
credentials = ManagedIdentityAppCredentials(app_id, oauth_scope) | ||
|
||
return credentials |
This file contains 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ botbuilder-schema==4.16.0 | |
requests==2.32.0 | ||
PyJWT==2.4.0 | ||
cryptography==42.0.4 | ||
msal==1.* | ||
msal>=1.29.0 |
This file contains 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