Skip to content

Commit

Permalink
Remove user verification in DeleteAccount endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
r-tome committed Oct 25, 2024
1 parent 53ad9df commit 9abc95b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 73 deletions.
17 changes: 2 additions & 15 deletions src/Api/AdminConsole/Controllers/OrganizationUsersController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Bit.Api.AdminConsole.Models.Request.Organizations;
using Bit.Api.AdminConsole.Models.Response.Organizations;
using Bit.Api.Auth.Models.Request.Accounts;
using Bit.Api.Models.Request.Organizations;
using Bit.Api.Models.Response;
using Bit.Api.Vault.AuthorizationHandlers.Collections;
Expand Down Expand Up @@ -545,7 +544,7 @@ public async Task<ListResponseModel<OrganizationUserBulkResponseModel>> BulkRemo
[RequireFeature(FeatureFlagKeys.AccountDeprovisioning)]
[HttpDelete("{id}/delete-account")]
[HttpPost("{id}/delete-account")]
public async Task DeleteAccount(Guid orgId, Guid id, [FromBody] SecretVerificationRequestModel model)
public async Task DeleteAccount(Guid orgId, Guid id)
{
if (!await _currentContext.ManageUsers(orgId))
{
Expand All @@ -558,19 +557,13 @@ public async Task DeleteAccount(Guid orgId, Guid id, [FromBody] SecretVerificati
throw new UnauthorizedAccessException();
}

if (!await _userService.VerifySecretAsync(currentUser, model.Secret))
{
await Task.Delay(2000);
throw new BadRequestException(string.Empty, "User verification failed.");
}

await _deleteManagedOrganizationUserAccountCommand.DeleteUserAsync(orgId, id, currentUser.Id);
}

[RequireFeature(FeatureFlagKeys.AccountDeprovisioning)]
[HttpDelete("delete-account")]
[HttpPost("delete-account")]
public async Task<ListResponseModel<OrganizationUserBulkResponseModel>> BulkDeleteAccount(Guid orgId, [FromBody] SecureOrganizationUserBulkRequestModel model)
public async Task<ListResponseModel<OrganizationUserBulkResponseModel>> BulkDeleteAccount(Guid orgId, [FromBody] OrganizationUserBulkRequestModel model)
{
if (!await _currentContext.ManageUsers(orgId))
{
Expand All @@ -583,12 +576,6 @@ public async Task<ListResponseModel<OrganizationUserBulkResponseModel>> BulkDele
throw new UnauthorizedAccessException();
}

if (!await _userService.VerifySecretAsync(currentUser, model.Secret))
{
await Task.Delay(2000);
throw new BadRequestException(string.Empty, "User verification failed.");
}

var results = await _deleteManagedOrganizationUserAccountCommand.DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id);

return new ListResponseModel<OrganizationUserBulkResponseModel>(results.Select(r =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Security.Claims;
using Bit.Api.AdminConsole.Controllers;
using Bit.Api.AdminConsole.Models.Request.Organizations;
using Bit.Api.Auth.Models.Request.Accounts;
using Bit.Api.Vault.AuthorizationHandlers.Collections;
using Bit.Core;
using Bit.Core.AdminConsole.Entities;
Expand Down Expand Up @@ -273,17 +272,12 @@ public async Task GetAccountRecoveryDetails_WithoutManageResetPasswordPermission
[Theory]
[BitAutoData]
public async Task DeleteAccount_WhenUserCanManageUsers_Success(
Guid orgId,
Guid id,
SecretVerificationRequestModel model,
User currentUser,
SutProvider<OrganizationUsersController> sutProvider)
Guid orgId, Guid id, User currentUser, SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser);
sutProvider.GetDependency<IUserService>().VerifySecretAsync(currentUser, model.Secret).Returns(true);

await sutProvider.Sut.DeleteAccount(orgId, id, model);
await sutProvider.Sut.DeleteAccount(orgId, id);

await sutProvider.GetDependency<IDeleteManagedOrganizationUserAccountCommand>()
.Received(1)
Expand All @@ -293,60 +287,35 @@ await sutProvider.GetDependency<IDeleteManagedOrganizationUserAccountCommand>()
[Theory]
[BitAutoData]
public async Task DeleteAccount_WhenUserCannotManageUsers_ThrowsNotFoundException(
Guid orgId,
Guid id,
SecretVerificationRequestModel model,
SutProvider<OrganizationUsersController> sutProvider)
Guid orgId, Guid id, SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(false);

await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.DeleteAccount(orgId, id, model));
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.DeleteAccount(orgId, id));
}

