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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import re
from datetime import datetime, timedelta
from http import HTTPStatus
from typing import Union, Awaitable, Callable

from botframework.connector import Channels
from botframework.connector.auth import (
    ClaimsIdentity,
    SkillValidation,
    JwtTokenValidation,
)
from botbuilder.core import (
    CardFactory,
    MessageFactory,
    InvokeResponse,
    TurnContext,
    BotAdapter,
)
from botbuilder.core.bot_framework_adapter import TokenExchangeRequest
from botbuilder.dialogs import Dialog, DialogContext, DialogTurnResult
from botbuilder.schema import (
    Activity,
    ActivityTypes,
    ActionTypes,
    CardAction,
    InputHints,
    SigninCard,
    SignInConstants,
    OAuthCard,
    TokenResponse,
    TokenExchangeInvokeRequest,
    TokenExchangeInvokeResponse,
)

from .prompt_options import PromptOptions
from .oauth_prompt_settings import OAuthPromptSettings
from .prompt_validator_context import PromptValidatorContext
from .prompt_recognizer_result import PromptRecognizerResult

from .._user_token_access import _UserTokenAccess


class CallerInfo:
    def __init__(self, caller_service_url: str = None, scope: str = None):
        self.caller_service_url = caller_service_url
        self.scope = scope


