microsoft-agents-authentication-msal 0.5.3__py3-none-any.whl → 0.6.0__py3-none-any.whl
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.
- microsoft_agents/authentication/msal/errors/__init__.py +15 -0
- microsoft_agents/authentication/msal/errors/error_resources.py +67 -0
- microsoft_agents/authentication/msal/msal_auth.py +48 -26
- {microsoft_agents_authentication_msal-0.5.3.dist-info → microsoft_agents_authentication_msal-0.6.0.dist-info}/METADATA +2 -2
- microsoft_agents_authentication_msal-0.6.0.dist-info/RECORD +10 -0
- microsoft_agents_authentication_msal-0.5.3.dist-info/RECORD +0 -8
- {microsoft_agents_authentication_msal-0.5.3.dist-info → microsoft_agents_authentication_msal-0.6.0.dist-info}/WHEEL +0 -0
- {microsoft_agents_authentication_msal-0.5.3.dist-info → microsoft_agents_authentication_msal-0.6.0.dist-info}/licenses/LICENSE +0 -0
- {microsoft_agents_authentication_msal-0.5.3.dist-info → microsoft_agents_authentication_msal-0.6.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Error resources for Microsoft Agents Authentication MSAL package.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from microsoft_agents.activity.errors import ErrorMessage
|
|
9
|
+
|
|
10
|
+
from .error_resources import AuthenticationErrorResources
|
|
11
|
+
|
|
12
|
+
# Singleton instance
|
|
13
|
+
authentication_errors = AuthenticationErrorResources()
|
|
14
|
+
|
|
15
|
+
__all__ = ["ErrorMessage", "AuthenticationErrorResources", "authentication_errors"]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Authentication error resources for Microsoft Agents SDK.
|
|
6
|
+
|
|
7
|
+
Error codes are in the range -60000 to -60999.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from microsoft_agents.activity.errors import ErrorMessage
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AuthenticationErrorResources:
|
|
14
|
+
"""
|
|
15
|
+
Error messages for authentication operations.
|
|
16
|
+
|
|
17
|
+
Error codes are organized in the range -60000 to -60999.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
FailedToAcquireToken = ErrorMessage(
|
|
21
|
+
"Failed to acquire token. {0}",
|
|
22
|
+
-60012,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
InvalidInstanceUrl = ErrorMessage(
|
|
26
|
+
"Invalid instance URL",
|
|
27
|
+
-60013,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
OnBehalfOfFlowNotSupportedManagedIdentity = ErrorMessage(
|
|
31
|
+
"On-behalf-of flow is not supported with Managed Identity authentication.",
|
|
32
|
+
-60014,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
OnBehalfOfFlowNotSupportedAuthType = ErrorMessage(
|
|
36
|
+
"On-behalf-of flow is not supported with the current authentication type: {0}",
|
|
37
|
+
-60015,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
AuthenticationTypeNotSupported = ErrorMessage(
|
|
41
|
+
"Authentication type not supported",
|
|
42
|
+
-60016,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
AgentApplicationInstanceIdRequired = ErrorMessage(
|
|
46
|
+
"Agent application instance Id must be provided.",
|
|
47
|
+
-60017,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
FailedToAcquireAgenticInstanceToken = ErrorMessage(
|
|
51
|
+
"Failed to acquire agentic instance token or agent token for agent_app_instance_id {0}",
|
|
52
|
+
-60018,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
AgentApplicationInstanceIdAndUserIdRequired = ErrorMessage(
|
|
56
|
+
"Agent application instance Id and agentic user Id must be provided.",
|
|
57
|
+
-60019,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
FailedToAcquireInstanceOrAgentToken = ErrorMessage(
|
|
61
|
+
"Failed to acquire instance token or agent token for agent_app_instance_id {0} and agentic_user_id {1}",
|
|
62
|
+
-60020,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def __init__(self):
|
|
66
|
+
"""Initialize AuthenticationErrorResources."""
|
|
67
|
+
pass
|
|
@@ -19,27 +19,18 @@ from cryptography.x509 import load_pem_x509_certificate
|
|
|
19
19
|
from cryptography.hazmat.backends import default_backend
|
|
20
20
|
from cryptography.hazmat.primitives import hashes
|
|
21
21
|
|
|
22
|
+
from microsoft_agents.activity._utils import _DeferredString
|
|
23
|
+
|
|
22
24
|
from microsoft_agents.hosting.core import (
|
|
23
25
|
AuthTypes,
|
|
24
26
|
AccessTokenProviderBase,
|
|
25
27
|
AgentAuthConfiguration,
|
|
26
28
|
)
|
|
29
|
+
from microsoft_agents.authentication.msal.errors import authentication_errors
|
|
27
30
|
|
|
28
31
|
logger = logging.getLogger(__name__)
|
|
29
32
|
|
|
30
33
|
|
|
31
|
-
# this is deferred because jwt.decode is expensive and we don't want to do it unless we
|
|
32
|
-
# have logging.DEBUG enabled
|
|
33
|
-
class _DeferredLogOfBlueprintId:
|
|
34
|
-
def __init__(self, jwt_token: str):
|
|
35
|
-
self.jwt_token = jwt_token
|
|
36
|
-
|
|
37
|
-
def __str__(self):
|
|
38
|
-
payload = jwt.decode(self.jwt_token, options={"verify_signature": False})
|
|
39
|
-
agentic_blueprint_id = payload.get("xms_par_app_azp")
|
|
40
|
-
return f"Agentic blueprint id: {agentic_blueprint_id}"
|
|
41
|
-
|
|
42
|
-
|
|
43
34
|
async def _async_acquire_token_for_client(msal_auth_client, *args, **kwargs):
|
|
44
35
|
"""MSAL in Python does not support async, so we use asyncio.to_thread to run it in
|
|
45
36
|
a separate thread and avoid blocking the event loop
|
|
@@ -75,7 +66,7 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
75
66
|
)
|
|
76
67
|
valid_uri, instance_uri = self._uri_validator(resource_url)
|
|
77
68
|
if not valid_uri:
|
|
78
|
-
raise ValueError(
|
|
69
|
+
raise ValueError(str(authentication_errors.InvalidInstanceUrl))
|
|
79
70
|
|
|
80
71
|
local_scopes = self._resolve_scopes_list(instance_uri, scopes)
|
|
81
72
|
self._create_client_application()
|
|
@@ -96,7 +87,11 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
96
87
|
res = auth_result_payload.get("access_token") if auth_result_payload else None
|
|
97
88
|
if not res:
|
|
98
89
|
logger.error("Failed to acquire token for resource %s", auth_result_payload)
|
|
99
|
-
raise ValueError(
|
|
90
|
+
raise ValueError(
|
|
91
|
+
authentication_errors.FailedToAcquireToken.format(
|
|
92
|
+
str(auth_result_payload)
|
|
93
|
+
)
|
|
94
|
+
)
|
|
100
95
|
|
|
101
96
|
return res
|
|
102
97
|
|
|
@@ -116,7 +111,7 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
116
111
|
"Attempted on-behalf-of flow with Managed Identity authentication."
|
|
117
112
|
)
|
|
118
113
|
raise NotImplementedError(
|
|
119
|
-
|
|
114
|
+
str(authentication_errors.OnBehalfOfFlowNotSupportedManagedIdentity)
|
|
120
115
|
)
|
|
121
116
|
elif isinstance(self._msal_auth_client, ConfidentialClientApplication):
|
|
122
117
|
# TODO: Handling token error / acquisition failed
|
|
@@ -133,7 +128,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
133
128
|
logger.error(
|
|
134
129
|
f"Failed to acquire token on behalf of user: {user_assertion}"
|
|
135
130
|
)
|
|
136
|
-
raise ValueError(
|
|
131
|
+
raise ValueError(
|
|
132
|
+
authentication_errors.FailedToAcquireToken.format(str(token))
|
|
133
|
+
)
|
|
137
134
|
|
|
138
135
|
return token["access_token"]
|
|
139
136
|
|
|
@@ -141,7 +138,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
141
138
|
f"On-behalf-of flow is not supported with the current authentication type: {self._msal_auth_client.__class__.__name__}"
|
|
142
139
|
)
|
|
143
140
|
raise NotImplementedError(
|
|
144
|
-
|
|
141
|
+
authentication_errors.OnBehalfOfFlowNotSupportedAuthType.format(
|
|
142
|
+
self._msal_auth_client.__class__.__name__
|
|
143
|
+
)
|
|
145
144
|
)
|
|
146
145
|
|
|
147
146
|
def _create_client_application(self) -> None:
|
|
@@ -197,7 +196,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
197
196
|
logger.error(
|
|
198
197
|
f"Unsupported authentication type: {self._msal_configuration.AUTH_TYPE}"
|
|
199
198
|
)
|
|
200
|
-
raise NotImplementedError(
|
|
199
|
+
raise NotImplementedError(
|
|
200
|
+
str(authentication_errors.AuthenticationTypeNotSupported)
|
|
201
|
+
)
|
|
201
202
|
|
|
202
203
|
self._msal_auth_client = ConfidentialClientApplication(
|
|
203
204
|
client_id=self._msal_configuration.CLIENT_ID,
|
|
@@ -243,7 +244,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
243
244
|
"""
|
|
244
245
|
|
|
245
246
|
if not agent_app_instance_id:
|
|
246
|
-
raise ValueError(
|
|
247
|
+
raise ValueError(
|
|
248
|
+
str(authentication_errors.AgentApplicationInstanceIdRequired)
|
|
249
|
+
)
|
|
247
250
|
|
|
248
251
|
logger.info(
|
|
249
252
|
"Attempting to get agentic application token from agent_app_instance_id %s",
|
|
@@ -277,7 +280,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
277
280
|
"""
|
|
278
281
|
|
|
279
282
|
if not agent_app_instance_id:
|
|
280
|
-
raise ValueError(
|
|
283
|
+
raise ValueError(
|
|
284
|
+
str(authentication_errors.AgentApplicationInstanceIdRequired)
|
|
285
|
+
)
|
|
281
286
|
|
|
282
287
|
logger.info(
|
|
283
288
|
"Attempting to get agentic instance token from agent_app_instance_id %s",
|
|
@@ -293,7 +298,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
293
298
|
agent_app_instance_id,
|
|
294
299
|
)
|
|
295
300
|
raise Exception(
|
|
296
|
-
|
|
301
|
+
authentication_errors.FailedToAcquireAgenticInstanceToken.format(
|
|
302
|
+
agent_app_instance_id
|
|
303
|
+
)
|
|
297
304
|
)
|
|
298
305
|
|
|
299
306
|
authority = (
|
|
@@ -316,7 +323,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
316
323
|
agent_app_instance_id,
|
|
317
324
|
)
|
|
318
325
|
raise Exception(
|
|
319
|
-
|
|
326
|
+
authentication_errors.FailedToAcquireAgenticInstanceToken.format(
|
|
327
|
+
agent_app_instance_id
|
|
328
|
+
)
|
|
320
329
|
)
|
|
321
330
|
|
|
322
331
|
# future scenario where we don't know the blueprint id upfront
|
|
@@ -326,9 +335,20 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
326
335
|
logger.error(
|
|
327
336
|
"Failed to acquire agentic instance token, %s", agentic_instance_token
|
|
328
337
|
)
|
|
329
|
-
raise ValueError(
|
|
338
|
+
raise ValueError(
|
|
339
|
+
authentication_errors.FailedToAcquireToken.format(
|
|
340
|
+
str(agentic_instance_token)
|
|
341
|
+
)
|
|
342
|
+
)
|
|
330
343
|
|
|
331
|
-
logger.debug(
|
|
344
|
+
logger.debug(
|
|
345
|
+
"Agentic blueprint id: %s",
|
|
346
|
+
_DeferredString(
|
|
347
|
+
lambda: jwt.decode(token, options={"verify_signature": False}).get(
|
|
348
|
+
"xms_par_app_azp"
|
|
349
|
+
)
|
|
350
|
+
),
|
|
351
|
+
)
|
|
332
352
|
|
|
333
353
|
return agentic_instance_token["access_token"], agent_token_result
|
|
334
354
|
|
|
@@ -348,7 +368,7 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
348
368
|
"""
|
|
349
369
|
if not agent_app_instance_id or not agentic_user_id:
|
|
350
370
|
raise ValueError(
|
|
351
|
-
|
|
371
|
+
str(authentication_errors.AgentApplicationInstanceIdAndUserIdRequired)
|
|
352
372
|
)
|
|
353
373
|
|
|
354
374
|
logger.info(
|
|
@@ -367,7 +387,9 @@ class MsalAuth(AccessTokenProviderBase):
|
|
|
367
387
|
agentic_user_id,
|
|
368
388
|
)
|
|
369
389
|
raise Exception(
|
|
370
|
-
|
|
390
|
+
authentication_errors.FailedToAcquireInstanceOrAgentToken.format(
|
|
391
|
+
agent_app_instance_id, agentic_user_id
|
|
392
|
+
)
|
|
371
393
|
)
|
|
372
394
|
|
|
373
395
|
authority = (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: microsoft-agents-authentication-msal
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: A msal-based authentication library for Microsoft Agents
|
|
5
5
|
Author: Microsoft Corporation
|
|
6
6
|
License-Expression: MIT
|
|
@@ -15,7 +15,7 @@ Classifier: Operating System :: OS Independent
|
|
|
15
15
|
Requires-Python: >=3.10
|
|
16
16
|
Description-Content-Type: text/markdown
|
|
17
17
|
License-File: LICENSE
|
|
18
|
-
Requires-Dist: microsoft-agents-hosting-core==0.
|
|
18
|
+
Requires-Dist: microsoft-agents-hosting-core==0.6.0
|
|
19
19
|
Requires-Dist: msal>=1.31.1
|
|
20
20
|
Requires-Dist: requests>=2.32.3
|
|
21
21
|
Requires-Dist: cryptography>=44.0.0
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
microsoft_agents/authentication/msal/__init__.py,sha256=hjPpakL4zyqeCTEBOUCcHaRnSpG80q-L0csG5HMalYI,151
|
|
2
|
+
microsoft_agents/authentication/msal/msal_auth.py,sha256=iWXAFYYv0MoxK_mvuRq_cren8fJZWrBbW7hsSGmTDLQ,17057
|
|
3
|
+
microsoft_agents/authentication/msal/msal_connection_manager.py,sha256=v7o0ONzjId1G6Ta7IjHc1NtSeM3NWH4t7YilrwJzvYg,5713
|
|
4
|
+
microsoft_agents/authentication/msal/errors/__init__.py,sha256=9dbI_fGa0J4-qq6mTwdAIjaDADDFypenn4ZcsK-F4nE,449
|
|
5
|
+
microsoft_agents/authentication/msal/errors/error_resources.py,sha256=BIZNjhKLNZmyggblBkyQ3R2pGq3VkllEoni6QgBI4hw,1849
|
|
6
|
+
microsoft_agents_authentication_msal-0.6.0.dist-info/licenses/LICENSE,sha256=ws_MuBL-SCEBqPBFl9_FqZkaaydIJmxHrJG2parhU4M,1141
|
|
7
|
+
microsoft_agents_authentication_msal-0.6.0.dist-info/METADATA,sha256=rqi1LLuE9nuKYbpFnzwDQLsw62c9iP8TsWqOB35eouM,8363
|
|
8
|
+
microsoft_agents_authentication_msal-0.6.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
9
|
+
microsoft_agents_authentication_msal-0.6.0.dist-info/top_level.txt,sha256=lWKcT4v6fTA_NgsuHdNvuMjSrkiBMXohn64ApY7Xi8A,17
|
|
10
|
+
microsoft_agents_authentication_msal-0.6.0.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
microsoft_agents/authentication/msal/__init__.py,sha256=hjPpakL4zyqeCTEBOUCcHaRnSpG80q-L0csG5HMalYI,151
|
|
2
|
-
microsoft_agents/authentication/msal/msal_auth.py,sha256=MI5WNAUL7QyY6MOkstJIibTO0i2yR34FUwhiVRQtiaI,16720
|
|
3
|
-
microsoft_agents/authentication/msal/msal_connection_manager.py,sha256=v7o0ONzjId1G6Ta7IjHc1NtSeM3NWH4t7YilrwJzvYg,5713
|
|
4
|
-
microsoft_agents_authentication_msal-0.5.3.dist-info/licenses/LICENSE,sha256=ws_MuBL-SCEBqPBFl9_FqZkaaydIJmxHrJG2parhU4M,1141
|
|
5
|
-
microsoft_agents_authentication_msal-0.5.3.dist-info/METADATA,sha256=V32swLgZ4seO3dM8zoNd1eijDq5pA202m57yjnbL7Hg,8363
|
|
6
|
-
microsoft_agents_authentication_msal-0.5.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
-
microsoft_agents_authentication_msal-0.5.3.dist-info/top_level.txt,sha256=lWKcT4v6fTA_NgsuHdNvuMjSrkiBMXohn64ApY7Xi8A,17
|
|
8
|
-
microsoft_agents_authentication_msal-0.5.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|