Skip to content

Commit bb83c82

Browse files
author
Michael Miele
committed
Update oauth_prompt.py
Added reference documentation.
1 parent f6f69b7 commit bb83c82

File tree

1 file changed

+91
-25
lines changed

1 file changed

+91
-25
lines changed

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

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,54 @@
3232

3333

3434
class OAuthPrompt(Dialog):
35-
"""
35+
"""Creates a new prompt for the user to sign in
3636
Creates a new prompt that asks the user to sign in using the Bot Framework Single Sign On (SSO) service.
37-
The prompt will attempt to retrieve the users current token and if the user isn't signed in, it
38-
will send them an `OAuthCard` containing a button they can press to sign in. Depending on the channel,
39-
the user will be sent through one of two possible sign-in flows:
40-
- The automatic sign-in flow where once the user signs in, the SSO service will forward
41-
the bot the users access token using either an `event` or `invoke` activity.
42-
- The "magic code" flow where once the user signs in, they will be prompted by the SSO service
43-
to send the bot a six digit code confirming their identity. This code will be sent as a
44-
standard `message` activity.
45-
Both flows are automatically supported by the `OAuthPrompt` and they only thing you need to be careful of
46-
is that you don't block the `event` and `invoke` activities that the prompt might be waiting on.
47-
Note:
48-
You should avaoid persisting the access token with your bots other state. The Bot Frameworks SSO service
49-
will securely store the token on your behalf. If you store it in your bots state,
50-
it could expire or be revoked in between turns.
51-
When calling the prompt from within a waterfall step, you should use the token within the step
52-
following the prompt and then let the token go out of scope at the end of your function
53-
Prompt Usage
54-
When used with your bots `DialogSet`, you can simply add a new instance of the prompt as a named dialog using
55-
`DialogSet.add()`.
56-
You can then start the prompt from a waterfall step using either
57-
`DialogContext.begin()` or `DialogContext.prompt()`.
58-
The user will be prompted to sign in as needed and their access token will be passed as an argument to the callers
59-
next waterfall step.
60-
"""
6137
38+
.. remarks::
39+
The prompt will attempt to retrieve the users current token and if the user isn't signed in, it
40+
will send them an `OAuthCard` containing a button they can press to sign in. Depending on the channel,
41+
the user will be sent through one of two possible sign-in flows:
42+
- The automatic sign-in flow where once the user signs in, the SSO service will forward
43+
the bot the users access token using either an `event` or `invoke` activity.
44+
- The "magic code" flow where once the user signs in, they will be prompted by the SSO service
45+
to send the bot a six digit code confirming their identity. This code will be sent as a
46+
standard `message` activity.
47+
Both flows are automatically supported by the `OAuthPrompt` and they only thing you need to be careful of
48+
is that you don't block the `event` and `invoke` activities that the prompt might be waiting on.
49+
50+
.. note::
51+
You should avoid persisting the access token with your bots other state. The Bot Frameworks SSO service
52+
will securely store the token on your behalf. If you store it in your bots state,
53+
it could expire or be revoked in between turns.
54+
When calling the prompt from within a waterfall step, you should use the token within the step
55+
following the prompt and then let the token go out of scope at the end of your function.
56+
57+
**Prompt Usage**
58+
When used with your bots :class:`DialogSet`, you can simply add a new instance of the prompt as a named dialog using
59+
:meth`DialogSet.add()`.
60+
You can then start the prompt from a waterfall step using either :meth:`DialogContext.begin()` or :meth:`DialogContext.prompt()`.
61+
The user will be prompted to sign in as needed and their access token will be passed as an argument to the callers
62+
next waterfall step.
63+
"""
6264
def __init__(
6365
self,
6466
dialog_id: str,
6567
settings: OAuthPromptSettings,
6668
validator: Callable[[PromptValidatorContext], Awaitable[bool]] = None,
6769
):
70+
""" Creates a :class:`OAuthPrompt` instance
71+
Creates a new instance of the :class:`OAuthPrompt` class.
72+
73+
:param dialogId: The Id to assign to this prompt.
74+
:type dialogId: str
75+
:param settings: Additional authentication settings to use with this instance of the prompt.
76+
:type settings: :class:`OAuthPromptSettings`
77+
:param validator: Optional, a :class:`PromptValidator` that contains additional, custom validation for this prompt.
78+
:type validator: :class:`PromptValidatorContext`
79+
80+
.. remarks::
81+
The value of :param dialogId: must be unique within the :class:`DialogSet`or :class:`ComponentDialog` to which the prompt is added.
82+
"""
6883
super().__init__(dialog_id)
6984
self._validator = validator
7085

