microsoft-agents-authentication-msal 0.1.1__tar.gz → 0.2.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (14) hide show
  1. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/PKG-INFO +2 -2
  2. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft/agents/authentication/msal/msal_auth.py +11 -2
  3. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft/agents/authentication/msal/msal_connection_manager.py +1 -1
  4. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft_agents_authentication_msal.egg-info/PKG-INFO +2 -2
  5. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft_agents_authentication_msal.egg-info/SOURCES.txt +3 -1
  6. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft_agents_authentication_msal.egg-info/requires.txt +1 -1
  7. microsoft_agents_authentication_msal-0.2.0.dev1/tests/test_msal_auth.py +83 -0
  8. microsoft_agents_authentication_msal-0.2.0.dev1/tests/test_msal_connection_manager.py +35 -0
  9. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft/agents/authentication/msal/__init__.py +0 -0
  10. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft_agents_authentication_msal.egg-info/dependency_links.txt +0 -0
  11. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/microsoft_agents_authentication_msal.egg-info/top_level.txt +0 -0
  12. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/pyproject.toml +0 -0
  13. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/setup.cfg +0 -0
  14. {microsoft_agents_authentication_msal-0.1.1 → microsoft_agents_authentication_msal-0.2.0.dev1}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: microsoft-agents-authentication-msal
3
- Version: 0.1.1
3
+ Version: 0.2.0.dev1
4
4
  Summary: A msal-based authentication library for Microsoft Agents
5
5
  Author: Microsoft Corporation
6
6
  Project-URL: Homepage, https://github.com/microsoft/Agents
@@ -8,7 +8,7 @@ Classifier: Programming Language :: Python :: 3
8
8
  Classifier: License :: OSI Approved :: MIT License
9
9
  Classifier: Operating System :: OS Independent
10
10
  Requires-Python: >=3.9
11
- Requires-Dist: microsoft-agents-hosting-core==0.1.1
11
+ Requires-Dist: microsoft-agents-hosting-core==0.2.0.dev1
12
12
  Requires-Dist: msal>=1.31.1
13
13
  Requires-Dist: requests>=2.32.3
14
14
  Requires-Dist: cryptography>=44.0.0
@@ -80,9 +80,18 @@ class MsalAuth(AccessTokenProviderBase):
80
80
  )
81
81
  elif isinstance(msal_auth_client, ConfidentialClientApplication):
82
82
  # TODO: Handling token error / acquisition failed
83
- return msal_auth_client.acquire_token_on_behalf_of(
83
+
84
+ token = msal_auth_client.acquire_token_on_behalf_of(
84
85
  user_assertion=user_assertion, scopes=scopes
85
- )["access_token"]
86
+ )
87
+
88
+ if "access_token" not in token:
89
+ logger.error(
90
+ f"Failed to acquire token on behalf of user: {user_assertion}"
91
+ )
92
+ raise ValueError(f"Failed to acquire token. {str(token)}")
93
+
94
+ return token["access_token"]
86
95
 