[Theory]
[BitAutoData]
public async Task DeleteAccount_WhenCurrentUserNotFound_ThrowsUnauthorizedAccessException(
Guid orgId,
Guid id,
SecretVerificationRequestModel model,
SutProvider<OrganizationUsersController> sutProvider)
Guid orgId, Guid id, SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs((User)null);

await Assert.ThrowsAsync<UnauthorizedAccessException>(() =>
sutProvider.Sut.DeleteAccount(orgId, id, model));
}

[Theory]
[BitAutoData]
public async Task DeleteAccount_WhenSecretVerificationFails_ThrowsBadRequestException(
Guid orgId,
Guid id,
SecretVerificationRequestModel model,
User currentUser,
SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser);
sutProvider.GetDependency<IUserService>().VerifySecretAsync(currentUser, model.Secret).Returns(false);

await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.DeleteAccount(orgId, id, model));
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => sutProvider.Sut.DeleteAccount(orgId, id));
}

[Theory]
[BitAutoData]
public async Task BulkDeleteAccount_WhenUserCanManageUsers_Success(
Guid orgId,
SecureOrganizationUserBulkRequestModel model,
OrganizationUserBulkRequestModel model,
User currentUser,
List<(Guid, string)> deleteResults,
SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser);
sutProvider.GetDependency<IUserService>().VerifySecretAsync(currentUser, model.Secret).Returns(true);
sutProvider.GetDependency<IDeleteManagedOrganizationUserAccountCommand>()
.DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id)
.Returns(deleteResults);
Expand All @@ -364,21 +333,18 @@ await sutProvider.GetDependency<IDeleteManagedOrganizationUserAccountCommand>()
[BitAutoData]
public async Task BulkDeleteAccount_WhenUserCannotManageUsers_ThrowsNotFoundException(
Guid orgId,
SecureOrganizationUserBulkRequestModel model,
OrganizationUserBulkRequestModel model,
SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(false);

await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.BulkDeleteAccount(orgId, model));
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.BulkDeleteAccount(orgId, model));
}

[Theory]
[BitAutoData]
public async Task BulkDeleteAccount_WhenCurrentUserNotFound_ThrowsUnauthorizedAccessException(
Guid orgId,
SecureOrganizationUserBulkRequestModel model,
SutProvider<OrganizationUsersController> sutProvider)
Guid orgId, OrganizationUserBulkRequestModel model, SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs((User)null);
Expand All @@ -390,14 +356,11 @@ await Assert.ThrowsAsync<UnauthorizedAccessException>(() =>
[Theory]
[BitAutoData]
public async Task BulkDeleteAccount_WhenSecretVerificationFails_ThrowsBadRequestException(
Guid orgId,
SecureOrganizationUserBulkRequestModel model,
User currentUser,
Guid orgId, OrganizationUserBulkRequestModel model, User currentUser,
SutProvider<OrganizationUsersController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().ManageUsers(orgId).Returns(true);
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser);
sutProvider.GetDependency<IUserService>().VerifySecretAsync(currentUser, model.Secret).Returns(false);

await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.BulkDeleteAccount(orgId, model));

Check failure on line 365 in test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs

View workflow job for this annotation

GitHub Actions / Test Results

Bit.Api.Test.AdminConsole.Controllers.OrganizationUsersControllerTests ► BulkDeleteAccount_WhenSecretVerificationFails_ThrowsBadRequestException(orgId: 5286cae3-b61b-4c36-9ea1-929a6f01ba78, model: OrganizationUserBulkRequestModel { Ids = [···] }, curre...

Failed test found in: test/Api.Test/TestResults/oss-test-results.trx Error: Assert.Throws() Failure: No exception was thrown Expected: typeof(Bit.Core.Exceptions.BadRequestException)
Raw output
Assert.Throws() Failure: No exception was thrown
Expected: typeof(Bit.Core.Exceptions.BadRequestException)
   at Bit.Api.Test.AdminConsole.Controllers.OrganizationUsersControllerTests.BulkDeleteAccount_WhenSecretVerificationFails_ThrowsBadRequestException(Guid orgId, OrganizationUserBulkRequestModel model, User currentUser, SutProvider`1 sutProvider) in /home/runner/work/server/server/test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs:line 365
--- End of stack trace from previous location ---
}
Expand Down

0 comments on commit 9abc95b

Please sign in to comment.