From f7ff11e5c664434551abf14097c3da59c1663a12 Mon Sep 17 00:00:00 2001 From: CeciliaAvila Date: Mon, 2 Sep 2024 16:18:13 -0300 Subject: [PATCH] Support CloudAdapter and ExpectReplies in SSO --- .../SkillDialog.cs | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/libraries/Microsoft.Bot.Builder.Dialogs/SkillDialog.cs b/libraries/Microsoft.Bot.Builder.Dialogs/SkillDialog.cs index 295fa1852e..0ebe7f606e 100644 --- a/libraries/Microsoft.Bot.Builder.Dialogs/SkillDialog.cs +++ b/libraries/Microsoft.Bot.Builder.Dialogs/SkillDialog.cs @@ -320,45 +320,43 @@ private async Task SendToSkillAsync(ITurnContext context, Activity act /// private async Task 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(); - if (!string.IsNullOrWhiteSpace(oauthCard?.TokenExchangeResource?.Uri)) + return false; + } + + var oauthCard = ((JObject)oauthCardAttachment.Content).ToObject(); + 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 SendTokenExchangeInvokeToSkillAsync(Activity incomingActivity, string id, string connectionName, string token, CancellationToken cancellationToken)