Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
singhk97 committed Sep 3, 2024
1 parent bcb7f26 commit c4d9fe3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;
using Microsoft.Teams.AI.Exceptions;
using Newtonsoft.Json.Linq;

namespace Microsoft.Teams.AI.Application.Authentication.Bot
{
internal class FilteredTeamsSSOTokenExchangeMiddleware : TeamsSSOTokenExchangeMiddleware
internal class FilteredTeamsSSOTokenExchangeMiddleware : IMiddleware
{
private string _oauthConnectionName;
private TeamsSSOTokenExchangeMiddleware tokenExchangeMiddleware;

public FilteredTeamsSSOTokenExchangeMiddleware(IStorage storage, string oauthConnectionName) : base(storage, oauthConnectionName)
public FilteredTeamsSSOTokenExchangeMiddleware(IStorage storage, string oauthConnectionName)
{
this.tokenExchangeMiddleware = new TeamsSSOTokenExchangeMiddleware(storage, oauthConnectionName);
this._oauthConnectionName = oauthConnectionName;
}

public new async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
{
if (string.Equals(Channels.Msteams, turnContext.Activity.ChannelId, StringComparison.OrdinalIgnoreCase)
&& string.Equals(SignInConstants.TokenExchangeOperationName, turnContext.Activity.Name, StringComparison.OrdinalIgnoreCase))
{
string? connectionName = _GetConnectionName(turnContext);

// If connection name matches then continue to the Teams SSO Token Exchange Middleware.
if (connectionName == this._oauthConnectionName)
{
await tokenExchangeMiddleware.OnTurnAsync(turnContext, next, cancellationToken).ConfigureAwait(false);
return;
}
}

await next(cancellationToken).ConfigureAwait(false);
}

private string? _GetConnectionName(ITurnContext turnContext)
{
JObject? obj = turnContext.Activity.Value as JObject;
if (obj == null)
{
throw new TeamsAIException("Excepted `turnContext.Activity.Value` to have `connectionName` property");
};
string? connectionName = obj.Value<string>("connectionName");

// If connection name matches then continue to the Teams SSO Token Exchange Middleware.
if (connectionName == this._oauthConnectionName)
{
await base.OnTurnAsync(turnContext, next, cancellationToken);
}
else
{
await next(cancellationToken);
}
return obj.Value<string>("connectionName");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public OAuthBotAuthentication(Application<TState> app, OAuthSettings oauthSettin
this._oauthPrompt = new OAuthPrompt("OAuthPrompt", this._oauthSettings);

// Handles deduplication of token exchange event when using SSO with Bot Authentication
app.Adapter.Use(new FilteredTeamsSSOTokenExchangeMiddleware(storage ?? new MemoryStorage(), settingName));
if (!IsTokenExchangeMiddlewareRegistered(app))
{
app.Adapter.Use(new FilteredTeamsSSOTokenExchangeMiddleware(storage ?? new MemoryStorage(), oauthSettings.ConnectionName));
}
}

/// <summary>
Expand Down Expand Up @@ -115,9 +118,14 @@ public async Task<Attachment> CreateOAuthCard(ITurnContext context, Cancellation
};
}

protected async virtual Task<SignInResource> GetSignInResourceAsync(ITurnContext context, string connectionName, CancellationToken cancellationToken = default)
protected virtual async Task<SignInResource> GetSignInResourceAsync(ITurnContext context, string connectionName, CancellationToken cancellationToken = default)
{
return await UserTokenClientWrapper.GetSignInResourceAsync(context, this._oauthSettings.ConnectionName, cancellationToken);
}

private bool IsTokenExchangeMiddlewareRegistered(Application<TState> app)
{
return app.Adapter.MiddlewareSet.Where(middleWare => middleWare as FilteredTeamsSSOTokenExchangeMiddleware is not null).Count() > 0;
}
}
}

0 comments on commit c4d9fe3

Please sign in to comment.