class OAuthPrompt(Dialog):
    PERSISTED_OPTIONS = "options"
    PERSISTED_STATE = "state"
    PERSISTED_EXPIRES = "expires"
    PERSISTED_CALLER = "caller"

    """
    Creates a new prompt that asks the user to sign in, using the Bot Framework Single Sign On (SSO) service.

    .. remarks::
        The prompt will attempt to retrieve the users current token and if the user isn't signed in, it
        will send them an `OAuthCard` containing a button they can press to sign in. Depending on the channel,
        the user will be sent through one of two possible sign-in flows:
        - The automatic sign-in flow where once the user signs in, the SSO service will forward
        the bot the users access token using either an `event` or `invoke` activity.
        - The "magic code" flow where once the user signs in, they will be prompted by the SSO service
        to send the bot a six digit code confirming their identity. This code will be sent as a
        standard `message` activity.
        Both flows are automatically supported by the `OAuthPrompt` and they only thing you need to be careful of
        is that you don't block the `event` and `invoke` activities that the prompt might be waiting on.

        You should avoid persisting the access token with your bots other state. The Bot Frameworks SSO service
        will securely store the token on your behalf. If you store it in your bots state,
        it could expire or be revoked in between turns.
        When calling the prompt from within a waterfall step, you should use the token within the step
        following the prompt and then let the token go out of scope at the end of your function.

        When used with your bots :class:`DialogSet`, you can simply add a new instance of the prompt as a named
        dialog using :meth`DialogSet.add()`.
        You can then start the prompt from a waterfall step using either :meth:`DialogContext.begin()` or
        :meth:`DialogContext.prompt()`.
        The user will be prompted to sign in as needed and their access token will be passed as an argument to
        the callers next waterfall step.
    """

    def __init__(
        self,
        dialog_id: str,
        settings: OAuthPromptSettings,
        validator: Callable[[PromptValidatorContext], Awaitable[bool]] = None,
    ):
        """
        Creates a new instance of the :class:`OAuthPrompt` class.

        :param dialog_id: The Id to assign to this prompt.
        :type dialog_id: str
        :param settings: Additional authentication settings to use with this instance of the prompt
        :type settings: :class:`OAuthPromptSettings`
        :param validator: Optional, contains additional, custom validation for this prompt
        :type validator: :class:`PromptValidatorContext`

        .. remarks::
            The value of :param dialogId: must be unique within the :class:`DialogSet`or :class:`ComponentDialog`
            to which the prompt is added.
        """
        super().__init__(dialog_id)
        self._validator = validator

        if not settings:
            raise TypeError(
                "OAuthPrompt.__init__(): OAuthPrompt requires OAuthPromptSettings."
            )

        self._settings = settings
        self._validator = validator

    async def begin_dialog(
        self, dialog_context: DialogContext, options: PromptOptions = None
    ) -> DialogTurnResult:
        """
        Starts an authentication prompt dialog. Called when an authentication prompt dialog is pushed onto the
        dialog stack and is being activated.

        :param dialog_context: The dialog context for the current turn of the conversation
        :type dialog_context:  :class:`DialogContext`
        :param options: Optional, additional information to pass to the prompt being started
        :type options: :class:`PromptOptions`

        :return: Dialog turn result
        :rtype: :class`:`DialogTurnResult`

        .. remarks::

            If the task is successful, the result indicates whether the prompt is still active after the turn
            has been processed.
        """
        if dialog_context is None:
            raise TypeError(
                f"OAuthPrompt.begin_dialog(): Expected DialogContext but got NoneType instead"
            )

        options = options or PromptOptions()

        # Ensure prompts have input hint set
        if options.prompt and not options.prompt.input_hint:
            options.prompt.input_hint = InputHints.accepting_input

        if options.retry_prompt and not options.retry_prompt.input_hint:
            options.retry_prompt.input_hint = InputHints.accepting_input

        # Initialize prompt state
        timeout = (
            self._settings.timeout
            if isinstance(self._settings.timeout, int)
            else 900000
        )
        state = dialog_context.active_dialog.state
        state[OAuthPrompt.PERSISTED_STATE] = {}
        state[OAuthPrompt.PERSISTED_OPTIONS] = options
        state[OAuthPrompt.PERSISTED_EXPIRES] = datetime.now() + timedelta(
            seconds=timeout / 1000
        )
        state[OAuthPrompt.PERSISTED_CALLER] = OAuthPrompt.__create_caller_info(
            dialog_context.context
        )

        output = await _UserTokenAccess.get_user_token(
            dialog_context.context, self._settings, None
        )

        if output is not None:
            # Return token
            return await dialog_context.end_dialog(output)

        await self._send_oauth_card(dialog_context.context, options.prompt)
        return Dialog.end_of_turn

    async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResult:
        """
        Continues a dialog. Called when a prompt dialog is the active dialog and the user replied with a new activity.

        :param dialog_context: The dialog context for the current turn of the conversation
        :type dialog_context:  :class:`DialogContext`

        :return: Dialog turn result
        :rtype: :class:`DialogTurnResult`

        .. remarks::
            If the task is successful, the result indicates whether the dialog is still
            active after the turn has been processed by the dialog.
            The prompt generally continues to receive the user's replies until it accepts the
            user's reply as valid input for the prompt.
        """
        # Check for timeout
        state = dialog_context.active_dialog.state
        is_message = dialog_context.context.activity.type == ActivityTypes.message
        is_timeout_activity_type = (
            is_message
            or OAuthPrompt._is_token_response_event(dialog_context.context)
            or OAuthPrompt._is_teams_verification_invoke(dialog_context.context)
            or OAuthPrompt._is_token_exchange_request_invoke(dialog_context.context)
        )

        has_timed_out = is_timeout_activity_type and (
            datetime.now() > state[OAuthPrompt.PERSISTED_EXPIRES]
        )

        if has_timed_out:
            return await dialog_context.end_dialog(None)

        if state["state"].get("attemptCount") is None:
            state["state"]["attemptCount"] = 1
        else:
            state["state"]["attemptCount"] += 1

        # Recognize token
        recognized = await self._recognize_token(dialog_context)

        # Validate the return value
        is_valid = False
        if self._validator is not None:
            is_valid = await self._validator(
                PromptValidatorContext(
                    dialog_context.context,
                    recognized,
                    state[OAuthPrompt.PERSISTED_STATE],
                    state[OAuthPrompt.PERSISTED_OPTIONS],
                )
            )
        elif recognized.succeeded:
            is_valid = True

        # Return recognized value or re-prompt
        if is_valid:
            return await dialog_context.end_dialog(recognized.value)
        if is_message and self._settings.end_on_invalid_message:
            # If EndOnInvalidMessage is set, complete the prompt with no result.
            return await dialog_context.end_dialog(None)

        # Send retry prompt
        if (
            not dialog_context.context.responded
            and is_message
            and state[OAuthPrompt.PERSISTED_OPTIONS].retry_prompt is not None
        ):
            await dialog_context.context.send_activity(
                state[OAuthPrompt.PERSISTED_OPTIONS].retry_prompt
            )

        return Dialog.end_of_turn

    async def get_user_token(
        self, context: TurnContext, code: str = None
    ) -> TokenResponse:
        """
        Gets the user's tokeN.

        :param context: Context for the current turn of conversation with the user
        :type context:  :class:`TurnContext`
        :param code: (Optional) Optional user entered code to validate.
        :type code: str

        :return: A response that includes the user's token
        :rtype: :class:`TokenResponse`

        .. remarks::
            If the task is successful and the user already has a token or the user successfully signs in,
            the result contains the user's token.
        """
        return await _UserTokenAccess.get_user_token(context, self._settings, code)

    async def sign_out_user(self, context: TurnContext):
        """
        Signs out the user

        :param context: Context for the current turn of conversation with the user
        :type context:  :class:`TurnContext`
        :return: A task representing the work queued to execute

        .. remarks::
            If the task is successful and the user already has a token or the user successfully signs in,
            the result contains the user's token.
        """
        return await _UserTokenAccess.sign_out_user(context, self._settings)

    @staticmethod
    def __create_caller_info(context: TurnContext) -> CallerInfo:
        bot_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY)
        if bot_identity and SkillValidation.is_skill_claim(bot_identity.claims):
            return CallerInfo(
                caller_service_url=context.activity.service_url,
                scope=JwtTokenValidation.get_app_id_from_claims(bot_identity.claims),
            )

        return None

    async def _send_oauth_card(
        self, context: TurnContext, prompt: Union[Activity, str] = None
    ):
        if not isinstance(prompt, Activity):
            prompt = MessageFactory.text(prompt or "", None, InputHints.accepting_input)
        else:
            prompt.input_hint = prompt.input_hint or InputHints.accepting_input

        prompt.attachments = prompt.attachments or []

        if OAuthPrompt._channel_suppports_oauth_card(context.activity.channel_id):
            if not any(
                att.content_type == CardFactory.content_types.oauth_card
                for att in prompt.attachments
            ):
                card_action_type = ActionTypes.signin
                sign_in_resource = await _UserTokenAccess.get_sign_in_resource(
                    context, self._settings
                )
                link = sign_in_resource.sign_in_link
                bot_identity: ClaimsIdentity = context.turn_state.get(
                    BotAdapter.BOT_IDENTITY_KEY
                )

                # use the SignInLink when in speech channel or bot is a skill or
                # an extra OAuthAppCredentials is being passed in
                if (
                    (
                        bot_identity
                        and SkillValidation.is_skill_claim(bot_identity.claims)
                    )
                    or not context.activity.service_url.startswith("http")
                    or self._settings.oath_app_credentials
                ):
                    if context.activity.channel_id == Channels.emulator:
                        card_action_type = ActionTypes.open_url
                elif not OAuthPrompt._channel_requires_sign_in_link(
                    context.activity.channel_id
                ):
                    link = None

                json_token_ex_resource = (
                    sign_in_resource.token_exchange_resource.as_dict()
                    if sign_in_resource.token_exchange_resource
                    else None
                )
                prompt.attachments.append(
                    CardFactory.oauth_card(
                        OAuthCard(
                            text=self._settings.text,
                            connection_name=self._settings.connection_name,
                            buttons=[
                                CardAction(
                                    title=self._settings.title,
                                    text=self._settings.text,
                                    type=card_action_type,
                                    value=link,
                                )
                            ],
                            token_exchange_resource=json_token_ex_resource,
                        )
                    )
                )
        else:
            if not any(
                att.content_type == CardFactory.content_types.signin_card
                for att in prompt.attachments
            ):
                if not hasattr(context.adapter, "get_oauth_sign_in_link"):
                    raise Exception(
                        "OAuthPrompt._send_oauth_card(): get_oauth_sign_in_link() not supported by the current adapter"
                    )

                link = await context.adapter.get_oauth_sign_in_link(
                    context,
                    self._settings.connection_name,
                    None,
                    self._settings.oath_app_credentials,
                )
                prompt.attachments.append(
                    CardFactory.signin_card(
                        SigninCard(
                            text=self._settings.text,
                            buttons=[
                                CardAction(
                                    title=self._settings.title,
                                    value=link,
                                    type=ActionTypes.signin,
                                )
                            ],
                        )
                    )
                )

        # Send prompt
        await context.send_activity(prompt)

    async def _recognize_token(
        self, dialog_context: DialogContext
    ) -> PromptRecognizerResult:
        context = dialog_context.context
        token = None
        if OAuthPrompt._is_token_response_event(context):
            token = context.activity.value

            # fixup the turnContext's state context if this was received from a skill host caller
            state: CallerInfo = dialog_context.active_dialog.state[
                OAuthPrompt.PERSISTED_CALLER
            ]
            if state:
                # set the ServiceUrl to the skill host's Url
                dialog_context.context.activity.service_url = state.caller_service_url
                claims_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY)
                connector_client = await _UserTokenAccess.create_connector_client(
                    context,
                    dialog_context.context.activity.service_url,
                    claims_identity,
                    state.scope,
                )

                context.turn_state[BotAdapter.BOT_CONNECTOR_CLIENT_KEY] = (
                    connector_client
                )

        elif OAuthPrompt._is_teams_verification_invoke(context):
            code = context.activity.value["state"]
            try:
                token = await _UserTokenAccess.get_user_token(
                    context, self._settings, code
                )
                if token is not None:
                    await context.send_activity(
                        Activity(
                            type="invokeResponse",
                            value=InvokeResponse(status=HTTPStatus.OK),
                        )
                    )
                else:
                    await context.send_activity(
                        Activity(
                            type="invokeResponse",
                            value=InvokeResponse(status=HTTPStatus.NOT_FOUND),
                        )
                    )
            except Exception:
                await context.send_activity(
                    Activity(
                        type="invokeResponse",
                        value=InvokeResponse(status=HTTPStatus.INTERNAL_SERVER_ERROR),
                    )
                )
        elif self._is_token_exchange_request_invoke(context):
            if isinstance(context.activity.value, dict):
                context.activity.value = TokenExchangeInvokeRequest().from_dict(
                    context.activity.value
                )

            if not (
                context.activity.value
                and self._is_token_exchange_request(context.activity.value)
            ):
                # Received activity is not a token exchange request.
                await context.send_activity(
                    self._get_token_exchange_invoke_response(
                        int(HTTPStatus.BAD_REQUEST),
                        "The bot received an InvokeActivity that is missing a TokenExchangeInvokeRequest value."
                        " This is required to be sent with the InvokeActivity.",
                    )
                )
            elif (
                context.activity.value.connection_name != self._settings.connection_name
            ):
                # Connection name on activity does not match that of setting.
                await context.send_activity(
                    self._get_token_exchange_invoke_response(
                        int(HTTPStatus.BAD_REQUEST),
                        "The bot received an InvokeActivity with a TokenExchangeInvokeRequest containing a"
                        " ConnectionName that does not match the ConnectionName expected by the bots active"
                        " OAuthPrompt. Ensure these names match when sending the InvokeActivityInvalid"
                        " ConnectionName in the TokenExchangeInvokeRequest",
                    )
                )
            elif not getattr(context.adapter, "exchange_token"):
                # Token Exchange not supported in the adapter.
                await context.send_activity(
                    self._get_token_exchange_invoke_response(
                        int(HTTPStatus.BAD_GATEWAY),
                        "The bot's BotAdapter does not support token exchange operations."
                        " Ensure the bot's Adapter supports the ExtendedUserTokenProvider interface.",
                    )
                )

                raise AttributeError(
                    "OAuthPrompt._recognize_token(): not supported by the current adapter."
                )
            else:
                # No errors. Proceed with token exchange.
                token_exchange_response = None
                try:
                    token_exchange_response = await _UserTokenAccess.exchange_token(
                        context,
                        self._settings,
                        TokenExchangeRequest(token=context.activity.value.token),
                    )
                except:
                    # Ignore Exceptions
                    # If token exchange failed for any reason, tokenExchangeResponse above stays null, and
                    # hence we send back a failure invoke response to the caller.
                    pass

                if not token_exchange_response or not token_exchange_response.token:
                    await context.send_activity(
                        self._get_token_exchange_invoke_response(
                            int(HTTPStatus.PRECONDITION_FAILED),
                            "The bot is unable to exchange token. Proceed with regular login.",
                        )
                    )
                else:
                    await context.send_activity(
                        self._get_token_exchange_invoke_response(
                            int(HTTPStatus.OK), None, context.activity.value.id
                        )
                    )
                    token = TokenResponse(
                        channel_id=token_exchange_response.channel_id,
                        connection_name=token_exchange_response.connection_name,
                        token=token_exchange_response.token,
                        expiration=None,
                    )
        elif context.activity.type == ActivityTypes.message and context.activity.text:
            match = re.match(r"(?<!\d)\d{6}(?!\d)", context.activity.text)
            if match:
                token = await _UserTokenAccess.get_user_token(
                    context, self._settings, match[0]
                )

        return (
            PromptRecognizerResult(True, token)
            if token is not None
            else PromptRecognizerResult()
        )

    def _get_token_exchange_invoke_response(
        self, status: int, failure_detail: str, identifier: str = None
    ) -> Activity:
        return Activity(
            type=ActivityTypes.invoke_response,
            value=InvokeResponse(
                status=status,
                body=TokenExchangeInvokeResponse(
                    id=identifier,
                    connection_name=self._settings.connection_name,
                    failure_detail=failure_detail,
                ),
            ),
        )

    @staticmethod
    def _is_token_response_event(context: TurnContext) -> bool:
        activity = context.activity

        return (
            activity.type == ActivityTypes.event
            and activity.name == SignInConstants.token_response_event_name
        )

    @staticmethod
    def _is_teams_verification_invoke(context: TurnContext) -> bool:
        activity = context.activity

        return (
            activity.type == ActivityTypes.invoke
            and activity.name == SignInConstants.verify_state_operation_name
        )

    @staticmethod
    def _channel_suppports_oauth_card(channel_id: str) -> bool:
        if channel_id in [
            Channels.cortana,
            Channels.skype,
            Channels.skype_for_business,
        ]:
            return False

        return True

    @staticmethod
    def _channel_requires_sign_in_link(channel_id: str) -> bool:
        if channel_id in [Channels.ms_teams]:
            return True

        return False

    @staticmethod
    def _is_token_exchange_request_invoke(context: TurnContext) -> bool:
        activity = context.activity

        return (
            activity.type == ActivityTypes.invoke
            and activity.name == SignInConstants.token_exchange_operation_name
        )

    @staticmethod
    def _is_token_exchange_request(obj: TokenExchangeInvokeRequest) -> bool:
        return bool(obj.connection_name) and bool(obj.token)