-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[PM-34429] Create an endpoint to retrieve the InviteBlob #7994
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
Draft
JimmyVo16
wants to merge
3
commits into
pm-40216/data-protect-invite-link-codes
Choose a base branch
from
ac/pm-34429/get-invite-blob-endpoint
base: pm-40216/data-protect-invite-link-codes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
12 changes: 12 additions & 0 deletions
12
src/Api/AdminConsole/Models/Request/Organizations/GetOrganizationInviteBlobRequestModel.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 System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace Bit.Api.AdminConsole.Models.Request.Organizations; | ||
|
|
||
| public class GetOrganizationInviteBlobRequestModel | ||
| { | ||
| [Required] | ||
| public required Guid Code { get; set; } | ||
|
|
||
| [Required] | ||
| public required Guid OrganizationId { get; set; } | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/Api/AdminConsole/Models/Response/Organizations/OrganizationInviteBlobResponseModel.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.Api.AdminConsole.Models.Response.Organizations; | ||
|
|
||
| public record OrganizationInviteBlobResponseModel(string InviteBlob); |
46 changes: 46 additions & 0 deletions
46
src/Core/AdminConsole/OrganizationFeatures/InviteLinks/GetOrganizationInviteBlobCommand.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,46 @@ | ||
| ο»Ώusing Bit.Core.AdminConsole.AbilitiesCache; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.InviteLinks.Interfaces; | ||
| using Bit.Core.AdminConsole.Repositories; | ||
| using Bit.Core.AdminConsole.Utilities; | ||
| using Bit.Core.AdminConsole.Utilities.v2.Results; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.InviteLinks; | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the opaque invite blob for an invite link. See | ||
| /// <see cref="IGetOrganizationInviteBlobCommand"/> for the behavior. | ||
| /// </summary> | ||
| public class GetOrganizationInviteBlobCommand( | ||
| IOrganizationInviteLinkRepository organizationInviteLinkRepository, | ||
| IOrganizationAbilityCacheService organizationAbilityCacheService) | ||
| : IGetOrganizationInviteBlobCommand | ||
| { | ||
| public async Task<CommandResult<string>> GetInviteBlobAsync(GetOrganizationInviteBlobRequest request) | ||
| { | ||
| var user = request.User; | ||
|
|
||
| var link = await organizationInviteLinkRepository.GetByOrganizationIdAsync(request.OrganizationId); | ||
| if (link is null || !link.CodeMatches(request.Code.ToString())) | ||
| { | ||
| return new InviteLinkNotFound(); | ||
| } | ||
|
|
||
| var organizationAbility = await organizationAbilityCacheService.GetOrganizationAbilityAsync(link.OrganizationId); | ||
| if (organizationAbility is null or { Enabled: false }) | ||
| { | ||
| return new InviteLinkNotFound(); | ||
| } | ||
|
|
||
| if (!organizationAbility.UseInviteLinks) | ||
| { | ||
| return new InviteLinkNotAvailable(); | ||
| } | ||
|
|
||
| if (!InviteLinkDomainValidator.IsEmailDomainAllowed(user.Email, link.GetAllowedDomains())) | ||
| { | ||
| return new EmailDomainNotAllowed(); | ||
| } | ||
|
|
||
| return link.Invite; | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
src/Core/AdminConsole/OrganizationFeatures/InviteLinks/GetOrganizationInviteBlobRequest.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,26 @@ | ||
| ο»Ώusing Bit.Core.Entities; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.InviteLinks; | ||
|
|
||
| /// <summary> | ||
| /// The data required to retrieve the invite blob for an invite link. The blob is an opaque | ||
| /// cryptographic value that the server stores and transports but never inspects; it is decrypted | ||
| /// client-side to reconstruct and confirm the invite link. | ||
| /// </summary> | ||
| public record GetOrganizationInviteBlobRequest | ||
| { | ||
| /// <summary> | ||
| /// The ID of the organization whose invite link the user is retrieving the blob for. | ||
| /// </summary> | ||
| public required Guid OrganizationId { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The secret code embedded in the invite link the user is retrieving the blob for. | ||
| /// </summary> | ||
| public required Guid Code { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The authenticated user requesting the invite blob. | ||
| /// </summary> | ||
| public required User User { get; init; } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...nConsole/OrganizationFeatures/InviteLinks/Interfaces/IGetOrganizationInviteBlobCommand.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.AdminConsole.Utilities.v2.Results; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.InviteLinks.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the opaque invite blob for an invite link after validating that the link exists, its | ||
| /// organization is enabled and supports invite links, and the user's email domain is allowed. | ||
| /// </summary> | ||
| public interface IGetOrganizationInviteBlobCommand | ||
| { | ||
| Task<CommandResult<string>> GetInviteBlobAsync(GetOrganizationInviteBlobRequest request); | ||
| } |
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
158 changes: 158 additions & 0 deletions
158
...IntegrationTest/AdminConsole/Controllers/OrganizationUsersControllerGetInviteBlobTests.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,158 @@ | ||
| ο»Ώusing System.Net; | ||
| using Bit.Api.AdminConsole.Models.Request.Organizations; | ||
| using Bit.Api.AdminConsole.Models.Response.Organizations; | ||
| using Bit.Api.IntegrationTest.Factories; | ||
| using Bit.Api.IntegrationTest.Helpers; | ||
| using Bit.Core; | ||
| using Bit.Core.AdminConsole.AbilitiesCache; | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Enums; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Core.Services; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Api.IntegrationTest.AdminConsole.Controllers; | ||
|
|
||
| public class OrganizationUsersControllerGetInviteBlobTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime | ||
| { | ||
| private readonly HttpClient _client; | ||
| private readonly ApiApplicationFactory _factory; | ||
| private readonly LoginHelper _loginHelper; | ||
|
|
||
| private const string _validEncryptedKey = | ||
| "2.AOs41Hd8OQiCPXjyJKCiDA==|O6OHgt2U2hJGBSNGnimJmg==|iD33s8B69C8JhYYhSa4V1tArjvLr8eEaGqOV7BRo5Jk="; | ||
|
|
||
| private Organization _organization = null!; | ||
| private string _ownerEmail = null!; | ||
|
|
||
| public OrganizationUsersControllerGetInviteBlobTests(ApiApplicationFactory factory) | ||
| { | ||
| _factory = factory; | ||
| _factory.SubstituteService<IFeatureService>(featureService => | ||
| { | ||
| featureService | ||
| .IsEnabled(FeatureFlagKeys.GenerateInviteLink) | ||
| .Returns(true); | ||
| featureService | ||
| .IsEnabled(FeatureFlagKeys.InviteLinkAutoConfirm) | ||
| .Returns(true); | ||
| }); | ||
| _client = factory.CreateClient(); | ||
| _loginHelper = new LoginHelper(_factory, _client); | ||
| } | ||
|
|
||
| public async Task InitializeAsync() | ||
| { | ||
| _ownerEmail = $"integration-test{Guid.NewGuid()}@example.com"; | ||
| await _factory.LoginWithNewAccount(_ownerEmail); | ||
|
|
||
| (_organization, _) = await OrganizationTestHelpers.SignUpAsync( | ||
| _factory, | ||
| plan: PlanType.EnterpriseAnnually, | ||
| ownerEmail: _ownerEmail, | ||
| passwordManagerSeats: 10, | ||
| paymentMethod: PaymentMethodType.Card); | ||
|
|
||
| var organizationRepository = _factory.GetService<IOrganizationRepository>(); | ||
| _organization.UseInviteLinks = true; | ||
| await organizationRepository.ReplaceAsync(_organization); | ||
|
|
||
| // The endpoint reads Enabled/UseInviteLinks from the organization ability cache, so refresh it | ||
| // to reflect the invite links being enabled above. | ||
| await _factory.GetService<IOrganizationAbilityCacheService>() | ||
| .UpsertOrganizationAbilityAsync(_organization); | ||
|
|
||
| await _loginHelper.LoginAsync(_ownerEmail); | ||
| } | ||
|
|
||
| public Task DisposeAsync() | ||
| { | ||
| _client.Dispose(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetInviteBlob_WithValidRequest_ReturnsOkAndInviteBlob() | ||
| { | ||
| // Arrange | ||
| var code = await CreateInviteLinkAsync(["example.com"]); | ||
| var (joinerClient, _) = await CreateJoinerClientAsync(); | ||
|
|
||
| // Act | ||
| var response = await joinerClient.PostAsJsonAsync( | ||
| "/organizations/users/invite-link/invite-blob", BuildRequest(_organization.Id, code)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
|
||
| var result = await response.Content.ReadFromJsonAsync<OrganizationInviteBlobResponseModel>(); | ||
| Assert.NotNull(result); | ||
| Assert.Equal(_validEncryptedKey, result.InviteBlob); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetInviteBlob_WithUnknownCode_ReturnsNotFound() | ||
| { | ||
| // Arrange | ||
| await CreateInviteLinkAsync(["example.com"]); | ||
| var (joinerClient, _) = await CreateJoinerClientAsync(); | ||
|
|
||
| // Act β supply a code that does not match the organization's invite link | ||
| var response = await joinerClient.PostAsJsonAsync( | ||
| "/organizations/users/invite-link/invite-blob", BuildRequest(_organization.Id, Guid.NewGuid())); | ||
|
|
||
| // Assert | ||
| Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetInviteBlob_WhenEmailDomainNotAllowed_ReturnsBadRequest() | ||
| { | ||
| // Arrange β the link only allows a domain the joiner does not have | ||
| var code = await CreateInviteLinkAsync(["notallowed.example"]); | ||
| var (joinerClient, _) = await CreateJoinerClientAsync(); | ||
|
|
||
| // Act | ||
| var response = await joinerClient.PostAsJsonAsync( | ||
| "/organizations/users/invite-link/invite-blob", BuildRequest(_organization.Id, code)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); | ||
| } | ||
|
|
||
| private async Task<Guid> CreateInviteLinkAsync(string[] allowedDomains) | ||
| { | ||
| var createRequest = new CreateOrganizationInviteLinkRequestModel | ||
| { | ||
| AllowedDomains = allowedDomains, | ||
| Invite = _validEncryptedKey, | ||
| SupportsConfirmation = true, | ||
| }; | ||
| var createResponse = await _client.PostAsJsonAsync( | ||
| $"/organizations/{_organization.Id}/invite-link", createRequest); | ||
| Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode); | ||
|
|
||
| var created = await createResponse.Content.ReadFromJsonAsync<OrganizationInviteLinkResponseModel>(); | ||
| Assert.NotNull(created); | ||
| return created.Code; | ||
| } | ||
|
|
||
| private async Task<(HttpClient Client, string Email)> CreateJoinerClientAsync() | ||
| { | ||
| var joinerEmail = $"integration-test{Guid.NewGuid()}@example.com"; | ||
| await _factory.LoginWithNewAccount(joinerEmail); | ||
| var joinerClient = _factory.CreateClient(); | ||
| var joinerLoginHelper = new LoginHelper(_factory, joinerClient); | ||
| await joinerLoginHelper.LoginAsync(joinerEmail); | ||
| return (joinerClient, joinerEmail); | ||
| } | ||
|
|
||
| private static GetOrganizationInviteBlobRequestModel BuildRequest(Guid organizationId, Guid code) => | ||
| new() | ||
| { | ||
| OrganizationId = organizationId, | ||
| Code = code, | ||
| }; | ||
| } |
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.