Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6841] SkillDialog.InterceptOAuthCardsAsync doesn't support CloudAdapter #6848

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions libraries/Microsoft.Bot.Builder.Dialogs/SkillDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,45 +320,43 @@ private async Task<Activity> SendToSkillAsync(ITurnContext context, Activity act
/// </remarks>
private async Task<bool> InterceptOAuthCardsAsync(ITurnContext turnContext, Activity activity, string connectionName, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(connectionName) || !(turnContext.Adapter is IExtendedUserTokenProvider tokenExchangeProvider))
if (string.IsNullOrWhiteSpace(connectionName))
{
// The adapter may choose not to support token exchange, in which case we fallback to showing an oauth card to the user.
return false;
}

var oauthCardAttachment = activity.Attachments?.FirstOrDefault(a => a?.ContentType == OAuthCard.ContentType);
if (oauthCardAttachment != null)
if (oauthCardAttachment == null)
{
var oauthCard = ((JObject)oauthCardAttachment.Content).ToObject<OAuthCard>();
if (!string.IsNullOrWhiteSpace(oauthCard?.TokenExchangeResource?.Uri))
return false;
}

var oauthCard = ((JObject)oauthCardAttachment.Content).ToObject<OAuthCard>();
if (string.IsNullOrWhiteSpace(oauthCard?.TokenExchangeResource?.Uri))
{
return false;
}

try
{
var settings = new OAuthPromptSettings() { ConnectionName = connectionName };
var result = await UserTokenAccess.ExchangeTokenAsync(turnContext, settings, new TokenExchangeRequest(oauthCard.TokenExchangeResource.Uri), cancellationToken).ConfigureAwait(false);

if (string.IsNullOrWhiteSpace(result?.Token))
{
try
{
var result = await tokenExchangeProvider.ExchangeTokenAsync(
turnContext,
connectionName,
turnContext.Activity.From.Id,
new TokenExchangeRequest(oauthCard.TokenExchangeResource.Uri),
cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(result?.Token))
{
// If token above is null, then SSO has failed and hence we return false.
// If not, send an invoke to the skill with the token.
return await SendTokenExchangeInvokeToSkillAsync(activity, oauthCard.TokenExchangeResource.Id, oauthCard.ConnectionName, result.Token, cancellationToken).ConfigureAwait(false);
}
}
#pragma warning disable CA1031 // Do not catch general exception types (ignoring, see comment below)
catch
#pragma warning restore CA1031 // Do not catch general exception types
{
// Failures in token exchange are not fatal. They simply mean that the user needs to be shown the OAuth card.
return false;
}
// If token above is null, then SSO has failed and hence we return false.
return false;
}

// If not, send an invoke to the skill with the token.
return await SendTokenExchangeInvokeToSkillAsync(activity, oauthCard.TokenExchangeResource.Id, oauthCard.ConnectionName, result.Token, cancellationToken).ConfigureAwait(false);
}
catch
{
// Failures in token exchange are not fatal. They simply mean that the user needs to be shown the OAuth card.
return false;
}

return false;
}

private async Task<bool> SendTokenExchangeInvokeToSkillAsync(Activity incomingActivity, string id, string connectionName, string token, CancellationToken cancellationToken)
Expand Down
Loading