8000 Axsuarez/sso protocol (#793) · LucaSavio/botbuilder-python@4dd0966 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4dd0966

Browse files
Axsuarez/sso protocol (microsoft#793)
* Initial changes for SSO, schema done * fixes for schema * black: fixes for schema * Regen token-api * schema cleanup * Activity Handler changes pending * SSO code complete testing pending. * black: Merge w/master and auth changes * Adding token exchange resource in SignInCard * Fix on get_sign_in_resource * Adding credentials for token_api_client * Adding temporal fix by duplicating TokenExchangeResource in schema * Serialization work-around * sso: Small fixes on BFAdapter * sso: small fixes in ActivityHandler * exchange token noe receives creds * black: sso fixes * sso: updated TestAdapter * Added latest js fixes and swagat samples for testing * removed unused file in sso sample * fixed error introduced in merge with master * Activity Handler cleanup * pylint: Activity Handler cleanup * Removed StatusCodes Co-authored-by: tracyboehrer <tracyboehrer@users.noreply.github.com>
1 parent 10fdfb3 commit 4dd0966

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2967
-600
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .card_factory import CardFactory
2020
from .channel_service_handler import BotActionNotImplementedError, ChannelServiceHandler
2121
from .conversation_state import ConversationState
22+
from .extended_user_token_provider import ExtendedUserTokenProvider
2223
from .intent_score import IntentScore
2324
from .invoke_response import InvokeResponse
2425
from .bot_framework_http_client import BotFrameworkHttpClient
@@ -59,6 +60,7 @@
5960
"ChannelServiceHandler",
6061
"ConversationState",
6162
"conversation_reference_extension",
63+
"ExtendedUserTokenProvider",
6264
"IntentScore",
6365
"InvokeResponse",
6466
"BotFrameworkHttpClient",

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
from typing import List
4-
5-
from botbuilder.schema import ActivityTypes, ChannelAccount, MessageReaction
3+
from http import HTTPStatus
4+
from typing import List, Union
5+
6+
from botbuilder.schema import (
7+
Activity,
8+
ActivityTypes,
9+
ChannelAccount,
10+
MessageReaction,
11+
SignInConstants,
12+
)
13+
from .serializer_helper import serializer_helper
14+
from .bot_framework_adapter import BotFrameworkAdapter
15+
from .invoke_response import InvokeResponse
616
from .turn_context import TurnContext
717

818

@@ -58,6 +68,16 @@ async def on_turn(self, turn_context: TurnContext):
5868
await self.on_message_reaction_activity(turn_context)
5969
elif turn_context.activity.type == ActivityTypes.event:
6070
await self.on_event_activity(turn_context)
71+
elif turn_context.activity.type == ActivityTypes.invoke:
72+
invoke_response = await self.on_invoke_activity(turn_context)
73+
74+
# If OnInvokeActivityAsync has already sent an InvokeResponse, do not send another one.
75+
if invoke_response and not turn_context.turn_state.get(
76+
BotFrameworkAdapter._INVOKE_RESPONSE_KEY # pylint: disable=protected-access
77+
):
78+
await turn_context.send_activity(
79+
Activity(value=invoke_response, type=ActivityTypes.invoke_response)
80+
)
6181
elif turn_context.activity.type == ActivityTypes.end_of_conversation:
6282
await self.on_end_of_conversation_activity(turn_context)
6383
else:
@@ -269,7 +289,7 @@ async def on_event_activity(self, turn_context: TurnContext):
269289
The meaning of an event activity is defined by the event activity name property, which is meaningful within
270290
the scope of a channel.
271291
"""
272-
if turn_context.activity.name == "tokens/response":
292+
if turn_context.activity.name == SignInConstants.token_response_event_name:
273293
return await self.on_token_response_event(turn_context)
274294

275295
return await self.on_event(turn_context)
@@ -344,3 +364,58 @@ async def on_unrecognized_activity_type( # pylint: disable=unused-argument
344364
conversation update, message reaction, or event activity, it calls this method.
345365
"""
346366
return
367+
368+
async def on_invoke_activity( # pylint: disable=unused-argument
369+
self, turn_context: TurnContext
370+
) -> Union[InvokeResponse, None]:
371+
"""
372+
Registers an activity event handler for the _invoke_ event, emitted for every incoming event activity.
373+
374+
:param turn_context: The context object for this turn
375+
:type turn_context: :class:`botbuilder.core.TurnContext`
376+
377+
:returns: A task that represents the work queued to execute
378+
"""
379+
try:
380+
if (
381+
turn_context.activity.name
382+
== SignInConstants.verify_state_operation_name
383+
or turn_context.activity.name
384+
== SignInConstants.token_exchange_operation_name
385+
):
386+
await self.on_sign_in_invoke(turn_context)
387+
return self._create_invoke_response()
388+
389+
raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED)
390+
except _InvokeResponseException as invoke_exception:
391+
return invoke_exception.create_invoke_response()
392+
393+
async def on_sign_in_invoke( # pylint: disable=unused-argument
394+
self, turn_context: TurnContext
395+
):
396+
"""
397+
Invoked when a signin/verifyState or signin/tokenExchange event is received when the base behavior of
398+
on_invoke_activity(TurnContext{InvokeActivity}) is used.
399+
If using an OAuthPrompt, override this method to forward this Activity"/ to the current dialog.
400+
By default, this method does nothing.
401+
402+
:param turn_context: The context object for this turn
403+
:type turn_context: :class:`botbuilder.core.TurnContext`
404+
405+
:returns: A task that represents the work queued to execute
406+
"""
407+
raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED)
408+
409+
@staticmethod
410+
def _create_invoke_response(body: object = None) -> InvokeResponse:
411+
return InvokeResponse(status=int(HTTPStatus.OK), body=serializer_helper(body))
412+
413+
414+
class _InvokeResponseException(Exception):
415+
def __init__(self, status_code: HTTPStatus, body: object = None):
416+
super(_InvokeResponseException, self).__init__()
417+
self._status_code = status_code
418+
self._body = body
419+
420+
def create_invoke_response(self) -> InvokeResponse:
421+
return InvokeResponse(status=int(self._status_code), body=self._body)

0 commit comments

Comments
 (0)
0