8000 WIP Bug fixing · microsoft/Agents-for-python@129e902 · GitHub
[go: up one dir, main page]

Skip to content

Commit 129e902

Browse files
committed
WIP Bug fixing
1 parent 9cd3c89 commit 129e902

File tree

11 files changed

+40
-27
lines changed

11 files changed

+40
-27
lines changed

libraries/Botbuilder/microsoft-agents-botbuilder/microsoft/agents/botbuilder/channel_adapter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ class ChannelAdapter(Protocol):
2020
OAUTH_SCOPE_KEY = "Microsoft.Agents.BotBuilder.ChannelAdapter.OAuthScope"
2121
INVOKE_RESPONSE_KEY = "ChannelAdapter.InvokeResponse"
2222

23-
middleware_set: MiddlewareSet = None
2423
on_turn_error: Callable[[TurnContext, Exception], Awaitable] = None
24+
25+
def __init__(self):
26+
super().__init__()
27+
self.middleware_set = MiddlewareSet()
2528

2629
@abstractmethod
2730
async def send_activities(

libraries/Botbuilder/microsoft-agents-botbuilder/microsoft/agents/botbuilder/channel_service_adapter.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class ChannelServiceAdapter(ChannelAdapter, Protocol):
3939
_BOT_CONNECTOR_CLIENT_KEY = "ConnectorClient"
4040
_INVOKE_RESPONSE_KEY = "BotFrameworkAdapter.InvokeResponse"
4141

42-
_channel_service_client_factory: ChannelServiceClientFactoryBase = None
43-
4442
def __init__(self, channel_service_client_factory: ChannelServiceClientFactoryBase):
4543
self._channel_service_client_factory = channel_service_client_factory
4644

libraries/Client/microsoft-agents-connector/microsoft/agents/connector/_connector_client_configuration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ def _configure(self, **kwargs: Any) -> None:
5252
self.retry_policy = kwargs.get("retry_policy") or policies.AsyncRetryPolicy(
5353
**kwargs
5454
)
55+
self.authentication_policy = kwargs.get("authentication_policy")
56+
"""
5557
if not self.credential_scopes and not self.authentication_policy:
5658
raise ValueError(
5759
"You must provide either credential_scopes or authentication_policy as kwargs"
5860
)
61+
"""
5962
if self.credential and not self.authentication_policy:
63+
scopes = self.credential_scopes or []
6064
self.authentication_policy = policies.AsyncBearerTokenCredentialPolicy(
61-
self.credential, *self.credential_scopes, **kwargs
65+
self.credential, *scopes, **kwargs
6266
)

libraries/Client/microsoft-agents-connector/microsoft/agents/connector/token/_user_token_client_configuration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ def _configure(self, **kwargs: Any) -> None:
5656
self.retry_policy = kwargs.get("retry_policy") or policies.AsyncRetryPolicy(
5757
**kwargs
5858
)
59+
self.authentication_policy = kwargs.get("authentication_policy")
60+
"""
5961
if not self.credential_scopes and not self.authentication_policy:
6062
raise ValueError(
6163
"You must provide either credential_scopes or authentication_policy as kwargs"
6264
)
65+
"""
6366
if self.credential and not self.authentication_policy:
67+
scopes = self.credential_scopes or []
6468
self.authentication_policy = policies.AsyncBearerTokenCredentialPolicy(
65-
self.credential, *self.credential_scopes, **kwargs
69+
self.credential, *scopes, **kwargs
6670
)

libraries/Client/microsoft-agents-connector/microsoft/agents/connector/token/user_token_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ def __init__(
7575
self._serialize = Serializer()
7676
self._deserialize = Deserializer()
7777
self._serialize.client_side_validation = False
78-
self.bot_sign_in = BotSignInOperations(
78+
self._bot_sign_in = BotSignInOperations(
7979
self._client, self._config, self._serialize, self._deserialize
8080
)
81-
self.user_token = UserTokenOperations(
81+
self._user_token = UserTokenOperations(
8282
self._client, self._config, self._serialize, self._deserialize
8383
)
8484
self.token_internals = TokenInternalsOperations(
@@ -87,11 +87,11 @@ def __init__(
8787

8888
@property
8989
def bot_sign_in(self) -> BotSignInBase:
90-
return self.bot_sign_in
90+
return self._bot_sign_in
9191

9292
@property
9393
def user_token(self) -> UserTokenBase:
94-
return self.user_token
94+
return self._user_token
9595

9696
def send_request(
9797
self, request: HttpRequest, *, stream: bool = False, **kwargs: Any

libraries/Core/microsoft-agents-authentication/microsoft/agents/authentication/jwt_token_validator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
from jwt import PyJWKClient, PyJWK, decode, get_unverified_header
44

55
from .bot_auth_configuration import BotAuthConfiguration
6+
from .claims_identity import ClaimsIdentity
67

78

89
class JwtTokenValidator:
910
def __init__(self, configuration: BotAuthConfiguration):
1011
self.configuration = configuration
1112

12-
def validate_token(self, token: str):
13-
key = self._get_public_key_or_secret()
13+
def validate_token(self, token: str) -> ClaimsIdentity:
14+
key = self._get_public_key_or_secret(token)
1415
decoded_token = jwt.decode(
15-
token, key=key, algorithms=["RS256"], options={"verify_audience": False}
16+
token, key=key, algorithms=["RS256"], options={"verify_aud": False}
1617
)
1718
if decoded_token["aud"] != self.configuration.CLIENT_ID:
1819
raise ValueError("Invalid audience.")
1920

2021
# This probably should return a ClaimsIdentity
21-
return decoded_token
22+
return ClaimsIdentity(decoded_token, True)
2223

2324
def _get_public_key_or_secret(self, token: str) -> PyJWK:
2425
header = get_unverified_header(token)

libraries/Core/microsoft-agents-core/microsoft/agents/core/models/activity.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,21 @@ def get_conversation_reference(self):
504504
505505
:returns: A conversation reference for the conversation that contains this activity.
506506
"""
507-
return ConversationReference(
508-
activity_id=(
507+
conversation_reference = {
508+
"activityId": (
509509
self.id
510510
if self.type != ActivityTypes.conversation_update
511511
or self.channel_id not in ["directline", "webchat"]
512512
else None
513513
),
514-
user=copy(self.from_property),
515-
bot=copy(self.recipient),
516-
conversation=copy(self.conversation),
517-
channel_id=self.channel_id,
518-
locale=self.locale,
519-
service_url=self.service_url,
520-
)
514+
"user": self.from_property.model_dump(by_alias=True, exclude_unset=True),
515+
"bot": self.recipient.model_dump(by_alias=True, exclude_unset=True),
516+
"conversation": self.conversation.model_dump(by_alias=True, exclude_unset=True),
517+
"channelId": self.channel_id,
518+
"locale": self.locale,
519+
"serviceUrl": self.service_url,
520+
}
521+
return ConversationReference.model_validate(conversation_reference)
521522

522523
def get_mentions(self) -> list[Mention]:
523524
"""

libraries/Hosting/microsoft-agents-hosting-aiohttp/microsoft/agents/hosting/aiohttp/cloud_adapter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async def on_turn_error(context: TurnContext, error: Exception):
5353
)
5454

5555
self.on_turn_error = on_turn_error
56+
self._channel_service_client_factory = channel_service_client_factory
5657

5758
super().__init__(channel_service_client_factory)
5859

@@ -69,7 +70,7 @@ async def process(self, request: Request, bot: Bot) -> Optional[Response]:
6970
else:
7071
raise HTTPUnsupportedMediaType()
7172

72-
activity: Activity = Activity.model_validate_json(body)
73+
activity: Activity = Activity.model_validate(body)
7374
# TODO: Add the ability to pass in the ClaimsIdentity
7475
claims_identity: ClaimsIdentity = request.get("claims_identity")
7576

libraries/Hosting/microsoft-agents-hosting-aiohttp/microsoft/agents/hosting/aiohttp/jwt_authorization_middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ async def jwt_authorization_middleware(request: Request, handler):
1212
# Extract the token from the Authorization header
1313
token = auth_header.split(" ")[1]
1414
try:
15-
user = token_validator.validate_token(token)
16-
request["user"] = user
15+
claims = token_validator.validate_token(token)
16+
request["claims_identity"] = claims
1717
except ValueError as e:
1818
return json_response({"error": str(e)}, status=401)
1919
else:
2020
if (not auth_config.CLIENT_ID) and (request.app["env"] == "DEV"):
21-
# In development, if the client id is not set, we allow all requests
21+
# TODO: Define anonymous strategy
2222
request["user"] = {"name": "anonymous"}
2323
else:
2424
return json_response(

test_samples/echo_bot/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def messages(req: Request) -> Response:
4646

4747
APP = web.Application(middlewares=[jwt_authorization_middleware])
4848
APP.router.add_post("/api/messages", messages)
49+
APP["bot_configuration"] = CONFIG
4950

5051
if __name__ == "__main__":
5152
try:

test_samples/echo_bot/echo_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class EchoBot(ActivityHandler):
66
async def on_members_added_activity(
7-
self, members_added: [ChannelAccount], turn_context: TurnContext
7+
self, members_added: list[ChannelAccount], turn_context: TurnContext
88
):
99
for member in members_added:
1010
if member.id != turn_context.activity.recipient.< 38A2 span class=pl-c1>id:

0 commit comments

Comments
 (0)
0