-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into auth/pm-12995/user-cache-buttons
- Loading branch information
Showing
27 changed files
with
1,048 additions
and
15 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
12 changes: 12 additions & 0 deletions
12
src/Api/Auth/Models/Request/Accounts/UnauthenticatedSecretVerificatioRequestModel.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Bit.Core.Utilities; | ||
|
||
namespace Bit.Api.Auth.Models.Request.Accounts; | ||
|
||
public class UnauthenticatedSecretVerificatioRequestModel : SecretVerificationRequestModel | ||
{ | ||
[Required] | ||
[StrictEmailAddress] | ||
[StringLength(256)] | ||
public string Email { get; set; } | ||
} |
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
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
src/Api/Billing/Public/Models/Response/OrganizationSubscriptionDetailsResponseModel.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Bit.Api.Billing.Public.Models; | ||
|
||
public class OrganizationSubscriptionDetailsResponseModel : IValidatableObject | ||
{ | ||
public PasswordManagerSubscriptionDetails PasswordManager { get; set; } | ||
public SecretsManagerSubscriptionDetails SecretsManager { get; set; } | ||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
if (PasswordManager == null && SecretsManager == null) | ||
{ | ||
yield return new ValidationResult("At least one of PasswordManager or SecretsManager must be provided."); | ||
} | ||
|
||
yield return ValidationResult.Success; | ||
} | ||
} | ||
public class PasswordManagerSubscriptionDetails | ||
{ | ||
public int? Seats { get; set; } | ||
public int? MaxAutoScaleSeats { get; set; } | ||
public short? Storage { get; set; } | ||
} | ||
|
||
public class SecretsManagerSubscriptionDetails | ||
{ | ||
public int? Seats { get; set; } | ||
public int? MaxAutoScaleSeats { get; set; } | ||
public int? ServiceAccounts { get; set; } | ||
public int? MaxAutoScaleServiceAccounts { get; set; } | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Api/KeyManagement/Controllers/AccountsKeyManagementController.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#nullable enable | ||
using Bit.Api.KeyManagement.Models.Requests; | ||
using Bit.Core; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.KeyManagement.Commands.Interfaces; | ||
using Bit.Core.Repositories; | ||
using Bit.Core.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Bit.Api.KeyManagement.Controllers; | ||
|
||
[Route("accounts/key-management")] | ||
[Authorize("Application")] | ||
public class AccountsKeyManagementController : Controller | ||
{ | ||
private readonly IEmergencyAccessRepository _emergencyAccessRepository; | ||
private readonly IFeatureService _featureService; | ||
private readonly IOrganizationUserRepository _organizationUserRepository; | ||
private readonly IRegenerateUserAsymmetricKeysCommand _regenerateUserAsymmetricKeysCommand; | ||
private readonly IUserService _userService; | ||
|
||
public AccountsKeyManagementController(IUserService userService, | ||
IFeatureService featureService, | ||
IOrganizationUserRepository organizationUserRepository, | ||
IEmergencyAccessRepository emergencyAccessRepository, | ||
IRegenerateUserAsymmetricKeysCommand regenerateUserAsymmetricKeysCommand) | ||
{ | ||
_userService = userService; | ||
_featureService = featureService; | ||
_regenerateUserAsymmetricKeysCommand = regenerateUserAsymmetricKeysCommand; | ||
_organizationUserRepository = organizationUserRepository; | ||
_emergencyAccessRepository = emergencyAccessRepository; | ||
} | ||
|
||
[HttpPost("regenerate-keys")] | ||
public async Task RegenerateKeysAsync([FromBody] KeyRegenerationRequestModel request) | ||
{ | ||
if (!_featureService.IsEnabled(FeatureFlagKeys.PrivateKeyRegeneration)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var user = await _userService.GetUserByPrincipalAsync(User) ?? throw new UnauthorizedAccessException(); | ||
var usersOrganizationAccounts = await _organizationUserRepository.GetManyByUserAsync(user.Id); | ||
var designatedEmergencyAccess = await _emergencyAccessRepository.GetManyDetailsByGranteeIdAsync(user.Id); | ||
await _regenerateUserAsymmetricKeysCommand.RegenerateKeysAsync(request.ToUserAsymmetricKeys(user.Id), | ||
usersOrganizationAccounts, designatedEmergencyAccess); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Api/KeyManagement/Models/Requests/KeyRegenerationRequestModel.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#nullable enable | ||
using Bit.Core.KeyManagement.Models.Data; | ||
using Bit.Core.Utilities; | ||
|
||
namespace Bit.Api.KeyManagement.Models.Requests; | ||
|
||
public class KeyRegenerationRequestModel | ||
{ | ||
public required string UserPublicKey { get; set; } | ||
|
||
[EncryptedString] | ||
public required string UserKeyEncryptedUserPrivateKey { get; set; } | ||
|
||
public UserAsymmetricKeys ToUserAsymmetricKeys(Guid userId) | ||
{ | ||
return new UserAsymmetricKeys | ||
{ | ||
UserId = userId, | ||
PublicKey = UserPublicKey, | ||
UserKeyEncryptedPrivateKey = UserKeyEncryptedUserPrivateKey, | ||
}; | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
src/Core/KeyManagement/Commands/Interfaces/IRegenerateUserAsymmetricKeysCommand.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#nullable enable | ||
using Bit.Core.Auth.Models.Data; | ||
using Bit.Core.Entities; | ||
using Bit.Core.KeyManagement.Models.Data; | ||
|
||
namespace Bit.Core.KeyManagement.Commands.Interfaces; | ||
|
||
public interface IRegenerateUserAsymmetricKeysCommand | ||
{ | ||
Task RegenerateKeysAsync(UserAsymmetricKeys userAsymmetricKeys, | ||
ICollection<OrganizationUser> usersOrganizationAccounts, | ||
ICollection<EmergencyAccessDetails> designatedEmergencyAccess); | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Core/KeyManagement/Commands/RegenerateUserAsymmetricKeysCommand.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#nullable enable | ||
using Bit.Core.Auth.Enums; | ||
using Bit.Core.Auth.Models.Data; | ||
using Bit.Core.Context; | ||
using Bit.Core.Entities; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.KeyManagement.Commands.Interfaces; | ||
using Bit.Core.KeyManagement.Models.Data; | ||
using Bit.Core.KeyManagement.Repositories; | ||
using Bit.Core.Services; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Bit.Core.KeyManagement.Commands; | ||
|
||
public class RegenerateUserAsymmetricKeysCommand : IRegenerateUserAsymmetricKeysCommand | ||
{ | ||
private readonly ICurrentContext _currentContext; | ||
private readonly ILogger<RegenerateUserAsymmetricKeysCommand> _logger; | ||
private readonly IUserAsymmetricKeysRepository _userAsymmetricKeysRepository; | ||
private readonly IPushNotificationService _pushService; | ||
|
||
public RegenerateUserAsymmetricKeysCommand( | ||
ICurrentContext currentContext, | ||
IUserAsymmetricKeysRepository userAsymmetricKeysRepository, | ||
IPushNotificationService pushService, | ||
ILogger<RegenerateUserAsymmetricKeysCommand> logger) | ||
{ | ||
_currentContext = currentContext; | ||
_logger = logger; | ||
_userAsymmetricKeysRepository = userAsymmetricKeysRepository; | ||
_pushService = pushService; | ||
} | ||
|
||
public async Task RegenerateKeysAsync(UserAsymmetricKeys userAsymmetricKeys, | ||
ICollection<OrganizationUser> usersOrganizationAccounts, | ||
ICollection<EmergencyAccessDetails> designatedEmergencyAccess) | ||
{ | ||
var userId = _currentContext.UserId; | ||
if (!userId.HasValue || | ||
userAsymmetricKeys.UserId != userId.Value || | ||
usersOrganizationAccounts.Any(ou => ou.UserId != userId) || | ||
designatedEmergencyAccess.Any(dea => dea.GranteeId != userId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var inOrganizations = usersOrganizationAccounts.Any(ou => | ||
ou.Status is OrganizationUserStatusType.Confirmed or OrganizationUserStatusType.Revoked); | ||
var hasDesignatedEmergencyAccess = designatedEmergencyAccess.Any(x => | ||
x.Status is EmergencyAccessStatusType.Confirmed or EmergencyAccessStatusType.RecoveryApproved | ||
or EmergencyAccessStatusType.RecoveryInitiated); | ||
|
||
_logger.LogInformation( | ||
"User asymmetric keys regeneration requested. UserId: {userId} OrganizationMembership: {inOrganizations} DesignatedEmergencyAccess: {hasDesignatedEmergencyAccess} DeviceType: {deviceType}", | ||
userAsymmetricKeys.UserId, inOrganizations, hasDesignatedEmergencyAccess, _currentContext.DeviceType); | ||
|
||
// For now, don't regenerate asymmetric keys for user's with organization membership and designated emergency access. | ||
if (inOrganizations || hasDesignatedEmergencyAccess) | ||
{ | ||
throw new BadRequestException("Key regeneration not supported for this user."); | ||
} | ||
|
||
await _userAsymmetricKeysRepository.RegenerateUserAsymmetricKeysAsync(userAsymmetricKeys); | ||
_logger.LogInformation( | ||
"User's asymmetric keys regenerated. UserId: {userId} OrganizationMembership: {inOrganizations} DesignatedEmergencyAccess: {hasDesignatedEmergencyAccess} DeviceType: {deviceType}", | ||
userAsymmetricKeys.UserId, inOrganizations, hasDesignatedEmergencyAccess, _currentContext.DeviceType); | ||
|
||
await _pushService.PushSyncSettingsAsync(userId.Value); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Core/KeyManagement/KeyManagementServiceCollectionExtensions.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Bit.Core.KeyManagement.Commands; | ||
using Bit.Core.KeyManagement.Commands.Interfaces; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.Core.KeyManagement; | ||
|
||
public static class KeyManagementServiceCollectionExtensions | ||
{ | ||
public static void AddKeyManagementServices(this IServiceCollection services) | ||
{ | ||
services.AddKeyManagementCommands(); | ||
} | ||
|
||
private static void AddKeyManagementCommands(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IRegenerateUserAsymmetricKeysCommand, RegenerateUserAsymmetricKeysCommand>(); | ||
} | ||
} |
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
Oops, something went wrong.