-
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.
## Linked issues closes: #1744 #1933 ## Details Fixed bug in both #1744 and #1933. The issue does not persist in either JS, or PY so the fix is only for C#. #### Change details * Refactored `FilteredTeamsSSOTokenExchangeMiddleware` as the underlying middleware was not being invoked correctly. * The `app.adapter.Use` method was being called for every incomming request (since the `Application` object is a transient in the Asp.NET service collection). * As for the issue in #1744 - The code is updated to use `DateTime.UtcNow` by default. ## Attestation Checklist - [x] My code follows the style guidelines of this project - I have checked for/fixed spelling, linting, and other errors - I have commented my code for clarity - I have made corresponding changes to the documentation (updating the doc strings in the code is sufficient) - My changes generate no new warnings - I have added tests that validates my changes, and provides sufficient test coverage. I have tested with: - Local testing - E2E testing in Teams - New and existing unit tests pass locally with my changes ### Additional information > Feel free to add other relevant information below
- Loading branch information
Showing
5 changed files
with
46 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
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