10000 Updates to sso sample (#851) · LucaSavio/botbuilder-python@faf4eed · GitHub
[go: up one dir, main page]

Skip to content

Commit faf4eed

Browse files
author
Eric Dahlvang
authored
Updates to sso sample (microsoft#851)
1 parent 4dd0966 commit faf4eed

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

samples/experimental/sso/child/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from aiohttp import web
1010
from aiohttp.web import Request, Response
11+
from aiohttp.web_response import json_response
1112
from botbuilder.core import (
1213
BotFrameworkAdapterSettings,
1314
ConversationState,
@@ -83,7 +84,9 @@ async def messages(req: Request) -> Response:
8384
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""
8485

8586
try:
86-
await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
87+
response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
88+
if response:
89+
return json_response(data=response.body, status=response.status)
8790
return Response(status=201)
8891
except Exception as exception:
8992
raise exception

samples/experimental/sso/child/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class DefaultConfig:
1111
""" Bot Configuration """
1212

13-
PORT = 3978
13+
PORT = 3979
1414
APP_ID = os.environ.get("MicrosoftAppId", "")
1515
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
1616
CONNECTION_NAME = ""

samples/experimental/sso/parent/bots/parent_bot.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
ConversationState,
1313
UserState,
1414
MessageFactory,
15-
TurnContext
15+
TurnContext,
1616
)
1717
from botbuilder.schema import (
1818
Activity,
@@ -21,9 +21,12 @@
2121
DeliveryModes,
2222
ChannelAccount,
2323
OAuthCard,
24-
TokenExchangeInvokeRequest
24+
TokenExchangeInvokeRequest,
25+
)
26+
from botframework.connector.token_api.models import (
27+
TokenExchangeResource,
28+
TokenExchangeRequest,
2529
)
26-
from botframework.connector.token_api.models import TokenExchangeResource, TokenExchangeRequest
2730

2831
from config import DefaultConfig
2932
from helpers.dialog_helper import DialogHelper
@@ -44,7 +47,7 @@ def __init__(
4447
self._user_state = user_state
4548
self._dialog = dialog
4649
self._from_bot_id = config.APP_ID
47-
self._to_bot_id = config.SKILL_APP_ID
50+
self._to_bot_id = config.SKILL_MICROSOFT_APP_ID
4851
self._connection_name = config.CONNECTION_NAME
4952

5053
async def on_turn(self, turn_context: TurnContext):
@@ -57,7 +60,10 @@ async def on_message_activity(self, turn_context: TurnContext):
5760
# for signin, just use an oauth prompt to get the exchangeable token
5861
# also ensure that the channelId is not emulator
5962
if turn_context.activity.type != "emulator":
60-
if turn_context.activity.text == "login" or turn_context.activity.text.isdigit():
63+
if (
64+
turn_context.activity.text == "login"
65+
or turn_context.activity.text.isdigit()
66+
):
6167
await self._conversation_state.load(turn_context, True)
6268
await self._user_state.load(turn_context, Tru 57AE e)
6369
await DialogHelper.run_dialog(
@@ -68,31 +74,35 @@ async def on_message_activity(self, turn_context: TurnContext):
6874
elif turn_context.activity.text == "logout":
6975
bot_adapter = turn_context.adapter
7076
await bot_adapter.sign_out_user(turn_context, self._connection_name)
71-
await turn_context.send_activity(MessageFactory.text("You have been signed out."))
77+
await turn_context.send_activity(
78+
MessageFactory.text("You have been signed out.")
79+
)
7280
elif turn_context.activity.text in ("skill login", "skill logout"):
7381
# incoming activity needs to be cloned for buffered replies
7482
clone_activity = MessageFactory.text(turn_context.activity.text)
7583

7684
TurnContext.apply_conversation_reference(
7785
clone_activity,
7886
TurnContext.get_conversation_reference(turn_context.activity),
79-
True
87+
True,
8088
)
8189

8290
clone_activity.delivery_mode = DeliveryModes.expect_replies
8391

84-
response_1 = await self._client.post_activity(
92+
activities = await self._client.post_buffered_activity(
8593
self._from_bot_id,
8694
self._to_bot_id,
87-
"http://localhost:2303/api/messages",
95+
"http://localhost:3979/api/messages",
8896
"http://tempuri.org/whatever",
8997
turn_context.activity.conversation.id,
9098
clone_activity,
9199
)
92100

93-
if response_1.status == int(HTTPStatus.OK):
94-
if not await self._intercept_oauth_cards(response_1.body, turn_context):
95-
await turn_context.send_activities(response_1.body)
101+
if activities:
102+
if not await self._intercept_oauth_cards(
103+
activities, turn_context
104+
):
105+
await turn_context.send_activities(activities)
96106

97107
return
98108

@@ -102,22 +112,20 @@ async def on_message_activity(self, turn_context: TurnContext):
102112
TurnContext.apply_conversation_reference(
103113
activity,
104114
TurnContext.get_conversation_reference(turn_context.activity),
105-
True
115+
True,
106116
)
107117
activity.delivery_mode = DeliveryModes.expect_replies
108118

109-
response = await self._client.post_activity(
119+
activities = await self._client.post_buffered_activity(
110120
self._from_bot_id,
111121
self._to_bot_id,
112-
"http://localhost:2303/api/messages",
122+
"http://localhost:3979/api/messages",
113123
"http://tempuri.org/whatever",
114124
str(uuid4()),
115-
activity
125+
activity,
116126
)
117127

118-
if response.status == int(HTTPStatus.OK):
119-
await turn_context.send_activities(response.body)
120-
128+
await turn_context.send_activities(activities)
121129
await turn_context.send_activity(MessageFactory.text("parent: after child"))
122130

123131
async def on_members_added_activity(
@@ -130,36 +138,39 @@ async def on_members_added_activity(
130138
)
131139

132140
async def _intercept_oauth_cards(
133-
self,
134-
activities: List[Activity],
135-
turn_context: TurnContext,
141+
self, activities: List[Activity], turn_context: TurnContext,
136142
) -> bool:
137143
if not activities:
138144
return False
139145
activity = activities[0]
140146

141147
if activity.attachments:
142-
for attachment in filter(lambda att: att.content_type == CardFactory.content_types.oauth_card,
143-
activity.attachments):
148+
for attachment in filter(
149+
lambda att: att.content_type == CardFactory.content_types.oauth_card,
150+
activity.attachments,
151+
):
144152
oauth_card: OAuthCard = OAuthCard().from_dict(attachment.content)
145153
oauth_card.token_exchange_resource: TokenExchangeResource = TokenExchangeResource().from_dict(
146-
oauth_card.token_exchange_resource)
154+
oauth_card.token_exchange_resource
155+
)
147156
if oauth_card.token_exchange_resource:
148157
token_exchange_provider: BotFrameworkAdapter = turn_context.adapter
149158

150159
result = await token_exchange_provider.exchange_token(
151160
turn_context,
152161
self._connection_name,
153162
turn_context.activity.from_property.id,
154-
TokenExchangeRequest(uri=oauth_card.token_exchange_resource.uri)
163+
TokenExchangeRequest(
164+
uri=oauth_card.token_exchange_resource.uri
165+
),
155166
)
156167

157168
if result.token:
158169
return await self._send_token_exchange_invoke_to_skill(
159170
turn_context,
160171
activity,
161172
oauth_card.token_exchange_resource.id,
162-
result.token
173+
result.token,
163174
)
164175
return False
165176

@@ -168,33 +179,32 @@ async def _send_token_exchange_invoke_to_skill(
168179
turn_context: TurnContext,
169180
incoming_activity: Activity,
170181
identifier: str,
171-
token: str
182+
token: str,
172183
) -> bool:
173184
activity = self._create_reply(incoming_activity)
174185
activity.type = ActivityTypes.invoke
175186
activity.name = "signin/tokenExchange"
176-
activity.value = TokenExchangeInvokeRequest(
177-
id=identifier,
178-
token=token,
179-
)
187+
activity.value = TokenExchangeInvokeRequest(id=identifier, token=token,)
180188

181189
# route the activity to the skill
182190
response = await self._client.post_activity(
183191
self._from_bot_id,
184192
self._to_bot_id,
185-
"http://localhost:2303/api/messages",
193+
"http://localhost:3979/api/messages",
186194
"http://tempuri.org/whatever",
187195
incoming_activity.conversation.id,
188-
activity
196+
activity,
189197
)
190198

191199
# Check response status: true if success, false if failure
192200
is_success = int(HTTPStatus.OK) <= response.status <= 299
193-
message = "Skill token exchange successful" if is_success else "Skill token exchange failed"
201+
message = (
202+
"Skill token exchange successful"
203+
if is_success
204+
else "Skill token exchange failed"
205+
)
194206

195-
await turn_context.send_activity(MessageFactory.text(
196-
message
197-
))
207+
await turn_context.send_activity(MessageFactory.text(message))
198208

199209
return is_success
200210

0 commit comments

Comments
 (0)
0