-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
16 deletions.
There are no files selected for viewing
40 changes: 26 additions & 14 deletions
40
...crosoft.TeamsAI/Application/Authentication/Bot/FilteredTeamsSSOTokenExchangeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters