microsoft-agents-authentication-msal 0.5.0.dev11__tar.gz → 0.5.0.dev19__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.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/PKG-INFO +18 -4
  2. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents/authentication/msal/msal_auth.py +72 -47
  3. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents_authentication_msal.egg-info/PKG-INFO +18 -4
  4. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents_authentication_msal.egg-info/requires.txt +1 -1
  5. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/pyproject.toml +5 -0
  6. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/readme.md +11 -2
  7. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/LICENSE +0 -0
  8. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents/authentication/msal/__init__.py +0 -0
  9. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents/authentication/msal/msal_connection_manager.py +0 -0
  10. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents_authentication_msal.egg-info/SOURCES.txt +0 -0
  11. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents_authentication_msal.egg-info/dependency_links.txt +0 -0
  12. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/microsoft_agents_authentication_msal.egg-info/top_level.txt +0 -0
  13. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/setup.cfg +0 -0
  14. {microsoft_agents_authentication_msal-0.5.0.dev11 → microsoft_agents_authentication_msal-0.5.0.dev19}/setup.py +0 -0
@@ -1,16 +1,21 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: microsoft-agents-authentication-msal
3
- Version: 0.5.0.dev11
3
+ Version: 0.5.0.dev19
4
4
  Summary: A msal-based authentication library for Microsoft Agents
5
5
  Author: Microsoft Corporation
6
6
  License-Expression: MIT
7
7
  Project-URL: Homepage, https://github.com/microsoft/Agents
8
8
  Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
9
14
  Classifier: Operating System :: OS Independent
10
15
  Requires-Python: >=3.10
11
16
  Description-Content-Type: text/markdown
12
17
  License-File: LICENSE
13
- Requires-Dist: microsoft-agents-hosting-core==0.5.0.dev11
18
+ Requires-Dist: microsoft-agents-hosting-core==0.5.0.dev19
14
19
  Requires-Dist: msal>=1.31.1
15
20
  Requires-Dist: requests>=2.32.3
16
21
  Requires-Dist: cryptography>=44.0.0