87
96
  logger.error(
88
97
  f"On-behalf-of flow is not supported with the current authentication type: {msal_auth_client.__class__.__name__}"
@@ -1,5 +1,5 @@
1
1
  from typing import Dict, List, Optional
2
- from microsoft.agents.hosting.core.authorization import (
2
+ from microsoft.agents.hosting.core import (
3
3
  AgentAuthConfiguration,
4
4
  AccessTokenProviderBase,
5
5
  ClaimsIdentity,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: microsoft-agents-authentication-msal
3
- Version: 0.1.1
3
+ Version: 0.2.0.dev1
4
4
  Summary: A msal-based authentication library for Microsoft Agents
5
5
  Author: Microsoft Corporation
6
6
  Project-URL: Homepage, https://github.com/microsoft/Agents
@@ -8,7 +8,7 @@ Classifier: Programming Language :: Python :: 3
8
8
  Classifier: License :: OSI Approved :: MIT License
9
9
  Classifier: Operating System :: OS Independent
10
10
  Requires-Python: >=3.9
11
- Requires-Dist: microsoft-agents-hosting-core==0.1.1
11
+ Requires-Dist: microsoft-agents-hosting-core==0.2.0.dev1
12
12
  Requires-Dist: msal>=1.31.1
13
13
  Requires-Dist: requests>=2.32.3
14
14
  Requires-Dist: cryptography>=44.0.0
@@ -7,4 +7,6 @@ microsoft_agents_authentication_msal.egg-info/PKG-INFO
7
7
  microsoft_agents_authentication_msal.egg-info/SOURCES.txt
8
8
  microsoft_agents_authentication_msal.egg-info/dependency_links.txt
9
9
  microsoft_agents_authentication_msal.egg-info/requires.txt
10
- microsoft_agents_authentication_msal.egg-info/top_level.txt
10
+ microsoft_agents_authentication_msal.egg-info/top_level.txt
11
+ tests/test_msal_auth.py
12
+ tests/test_msal_connection_manager.py
@@ -1,4 +1,4 @@
1
- microsoft-agents-hosting-core==0.1.1
1
+ microsoft-agents-hosting-core==0.2.0.dev1
2
2
  msal>=1.31.1
3
3
  requests>=2.32.3
4
4
  cryptography>=44.0.0
@@ -0,0 +1,83 @@
1
+ import unittest
2
+ from unittest.mock import Mock
3
+ import pytest
4
+ from msal import ManagedIdentityClient, ConfidentialClientApplication
5
+ from microsoft.agents.authentication.msal import MsalAuth
6
+ from microsoft.agents.hosting.core.authorization import AgentAuthConfiguration
7
+
8
+
9
+ class TestingMsalAuth(MsalAuth):
10
+ """
11
+ Mock object for MsalAuth
12
+ """
13
+
14
+ def __init__(self, client_type):
15
+ super().__init__(AgentAuthConfiguration())
16
+ mock_client = Mock(spec=client_type)
17
+
18
+ mock_client.acquire_token_for_client = Mock(
19
+ return_value={"access_token": "token"}
20
+ )
21
+ mock_client.acquire_token_on_behalf_of = Mock(
22
+ return_value={"access_token": "token"}
23
+ )
24
+ self.mock_client = mock_client
25
+
26
+ self._create_client_application = Mock(return_value=self.mock_client)
27
+
28
+
29
+ class TestMsalAuth:
30
+ """
31
+ Test suite for testing MsalAuth functionality
32
+ """
33
+
34
+ @pytest.mark.asyncio
35
+ async def test_get_access_token_managed_identity(self):
36
+ mock_auth = TestingMsalAuth(ManagedIdentityClient)
37
+ token = await mock_auth.get_access_token(
38
+ "https://test.api.botframework.com", scopes=["test-scope"]
39
+ )
40
+
41
+ assert token == "token"
42
+ mock_auth.mock_client.acquire_token_for_client.assert_called_with(
43
+ resource="https://test.api.botframework.com"
44
+ )
45
+
46
+ @pytest.mark.asyncio
47
+ async def test_get_access_token_confidential(self):
48
+ mock_auth = TestingMsalAuth(ConfidentialClientApplication)
49
+ token = await mock_auth.get_access_token(
50
+ "https://test.api.botframework.com", scopes=["test-scope"]
51
+ )
52
+
53
+ assert token == "token"
54
+ mock_auth.mock_client.acquire_token_for_client.assert_called_with(
55
+ scopes=["test-scope"]
56
+ )
57
+
58
+ @pytest.mark.asyncio
59
+ async def test_aquire_token_on_behalf_of_managed_identity(self):
60
+ mock_auth = TestingMsalAuth(ManagedIdentityClient)
61
+
62
+ try:
63
+ await mock_auth.aquire_token_on_behalf_of(
64
+ scopes=["test-scope"], user_assertion="test-assertion"
65
+ )
66
+ except NotImplementedError:
67
+ assert True
68
+ else:
69
+ assert False
70
+
71
+ @pytest.mark.asyncio
72
+ async def test_aquire_token_on_behalf_of_confidential(self):
73
+ mock_auth = TestingMsalAuth(ConfidentialClientApplication)
74
+ mock_auth._create_client_application = Mock(return_value=mock_auth.mock_client)
75
+
76
+ token = await mock_auth.aquire_token_on_behalf_of(
77
+ scopes=["test-scope"], user_assertion="test-assertion"
78
+ )
79
+
80
+ assert token == "token"
81
+ mock_auth.mock_client.acquire_token_on_behalf_of.assert_called_with(
82
+ scopes=["test-scope"], user_assertion="test-assertion"
83
+ )
@@ -0,0 +1,35 @@
1
+ from os import environ
2
+ from microsoft.agents.activity import load_configuration_from_env
3
+ from microsoft.agents.hosting.core import AuthTypes
4
+ from microsoft.agents.authentication.msal import MsalConnectionManager
5
+
6
+
7
+ class TestMsalConnectionManager:
8
+ """
9
+ Test suite for the Msal Connection Manager
10
+ """
11
+
12
+ def test_msal_connection_manager(self):
13
+ mock_environ = {
14
+ **environ,
15
+ "CONNECTIONS__SERVICE_CONNECTION__SETTINGS__TENANTID": "test-tenant-id-SERVICE_CONNECTION",
16
+ "CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTID": "test-client-id-SERVICE_CONNECTION",
17
+ "CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTSECRET": "test-client-secret-SERVICE_CONNECTION",
18
+ "CONNECTIONS__MCS__SETTINGS__TENANTID": "test-tenant-id-MCS",
19
+ "CONNECTIONS__MCS__SETTINGS__CLIENTID": "test-client-id-MCS",
20
+ "CONNECTIONS__MCS__SETTINGS__CLIENTSECRET": "test-client-secret-MCS",
21
+ }
22
+
23
+ config = load_configuration_from_env(mock_environ)
24
+ connection_manager = MsalConnectionManager(**config)
25
+ for key in connection_manager._connections:
26
+ auth = connection_manager.get_connection(key)._msal_configuration
27
+ assert auth.AUTH_TYPE == AuthTypes.client_secret
28
+ assert auth.CLIENT_ID == f"test-client-id-{key}"
29
+ assert auth.TENANT_ID == f"test-tenant-id-{key}"
30
+ assert auth.CLIENT_SECRET == f"test-client-secret-{key}"
31
+ assert auth.ISSUERS == [
32
+ "https://api.botframework.com",
33
+ f"https://sts.windows.net/test-tenant-id-{key}/",
34
+ f"https://login.microsoftonline.com/test-tenant-id-{key}/v2.0",
35
+ ]