Skip to content

Commit 8b2761f

Browse files
tracyboehrerTracy Boehrer
andauthored
Teams SSO and OAuth fixes (#2226) (#2228)
* Fixed Teams SSO & OAuth * Formatting --------- Co-authored-by: Tracy Boehrer <[email protected]>
1 parent 13ee2f2 commit 8b2761f

File tree

17 files changed

+110
-22
lines changed

17 files changed

+110
-22
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
aiohttp==3.10.11
1+
aiohttp
22
pyslack
33
botbuilder-core==4.17.0
44
slackclient

libraries/botbuilder-ai/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"azure-cognitiveservices-language-luis==0.2.0",
99
"botbuilder-schema==4.17.0",
1010
"botbuilder-core==4.17.0",
11-
"aiohttp==3.10.11",
11+
"aiohttp>=3.10,<4.0",
1212
]
1313

1414
TESTS_REQUIRES = ["aiounittest>=1.1.0"]

libraries/botbuilder-core/botbuilder/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from .user_state import UserState
4949
from .register_class_middleware import RegisterClassMiddleware
5050
from .adapter_extensions import AdapterExtensions
51+
from .serializer_helper import serializer_helper
5152

5253
__all__ = [
5354
"ActivityHandler",
@@ -100,5 +101,6 @@
100101
"TurnContext",
101102
"UserState",
102103
"UserTokenProvider",
104+
"serializer_helper",
103105
"__version__",
104106
]

libraries/botbuilder-core/botbuilder/core/activity_handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,19 @@ async def on_invoke_activity( # pylint: disable=unused-argument
478478
if (
479479
turn_context.activity.name
480480
== SignInConstants.verify_state_operation_name
481-
or turn_context.activity.name
482-
== SignInConstants.token_exchange_operation_name
483481
):
484482
await self.on_sign_in_invoke(turn_context)
485483
return self._create_invoke_response()
486484

485+
# This is for back-compat with previous versions of Python SDK. This method does not
486+
# exist in the C# SDK, and is not used in the Python SDK.
487+
if (
488+
turn_context.activity.name
489+
== SignInConstants.token_exchange_operation_name
490+
):
491+
await self.on_teams_signin_token_exchange(turn_context)
492+
return self._create_invoke_response()
493+
487494
if turn_context.activity.name == "adaptiveCard/action":
488495
invoke_value = self._get_adaptive_card_invoke_value(
489496
turn_context.activity

libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse:
5656
):
5757
return await self.on_teams_card_action_invoke(turn_context)
5858

59-
if (
60-
turn_context.activity.name
61-
== SignInConstants.token_exchange_operation_name
62-
):
63-
await self.on_teams_signin_token_exchange(turn_context)
64-
return self._create_invoke_response()
65-
6659
if turn_context.activity.name == "fileConsent/invoke":
6760
return await self.on_teams_file_consent(
6861
turn_context,
@@ -250,7 +243,9 @@ async def on_teams_signin_verify_state(self, turn_context: TurnContext):
250243
raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED)
251244

252245
async def on_teams_signin_token_exchange(self, turn_context: TurnContext):
253-
raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED)
246+
# This is for back-compat with previous versions of Python SDK. This method does not
247+
# exist in the C# SDK, and is not used in the Python SDK.
248+
return await self.on_teams_signin_verify_state(turn_context)
254249

255250
async def on_teams_file_consent(
256251
self,

libraries/botbuilder-core/botbuilder/core/teams/teams_sso_token_exchange_middleware.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
StoreItem,
2727
TurnContext,
2828
)
29+
from botframework.connector.auth.user_token_client import UserTokenClient
2930

3031

3132
class _TokenStoreItem(StoreItem):
@@ -147,17 +148,29 @@ async def _exchanged_token(self, turn_context: TurnContext) -> bool:
147148
token_exchange_response: TokenResponse = None
148149
aux_dict = {}
149150
if turn_context.activity.value:
150-
for prop in ["id", "connection_name", "token", "properties"]:
151+
for prop in ["id", "connectionName", "token", "properties"]:
151152
aux_dict[prop] = turn_context.activity.value.get(prop)
152153
token_exchange_request = TokenExchangeInvokeRequest(
153154
id=aux_dict["id"],
154-
connection_name=aux_dict["connection_name"],
155+
connection_name=aux_dict["connectionName"],
155156
token=aux_dict["token"],
156157
properties=aux_dict["properties"],
157158
)
158159
try:
159160
adapter = turn_context.adapter
160-
if isinstance(turn_context.adapter, ExtendedUserTokenProvider):
161+
162+
user_token_client: UserTokenClient = turn_context.turn_state.get(
163+
UserTokenClient.__name__, None
164+
)
165+
if user_token_client:
166+
# If the adapter has UserTokenClient, use it to exchange the token.
167+
token_exchange_response = await user_token_client.exchange_token(
168+
turn_context.activity.from_property.id,
169+
token_exchange_request.connection_name,
170+
turn_context.activity.channel_id,
171+
TokenExchangeRequest(token=token_exchange_request.token),
172+
)
173+
elif isinstance(turn_context.adapter, ExtendedUserTokenProvider):
161174
token_exchange_response = await adapter.exchange_token(
162175
turn_context,
163176
self._oauth_connection_name,

libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ async def _send_oauth_card(
341341
if sign_in_resource.token_exchange_resource
342342
else None
343343
)
344+
345+
json_token_ex_post = (
346+
sign_in_resource.token_post_resource.as_dict()
347+
if sign_in_resource.token_post_resource
348+
else None
349+
)
350+
344351
prompt.attachments.append(
345352
CardFactory.oauth_card(
346353
OAuthCard(
@@ -355,6 +362,7 @@ async def _send_oauth_card(
355362
)
356363
],
357364
token_exchange_resource=json_token_ex_resource,
365+
token_post_resource=json_token_ex_post,
358366
)
359367
)
360368
)

libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/cloud_adapter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
import json
5+
46
from typing import Awaitable, Callable, Optional
57

68
from aiohttp.web import (
@@ -17,6 +19,7 @@
1719
Bot,
1820
CloudAdapterBase,
1921
InvokeResponse,
22+
serializer_helper,
2023
TurnContext,
2124
)
2225
from botbuilder.core.streaming import (
@@ -102,7 +105,8 @@ async def process(
102105
# Write the response, serializing the InvokeResponse
103106
if invoke_response:
104107
return json_response(
105-
data=invoke_response.body, status=invoke_response.status
108+
data=serializer_helper(invoke_response.body),
109+
status=invoke_response.status,
106110
)
107111
return Response(status=201)
108112
else:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
msrest== 0.7.*
22
botframework-connector==4.17.0
33
botbuilder-schema==4.17.0
4-
aiohttp==3.10.11
4+
aiohttp==3.*.*

libraries/botbuilder-integration-aiohttp/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"botframework-connector==4.17.0",
1111
"botbuilder-core==4.17.0",
1212
"yarl>=1.8.1",
13-
"aiohttp==3.10.11",
13+
"aiohttp>=3.10,<4.0",
1414
]
1515

1616
root = os.path.abspath(os.path.dirname(__file__))

0 commit comments

Comments
 (0)