Skip to content

Commit

Permalink
Add exception handling for auth (microsoft#509)
Browse files Browse the repository at this point in the history
* add exception handling for auth scenario

* remove useless change

* resolve comment
  • Loading branch information
lzc850612 authored Dec 29, 2018
1 parent b760ef8 commit bf3a91f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -18,6 +19,7 @@ public class MultiProviderAuthDialog : ComponentDialog
{
private ISkillConfiguration _skillConfiguration;
private CommonResponseBuilder _responseBuilder = new CommonResponseBuilder();
private string _selectedAuthType = string.Empty;

public MultiProviderAuthDialog(ISkillConfiguration skillConfiguration)
: base(nameof(MultiProviderAuthDialog))
Expand Down Expand Up @@ -116,26 +118,50 @@ private async Task<DialogTurnResult> PromptForProvider(WaterfallStepContext step

private async Task<DialogTurnResult> PromptForAuth(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var authType = string.Empty;
if (stepContext.Result is string)
{
authType = stepContext.Result as string;
_selectedAuthType = stepContext.Result as string;
}
else if (stepContext.Result is FoundChoice)
{
var choice = stepContext.Result as FoundChoice;
authType = choice.Value;
_selectedAuthType = choice.Value;
}

return await stepContext.PromptAsync(authType, new PromptOptions());
return await stepContext.PromptAsync(_selectedAuthType, new PromptOptions());
}

private async Task<DialogTurnResult> HandleTokenResponse(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var tokenResponse = stepContext.Result as TokenResponse;
var result = await CreateProviderTokenResponse(stepContext.Context, tokenResponse);
if (tokenResponse != null && !string.IsNullOrWhiteSpace(tokenResponse.Token))
{
var result = await CreateProviderTokenResponse(stepContext.Context, tokenResponse);

return await stepContext.EndDialogAsync(result);
}
else
{
TelemetryClient.TrackEvent("TokenRetrievalFailure", new Dictionary<string, string>
{
{
"userId", stepContext.Context.Activity.From.Id
},
{
"conversationId", stepContext.Context.Activity.Conversation.Id
},
{
"activityId", stepContext.Context.Activity.Id
},
{
"activeDialog", stepContext.ActiveDialog?.Id
}
});

return await stepContext.EndDialogAsync(result);
stepContext.Context.Activity.CreateReply(CommonResponses.ErrorMessage_AuthFailure, null, new StringDictionary { { "authType", _selectedAuthType } });

return new DialogTurnResult(DialogTurnStatus.Cancelled);
}
}

private async Task<ProviderTokenResponse> CreateProviderTokenResponse(ITurnContext context, TokenResponse tokenResponse)
Expand All @@ -154,7 +180,7 @@ private async Task<ProviderTokenResponse> CreateProviderTokenResponse(ITurnConte
private Task<bool> AuthPromptValidator(PromptValidatorContext<TokenResponse> promptContext, CancellationToken cancellationToken)
{
var token = promptContext.Recognized.Value;
if (token != null)
if (token != null && !string.IsNullOrWhiteSpace(token.Token))
{
return Task.FromResult(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// https://docs.microsoft.com/en-us/visualstudio/modeling/t4-include-directive?view=vs-2017
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Bot.Solutions.Dialogs;
Expand Down Expand Up @@ -29,6 +28,8 @@ static CommonResponses()

public static BotResponse ErrorMessage => GetBotResponse();

public static BotResponse ErrorMessage_AuthFailure => GetBotResponse();

public static BotResponse ErrorMessage_SkillError => GetBotResponse();

public static BotResponse SkillAuthenticationTitle => GetBotResponse();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
],
"inputHint": "expectingInput"
},

"ConfirmSaveInfoFailed": {
"replies": [
{
Expand All @@ -22,7 +21,6 @@
],
"inputHint": "expectingInput"
},

"ErrorMessage": {
"replies": [
{
Expand All @@ -48,7 +46,15 @@
],
"inputHint": "acceptingInput"
},

"ErrorMessage_AuthFailure": {
"replies": [
{
"text": "Sorry, it looks like something went wrong when trying to get authenticated for your {authType} connection! Please try again later.",
"speak": "Sorry, it looks like something went wrong when trying to get authenticated for your {authType} connection! Please try again later."
}
],
"inputHint": "acceptingInput"
},
"ErrorMessage_SkillError": {
"replies": [
{
Expand All @@ -58,7 +64,6 @@
],
"inputHint": "expectingInput"
},

"SkillAuthenticationTitle": {
"replies": [
{
Expand All @@ -68,7 +73,6 @@
],
"inputHint": "acceptingInput"
},

"SkillAuthenticationPrompt": {
"replies": [
{
Expand All @@ -78,7 +82,6 @@
],
"inputHint": "acceptingInput"
},

"AuthProvidersPrompt": {
"replies": [
{
Expand All @@ -88,7 +91,6 @@
],
"inputHint": "expectingInput"
},

"ConfiguredAuthProvidersPrompt": {
"replies": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
],
"inputHint": "expectingInput"
},

"ConfirmSaveInfoFailed": {
"replies": [
{
Expand All @@ -22,7 +21,6 @@
],
"inputHint": "expectingInput"
},

"ErrorMessage": {
"replies": [
{
Expand All @@ -48,7 +46,15 @@
],
"inputHint": "acceptingInput"
},

"ErrorMessage_AuthFailure": {
"replies": [
{
"text": "对不起,在尝试为了您的{authType}获得授权的时候出现了问题。请稍候重试。",
"speak": "对不起,在尝试为了您的{authType}获得授权的时候出现了问题。请稍候重试。"
}
],
"inputHint": "acceptingInput"
},
"ErrorMessage_SkillError": {
"replies": [
{
Expand All @@ -58,7 +64,6 @@
],
"inputHint": "expectingInput"
},

"SkillAuthenticationTitle": {
"replies": [
{
Expand All @@ -68,7 +73,6 @@
],
"inputHint": "acceptingInput"
},

"SkillAuthenticationPrompt": {
"replies": [
{
Expand All @@ -78,7 +82,6 @@
],
"inputHint": "acceptingInput"
},

"AuthProvidersPrompt": {
"replies": [
{
Expand All @@ -88,7 +91,6 @@
],
"inputHint": "expectingInput"
},

"ConfiguredAuthProvidersPrompt": {
"replies": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ private IDictionary<string, string> AssembleTelemetryData(WaterfallStepContext s
{
{ "activityId", sc.Context.Activity.Id },
{ "userId", sc.Context.Activity.From.Id },
{ "activeDialog", sc.ActiveDialog.ToString() }
{ "activeDialog", sc.ActiveDialog?.Id }
};
return telemetryData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ private IDictionary<string, string> AssembleTelemetryData(WaterfallStepContext s
{
{ "activityId", sc.Context.Activity.Id },
{ "userId", sc.Context.Activity.From.Id },
{ "activeDialog", sc.ActiveDialog.ToString() }
{ "activeDialog", sc.ActiveDialog?.Id }
};

return telemetryData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ private IDictionary<string, string> AssembleTelemetryData(WaterfallStepContext s
var telemetryData = new Dictionary<string, string>();
telemetryData.Add("activityId", sc.Context.Activity.Id);
telemetryData.Add("userId", sc.Context.Activity.From.Id);
telemetryData.Add("activeDialog", sc.ActiveDialog.ToString());
telemetryData.Add("activeDialog", sc.ActiveDialog?.Id);

return telemetryData;
}
Expand Down

0 comments on commit bf3a91f

Please sign in to comment.