-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Microsoft Teams integration #6410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
512278b
Add Microsoft Teams integration
brant-livefront cdca944
Fix method naming error
brant-livefront 886fe32
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront 85a2b7c
Expand and clean up unit test coverage
brant-livefront 2589ec4
Merge branch 'brant/microsoft-teams-integration' of github.com:bitwar…
brant-livefront f42fd7b
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront 59445c0
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront 752d1d5
Update with PR feedback
brant-livefront 5bfcde8
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront b5376d5
Add documentation, add In Progress logic/tests for Teams
brant-livefront b4024a4
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront 2ce25ed
Fixed lowercase Slack
brant-livefront ca9ec78
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront 00bae24
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront ca3f6d0
Added docs; Updated PR suggestions;
brant-livefront 1580da7
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront a812d90
Fix broken tests
brant-livefront 81fd273
Merge branch 'main' into brant/microsoft-teams-integration
brant-livefront File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
147 changes: 147 additions & 0 deletions
147
src/Api/AdminConsole/Controllers/TeamsIntegrationController.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using System.Text.Json; | ||
using Bit.Api.AdminConsole.Models.Response.Organizations; | ||
using Bit.Core; | ||
using Bit.Core.AdminConsole.Entities; | ||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations; | ||
using Bit.Core.Context; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Repositories; | ||
using Bit.Core.Services; | ||
using Bit.Core.Utilities; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Builder.Integration.AspNet.Core; | ||
|
||
namespace Bit.Api.AdminConsole.Controllers; | ||
|
||
[RequireFeature(FeatureFlagKeys.EventBasedOrganizationIntegrations)] | ||
[Route("organizations")] | ||
[Authorize("Application")] | ||
public class TeamsIntegrationController( | ||
ICurrentContext currentContext, | ||
IOrganizationIntegrationRepository integrationRepository, | ||
IBot bot, | ||
IBotFrameworkHttpAdapter adapter, | ||
ITeamsService teamsService, | ||
TimeProvider timeProvider) : Controller | ||
{ | ||
[HttpGet("{organizationId:guid}/integrations/teams/redirect")] | ||
public async Task<IActionResult> RedirectAsync(Guid organizationId) | ||
{ | ||
if (!await currentContext.OrganizationOwner(organizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var callbackUrl = Url.RouteUrl( | ||
routeName: "TeamsIntegration_Create", | ||
values: null, | ||
protocol: currentContext.HttpContext.Request.Scheme, | ||
host: currentContext.HttpContext.Request.Host.ToUriComponent() | ||
); | ||
if (string.IsNullOrEmpty(callbackUrl)) | ||
{ | ||
throw new BadRequestException("Unable to build callback Url"); | ||
} | ||
|
||
var integrations = await integrationRepository.GetManyByOrganizationAsync(organizationId); | ||
var integration = integrations.FirstOrDefault(i => i.Type == IntegrationType.Teams); | ||
|
||
if (integration is null) | ||
{ | ||
// No teams integration exists, create Initiated version | ||
integration = await integrationRepository.CreateAsync(new OrganizationIntegration | ||
{ | ||
OrganizationId = organizationId, | ||
Type = IntegrationType.Teams, | ||
Configuration = null, | ||
}); | ||
} | ||
else if (integration.Configuration is not null) | ||
{ | ||
// A Completed (fully configured) Teams integration already exists, throw to prevent overriding | ||
throw new BadRequestException("There already exists a Teams integration for this organization"); | ||
|
||
} // An Initiated teams integration exits, re-use it and kick off a new OAuth flow | ||
|
||
var state = IntegrationOAuthState.FromIntegration(integration, timeProvider); | ||
var redirectUrl = teamsService.GetRedirectUrl( | ||
callbackUrl: callbackUrl, | ||
state: state.ToString() | ||
); | ||
|
||
if (string.IsNullOrEmpty(redirectUrl)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
return Redirect(redirectUrl); | ||
} | ||
|
||
[HttpGet("integrations/teams/create", Name = "TeamsIntegration_Create")] | ||
[AllowAnonymous] | ||
public async Task<IActionResult> CreateAsync([FromQuery] string code, [FromQuery] string state) | ||
{ | ||
var oAuthState = IntegrationOAuthState.FromString(state: state, timeProvider: timeProvider); | ||
if (oAuthState is null) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
// Fetch existing Initiated record | ||
var integration = await integrationRepository.GetByIdAsync(oAuthState.IntegrationId); | ||
if (integration is null || | ||
integration.Type != IntegrationType.Teams || | ||
integration.Configuration is not null) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
// Verify Organization matches hash | ||
if (!oAuthState.ValidateOrg(integration.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var callbackUrl = Url.RouteUrl( | ||
routeName: "TeamsIntegration_Create", | ||
values: null, | ||
protocol: currentContext.HttpContext.Request.Scheme, | ||
host: currentContext.HttpContext.Request.Host.ToUriComponent() | ||
); | ||
if (string.IsNullOrEmpty(callbackUrl)) | ||
{ | ||
throw new BadRequestException("Unable to build callback Url"); | ||
} | ||
|
||
var token = await teamsService.ObtainTokenViaOAuth(code, callbackUrl); | ||
if (string.IsNullOrEmpty(token)) | ||
{ | ||
throw new BadRequestException("Invalid response from Teams."); | ||
} | ||
|
||
var teams = await teamsService.GetJoinedTeamsAsync(token); | ||
|
||
if (!teams.Any()) | ||
{ | ||
throw new BadRequestException("No teams were found."); | ||
} | ||
|
||
var teamsIntegration = new TeamsIntegration(TenantId: teams[0].TenantId, Teams: teams); | ||
integration.Configuration = JsonSerializer.Serialize(teamsIntegration); | ||
await integrationRepository.UpsertAsync(integration); | ||
|
||
var location = $"/organizations/{integration.OrganizationId}/integrations/{integration.Id}"; | ||
return Created(location, new OrganizationIntegrationResponseModel(integration)); | ||
} | ||
|
||
[Route("integrations/teams/incoming")] | ||
[AllowAnonymous] | ||
[HttpPost] | ||
public async Task IncomingPostAsync() | ||
{ | ||
await adapter.ProcessAsync(Request, Response, bot); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
src/Core/AdminConsole/Models/Data/EventIntegrations/TeamsIntegration.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Bit.Core.Models.Teams; | ||
|
||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations; | ||
|
||
public record TeamsIntegration( | ||
string TenantId, | ||
IReadOnlyList<TeamInfo> Teams, | ||
string? ChannelId = null, | ||
Uri? ServiceUrl = null) | ||
{ | ||
public bool IsCompleted => !string.IsNullOrEmpty(ChannelId) && ServiceUrl is not null; | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Core/AdminConsole/Models/Data/EventIntegrations/TeamsIntegrationConfigurationDetails.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations; | ||
|
||
public record TeamsIntegrationConfigurationDetails(string ChannelId, Uri ServiceUrl); |
38 changes: 38 additions & 0 deletions
38
src/Core/AdminConsole/Models/Data/EventIntegrations/TeamsListenerConfiguration.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Bit.Core.Enums; | ||
using Bit.Core.Settings; | ||
|
||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations; | ||
|
||
public class TeamsListenerConfiguration(GlobalSettings globalSettings) : | ||
ListenerConfiguration(globalSettings), IIntegrationListenerConfiguration | ||
{ | ||
public IntegrationType IntegrationType | ||
{ | ||
get => IntegrationType.Teams; | ||
} | ||
|
||
public string EventQueueName | ||
{ | ||
get => _globalSettings.EventLogging.RabbitMq.TeamsEventsQueueName; | ||
} | ||
|
||
public string IntegrationQueueName | ||
{ | ||
get => _globalSettings.EventLogging.RabbitMq.TeamsIntegrationQueueName; | ||
} | ||
|
||
public string IntegrationRetryQueueName | ||
{ | ||
get => _globalSettings.EventLogging.RabbitMq.TeamsIntegrationRetryQueueName; | ||
} | ||
|
||
public string EventSubscriptionName | ||
{ | ||
get => _globalSettings.EventLogging.AzureServiceBus.TeamsEventSubscriptionName; | ||
} | ||
|
||
public string IntegrationSubscriptionName | ||
{ | ||
get => _globalSettings.EventLogging.AzureServiceBus.TeamsIntegrationSubscriptionName; | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.