@@ -79,6 +94,20 @@ def __init__(
7994
async def begin_dialog(
8095
self, dialog_context: DialogContext, options: PromptOptions = None
8196
) -> DialogTurnResult:
97+
""" Starts an authentication prompt dialog.
98+
Called when an authentication prompt dialog is pushed onto the dialog stack and is being activated.
99+
100+
:param dialog_context: The dialog context for the current turn of the conversation.
101+
:type dialog_context: :class:`DialogContext`
102+
:param options: Optional, additional information to pass to the prompt being started.
103+
:type options: :class:PromptOptions
104+
:return: Dialog turn result
105+
:rtype: :class:DialogTurnResult
106+
107+
.. remarks::
108+
If the task is successful, the result indicates whether the prompt is still active
109+
after the turn has been processed by the prompt.
110+
"""
82111
if dialog_context is None:
83112
raise TypeError(
84113
f"OAuthPrompt.begin_dialog: Expected DialogContext but got NoneType instead"
@@ -120,6 +149,20 @@ async def begin_dialog(
120149
return Dialog.end_of_turn
121150

122151
async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResult:
152+
""" Continues a dialog.
153+
Called when a prompt dialog is the active dialog and the user replied with a new activity.
154+
155+
:param dialog_context: The dialog context for the current turn of the conversation.
156+
:type dialog_context: :class:`DialogContext`
157+
:return: Dialog turn result
158+
:rtype: :class:DialogTurnResult
159+
160+
.. remarks::
161+
If the task is successful, the result indicates whether the dialog is still
162+
active after the turn has been processed by the dialog.
163+
The prompt generally continues to receive the user's replies until it accepts the
164+
user's reply as valid input for the prompt.
165+
"""
123166
# Recognize token
124167
recognized = await self._recognize_token(dialog_context.context)
125168

@@ -167,6 +210,18 @@ async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResu
167210
async def get_user_token(
168211
self, context: TurnContext, code: str = None
169212
) -> TokenResponse:
213+
"""Gets the user's token
214+
Attempts to get the user's token.
215+
216+
:param context: Context for the current turn of conversation with the user.
217+
:type context: :class:TurnContext
218+
:return: A response that includes the user's token
219+
:rtype: :class:TokenResponse
220+
221+
.. remarks::
222+
If the task is successful and the user already has a token or the user successfully signs in,
223+
the result contains the user's token.
224+
"""
170225
adapter = context.adapter
171226

172227
# Validate adapter type
@@ -180,6 +235,17 @@ async def get_user_token(
180235
)
181236

182237
async def sign_out_user(self, context: TurnContext):
238+
"""Signs out the user
239+
240+
:param context: Context for the current turn of conversation with the user.
241+
:type context: :class:`TurnContext`
242+
:return: A :class:`Task` representing the work queued to execute.
243+
:rtype: :class:`Task`
244+
245+
.. remarks::
246+
If the task is successful and the user already has a token or the user successfully signs in,
247+
the result contains the user's token.
248+
"""
183249
adapter = context.adapter
184250

185251
# Validate adapter type

0 commit comments

Comments
 (0)