Skip to content

Commit

Permalink
Support CloudAdapter and ExpectReplies in SSO (#6848)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciliaavila committed Sep 10, 2024
1 parent 2eba921 commit 1f6ff08
Showing 1 changed file with 27 additions and 29 deletions.
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

0 comments on commit 1f6ff08

Please sign in to comment.