@@ -130,5 +135,14 @@ class AuthTypes(str, Enum):
130
135
  - 🐛 [Report Issues](https://github.com/microsoft/Agents-for-python/issues)
131
136
 
132
137
  # Sample Applications
133
-
134
- w
138
+ Explore working examples in the [Python samples repository](https://github.com/microsoft/Agents/tree/main/samples/python):
139
+
140
+ |Name|Description|README|
141
+ |----|----|----|
142
+ |Quickstart|Simplest agent|[Quickstart](https://github.com/microsoft/Agents/blob/main/samples/python/quickstart/README.md)|
143
+ |Auto Sign In|Simple OAuth agent using Graph and GitHub|[auto-signin](https://github.com/microsoft/Agents/blob/main/samples/python/auto-signin/README.md)|
144
+ |OBO Authorization|OBO flow to access a Copilot Studio Agent|[obo-authorization](https://github.com/microsoft/Agents/blob/main/samples/python/obo-authorization/README.md)|
145
+ |Semantic Kernel Integration|A weather agent built with Semantic Kernel|[semantic-kernel-multiturn](https://github.com/microsoft/Agents/blob/main/samples/python/semantic-kernel-multiturn/README.md)|
146
+ |Streaming Agent|Streams OpenAI responses|[azure-ai-streaming](https://github.com/microsoft/Agents/blob/main/samples/python/azureai-streaming/README.md)|
147
+ |Copilot Studio Client|Console app to consume a Copilot Studio Agent|[copilotstudio-client](https://github.com/microsoft/Agents/blob/main/samples/python/copilotstudio-client/README.md)|
148
+ |Cards Agent|Agent that uses rich cards to enhance conversation design |[cards](https://github.com/microsoft/Agents/blob/main/samples/python/cards/README.md)|
@@ -3,6 +3,7 @@
3
3
 
4
4
  from __future__ import annotations
5
5
 
6
+ import asyncio
6
7
  import logging
7
8
  import jwt
8
9
  from typing import Optional
@@ -39,12 +40,29 @@ class _DeferredLogOfBlueprintId:
39
40
  return f"Agentic blueprint id: {agentic_blueprint_id}"
40
41
 
41
42
 
43
+ async def _async_acquire_token_for_client(msal_auth_client, *args, **kwargs):
44
+ """MSAL in Python does not support async, so we use asyncio.to_thread to run it in
45
+ a separate thread and avoid blocking the event loop
46
+ """
47
+ return await asyncio.to_thread(
48
+ lambda: msal_auth_client.acquire_token_for_client(*args, **kwargs)
49
+ )
50
+
51
+
42
52
  class MsalAuth(AccessTokenProviderBase):
43
53
 
44
54
  _client_credential_cache = None
45
55
 
46
56
  def __init__(self, msal_configuration: AgentAuthConfiguration):
57
+ """Initializes the MsalAuth class with the given configuration.
58
+
59
+ :param msal_configuration: The MSAL authentication configuration. Assumed to
60
+ not be mutated after being passed in.
61
+ :type msal_configuration: AgentAuthConfiguration
62
+ """
63
+
47
64
  self._msal_configuration = msal_configuration
65
+ self._msal_auth_client = None
48
66
  logger.debug(
49
67
  f"Initializing MsalAuth with configuration: {self._msal_configuration}"
50
68
  )
@@ -60,17 +78,17 @@ class MsalAuth(AccessTokenProviderBase):
60
78
  raise ValueError("Invalid instance URL")
61
79
 
62
80
  local_scopes = self._resolve_scopes_list(instance_uri, scopes)
63
- msal_auth_client = self._create_client_application()
81
+ self._create_client_application()
64
82
 
65
- if isinstance(msal_auth_client, ManagedIdentityClient):
83
+ if isinstance(self._msal_auth_client, ManagedIdentityClient):
66
84
  logger.info("Acquiring token using Managed Identity Client.")
67
- auth_result_payload = msal_auth_client.acquire_token_for_client(
68
- resource=resource_url
85
+ auth_result_payload = await _async_acquire_token_for_client(
86
+ self._msal_auth_client, resource=resource_url
69
87
  )
70
- elif isinstance(msal_auth_client, ConfidentialClientApplication):
88
+ elif isinstance(self._msal_auth_client, ConfidentialClientApplication):
71
89
  logger.info("Acquiring token using Confidential Client Application.")
72
- auth_result_payload = msal_auth_client.acquire_token_for_client(
73
- scopes=local_scopes
90
+ auth_result_payload = await _async_acquire_token_for_client(
91
+ self._msal_auth_client, scopes=local_scopes
74
92
  )
75
93
  else:
76
94
  auth_result_payload = None
@@ -79,6 +97,7 @@ class MsalAuth(AccessTokenProviderBase):
79
97
  if not res:
80
98
  logger.error("Failed to acquire token for resource %s", auth_result_payload)
81
99
  raise ValueError(f"Failed to acquire token. {str(auth_result_payload)}")
100
+
82
101
  return res
83
102
 
84
103
  async def acquire_token_on_behalf_of(
@@ -91,19 +110,23 @@ class MsalAuth(AccessTokenProviderBase):
91
110
  :return: The access token as a string.
92
111
  """
93
112
 
94
- msal_auth_client = self._create_client_application()
95
- if isinstance(msal_auth_client, ManagedIdentityClient):
113
+ self._create_client_application()
114
+ if isinstance(self._msal_auth_client, ManagedIdentityClient):
96
115
  logger.error(
97
116
  "Attempted on-behalf-of flow with Managed Identity authentication."
98
117
  )
99
118
  raise NotImplementedError(
100
119
  "On-behalf-of flow is not supported with Managed Identity authentication."
101
120
  )
102
- elif isinstance(msal_auth_client, ConfidentialClientApplication):
121
+ elif isinstance(self._msal_auth_client, ConfidentialClientApplication):
103
122
  # TODO: Handling token error / acquisition failed
104
123
 
105
- token = msal_auth_client.acquire_token_on_behalf_of(
106
- user_assertion=user_assertion, scopes=scopes
124
+ # MSAL in Python does not support async, so we use asyncio.to_thread to run it in
125
+ # a separate thread and avoid blocking the event loop
126
+ token = await asyncio.to_thread(
127
+ lambda: self._msal_auth_client.acquire_token_on_behalf_of(
128
+ scopes=scopes, user_assertion=user_assertion
129
+ )
107
130
  )
108
131
 
109
132
  if "access_token" not in token:
@@ -115,19 +138,19 @@ class MsalAuth(AccessTokenProviderBase):
115
138
  return token["access_token"]
116
139
 
117
140
  logger.error(
118
- f"On-behalf-of flow is not supported with the current authentication type: {msal_auth_client.__class__.__name__}"
141
+ f"On-behalf-of flow is not supported with the current authentication type: {self._msal_auth_client.__class__.__name__}"
119
142
  )
120
143
  raise NotImplementedError(
121
- f"On-behalf-of flow is not supported with the current authentication type: {msal_auth_client.__class__.__name__}"
144
+ f"On-behalf-of flow is not supported with the current authentication type: {self._msal_auth_client.__class__.__name__}"
122
145
  )
123
146
 
124
- def _create_client_application(
125
- self,
126
- ) -> ManagedIdentityClient | ConfidentialClientApplication:
127
- msal_auth_client = None
147
+ def _create_client_application(self) -> None:
148
+
149
+ if self._msal_auth_client:
150
+ return
128
151
 
129
152
  if self._msal_configuration.AUTH_TYPE == AuthTypes.user_managed_identity:
130
- msal_auth_client = ManagedIdentityClient(
153
+ self._msal_auth_client = ManagedIdentityClient(
131
154
  UserAssignedManagedIdentity(
132
155
  client_id=self._msal_configuration.CLIENT_ID
133
156
  ),
@@ -135,7 +158,7 @@ class MsalAuth(AccessTokenProviderBase):
135
158
  )
136
159
 
137
160
  elif self._msal_configuration.AUTH_TYPE == AuthTypes.system_managed_identity:
138
- msal_auth_client = ManagedIdentityClient(
161
+ self._msal_auth_client = ManagedIdentityClient(
139
162
  SystemAssignedManagedIdentity(),
140
163
  http_client=Session(),
141
164
  )
@@ -176,14 +199,12 @@ class MsalAuth(AccessTokenProviderBase):
176
199
  )
177
200
  raise NotImplementedError("Authentication type not supported")
178
201
 
179
- msal_auth_client = ConfidentialClientApplication(
202
+ self._msal_auth_client = ConfidentialClientApplication(
180
203
  client_id=self._msal_configuration.CLIENT_ID,
181
204
  authority=authority,
182
205
  client_credential=self._client_credential_cache,
183
206
  )
184
207
 
185
- return msal_auth_client
186
-
187
208
  @staticmethod
188
209
  def _uri_validator(url_str: str) -> tuple[bool, Optional[URI]]:
189
210
  try:
@@ -228,12 +249,13 @@ class MsalAuth(AccessTokenProviderBase):
228
249
  "Attempting to get agentic application token from agent_app_instance_id %s",
229
250
  agent_app_instance_id,
230
251
  )
231
- msal_auth_client = self._create_client_application()
252
+ self._create_client_application()
232
253
 
233
- if isinstance(msal_auth_client, ConfidentialClientApplication):
254
+ if isinstance(self._msal_auth_client, ConfidentialClientApplication):
234
255
 
235
256
  # https://github.dev/AzureAD/microsoft-authentication-library-for-dotnet
236
- auth_result_payload = msal_auth_client.acquire_token_for_client(
257
+ auth_result_payload = await _async_acquire_token_for_client(
258
+ self._msal_auth_client,
237
259
  ["api://AzureAdTokenExchange/.default"],
238
260
  data={"fmi_path": agent_app_instance_id},
239
261
  )
@@ -284,8 +306,8 @@ class MsalAuth(AccessTokenProviderBase):
284
306
  client_credential={"client_assertion": agent_token_result},
285
307
  )
286
308
 
287
- agentic_instance_token = instance_app.acquire_token_for_client(
288
- ["api://AzureAdTokenExchange/.default"]
309
+ agentic_instance_token = await _async_acquire_token_for_client(
310
+ instance_app, ["api://AzureAdTokenExchange/.default"]
289
311
  )
290
312
 
291
313
  if not agentic_instance_token:
@@ -311,28 +333,28 @@ class MsalAuth(AccessTokenProviderBase):
311
333
  return agentic_instance_token["access_token"], agent_token_result
312
334
 
313
335
  async def get_agentic_user_token(
314
- self, agent_app_instance_id: str, upn: str, scopes: list[str]
336
+ self, agent_app_instance_id: str, agentic_user_id: str, scopes: list[str]
315
337
  ) -> Optional[str]:
316
- """Gets the agentic user token for the given agent application instance ID and user principal name and the scopes.
338
+ """Gets the agentic user token for the given agent application instance ID and agentic user Id and the scopes.
317
339
 
318
340
  :param agent_app_instance_id: The agent application instance ID.
319
341
  :type agent_app_instance_id: str
320
- :param upn: The user principal name.
321
- :type upn: str
342
+ :param agentic_user_id: The agentic user ID.
343
+ :type agentic_user_id: str
322
344
  :param scopes: The scopes to request for the token.
323
345
  :type scopes: list[str]
324
346
  :return: The agentic user token, or None if not found.
325
347
  :rtype: Optional[str]
326
348
  """
327
- if not agent_app_instance_id or not upn:
349
+ if not agent_app_instance_id or not agentic_user_id:
328
350
  raise ValueError(
329
- "Agent application instance Id and user principal name must be provided."
351
+ "Agent application instance Id and agentic user Id must be provided."
330
352
  )
331
353
 
332
354
  logger.info(
333
- "Attempting to get agentic user token from agent_app_instance_id %s and upn %s",
355
+ "Attempting to get agentic user token from agent_app_instance_id %s and agentic_user_id %s",
334
356
  agent_app_instance_id,
335
- upn,
357
+ agentic_user_id,
336
358
  )
337
359
  instance_token, agent_token = await self.get_agentic_instance_token(
338
360
  agent_app_instance_id
@@ -340,12 +362,12 @@ class MsalAuth(AccessTokenProviderBase):
340
362
 
341
363
  if not instance_token or not agent_token:
342
364
  logger.error(
343
- "Failed to acquire instance token or agent token for agent_app_instance_id %s and upn %s",
365
+ "Failed to acquire instance token or agent token for agent_app_instance_id %s and agentic_user_id %s",
344
366
  agent_app_instance_id,
345
- upn,
367
+ agentic_user_id,
346
368
  )
347
369
  raise Exception(
348
- f"Failed to acquire instance token or agent token for agent_app_instance_id {agent_app_instance_id} and upn {upn}"
370
+ f"Failed to acquire instance token or agent token for agent_app_instance_id {agent_app_instance_id} and agentic_user_id {agentic_user_id}"
349
371
  )
350
372
 
351
373
  authority = (
@@ -359,14 +381,17 @@ class MsalAuth(AccessTokenProviderBase):
359
381
  )
360
382
 
361
383
  logger.info(
362
- "Acquiring agentic user token for agent_app_instance_id %s and upn %s",
384
+ "Acquiring agentic user token for agent_app_instance_id %s and agentic_user_id %s",
363
385
  agent_app_instance_id,
364
- upn,
386
+ agentic_user_id,
365
387
  )
366
- auth_result_payload = instance_app.acquire_token_for_client(
388
+ # MSAL in Python does not support async, so we use asyncio.to_thread to run it in
389
+ # a separate thread and avoid blocking the event loop
390
+ auth_result_payload = await _async_acquire_token_for_client(
391
+ instance_app,
367
392
  scopes,
368
393
  data={
369
- "username": upn,
394
+ "user_id": agentic_user_id,
370
395
  "user_federated_identity_credential": instance_token,
371
396
  "grant_type": "user_fic",
372
397
  },
@@ -374,9 +399,9 @@ class MsalAuth(AccessTokenProviderBase):
374
399
 
375
400
  if not auth_result_payload:
376
401
  logger.error(
377
- "Failed to acquire agentic user token for agent_app_instance_id %s and upn %s, %s",
402
+ "Failed to acquire agentic user token for agent_app_instance_id %s and agentic_user_id %s, %s",
378
403
  agent_app_instance_id,
379
- upn,
404
+ agentic_user_id,
380
405
  auth_result_payload,
381
406
  )
382
407
  return None
@@ -384,9 +409,9 @@ class MsalAuth(AccessTokenProviderBase):
384
409
  access_token = auth_result_payload.get("access_token")
385
410
  if not access_token:
386
411
  logger.error(
387
- "Failed to acquire agentic user token for agent_app_instance_id %s and upn %s, %s",
412
+ "Failed to acquire agentic user token for agent_app_instance_id %s and agentic_user_id %s, %s",
388
413
  agent_app_instance_id,
389
- upn,
414
+ agentic_user_id,
390
415
  auth_result_payload,
391
416
  )
392
417
  return None
@@ -1,16 +1,21 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: microsoft-agents-authentication-msal
3
- Version: 0.5.0.dev11
3
+ Version: 0.5.0.dev19
4
4
  Summary: A msal-based authentication library for Microsoft Agents
5
5
  Author: Microsoft Corporation
6
6
  License-Expression: MIT
7
7
  Project-URL: Homepage, https://github.com/microsoft/Agents
8
8
  Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
9
14
  Classifier: Operating System :: OS Independent
10
15
  Requires-Python: >=3.10
11
16
  Description-Content-Type: text/markdown
12
17
  License-File: LICENSE
13
- Requires-Dist: microsoft-agents-hosting-core==0.5.0.dev11
18
+ Requires-Dist: microsoft-agents-hosting-core==0.5.0.dev19
14
19
  Requires-Dist: msal>=1.31.1
15
20
  Requires-Dist: requests>=2.32.3
16
21
  Requires-Dist: cryptography>=44.0.0
@@ -130,5 +135,14 @@ class AuthTypes(str, Enum):
130
135
  - 🐛 [Report Issues](https://github.com/microsoft/Agents-for-python/issues)
131
136
 
132
137
  # Sample Applications
133
-
134
- w
138
+ Explore working examples in the [Python samples repository](https://github.com/microsoft/Agents/tree/main/samples/python):
139
+
140
+ |Name|Description|README|
141
+ |----|----|----|
142
+ |Quickstart|Simplest agent|[Quickstart](https://github.com/microsoft/Agents/blob/main/samples/python/quickstart/README.md)|
143
+ |Auto Sign In|Simple OAuth agent using Graph and GitHub|[auto-signin](https://github.com/microsoft/Agents/blob/main/samples/python/auto-signin/README.md)|
144
+ |OBO Authorization|OBO flow to access a Copilot Studio Agent|[obo-authorization](https://github.com/microsoft/Agents/blob/main/samples/python/obo-authorization/README.md)|
145
+ |Semantic Kernel Integration|A weather agent built with Semantic Kernel|[semantic-kernel-multiturn](https://github.com/microsoft/Agents/blob/main/samples/python/semantic-kernel-multiturn/README.md)|
146
+ |Streaming Agent|Streams OpenAI responses|[azure-ai-streaming](https://github.com/microsoft/Agents/blob/main/samples/python/azureai-streaming/README.md)|
147
+ |Copilot Studio Client|Console app to consume a Copilot Studio Agent|[copilotstudio-client](https://github.com/microsoft/Agents/blob/main/samples/python/copilotstudio-client/README.md)|
148
+ |Cards Agent|Agent that uses rich cards to enhance conversation design |[cards](https://github.com/microsoft/Agents/blob/main/samples/python/cards/README.md)|
@@ -1,4 +1,4 @@
1
- microsoft-agents-hosting-core==0.5.0.dev11
1
+ microsoft-agents-hosting-core==0.5.0.dev19
2
2
  msal>=1.31.1
3
3
  requests>=2.32.3
4
4
  cryptography>=44.0.0
@@ -13,6 +13,11 @@ license-files = ["LICENSE"]
13
13
  requires-python = ">=3.10"
14
14
  classifiers = [
15
15
  "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.10",
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Programming Language :: Python :: 3.14",
16
21
  "Operating System :: OS Independent",
17
22
  ]
18
23
 
@@ -111,5 +111,14 @@ class AuthTypes(str, Enum):
111
111
  - 🐛 [Report Issues](https://github.com/microsoft/Agents-for-python/issues)
112
112
 
113
113
  # Sample Applications
114
-
115
- w
114
+ Explore working examples in the [Python samples repository](https://github.com/microsoft/Agents/tree/main/samples/python):
115
+
116
+ |Name|Description|README|
117
+ |----|----|----|
118
+ |Quickstart|Simplest agent|[Quickstart](https://github.com/microsoft/Agents/blob/main/samples/python/quickstart/README.md)|
119
+ |Auto Sign In|Simple OAuth agent using Graph and GitHub|[auto-signin](https://github.com/microsoft/Agents/blob/main/samples/python/auto-signin/README.md)|
120
+ |OBO Authorization|OBO flow to access a Copilot Studio Agent|[obo-authorization](https://github.com/microsoft/Agents/blob/main/samples/python/obo-authorization/README.md)|
121
+ |Semantic Kernel Integration|A weather agent built with Semantic Kernel|[semantic-kernel-multiturn](https://github.com/microsoft/Agents/blob/main/samples/python/semantic-kernel-multiturn/README.md)|
122
+ |Streaming Agent|Streams OpenAI responses|[azure-ai-streaming](https://github.com/microsoft/Agents/blob/main/samples/python/azureai-streaming/README.md)|
123
+ |Copilot Studio Client|Console app to consume a Copilot Studio Agent|[copilotstudio-client](https://github.com/microsoft/Agents/blob/main/samples/python/copilotstudio-client/README.md)|
124
+ |Cards Agent|Agent that uses rich cards to enhance conversation design |[cards](https://github.com/microsoft/Agents/blob/main/samples/python/cards/README.md)|