mirror of
https://github.com/bitwarden/server.git
synced 2025-06-19 18:38:03 -05:00

* Add signing key repositories, models, and sql migration scripts * Rename UserSigningKeys table to UserSigningKey * Rename signedpublickeyownershipclaim to signedpublickey * Move signedPublicKey to last parameter * Add newline at end of file * Rename to signature key pair * Further rename to signaturekeypair * Rename to UserSignatureKeyPairRepository * Add newline * Rename more instances to UserSignatureKeyPair * Update parameter order * Fix order * Add more renames * Cleanup * Fix sql * Add ef migrations * Fix difference in SQL SP compared to migration SP * Fix difference in SQL SP vs migration * Fix difference in SQL SP vs migration * Attempt to fix sql * Rename migration to start later * Address feedback * Move UserSignatureKeyPair to KM codeownership * Fix build * Fix build * Fix build * Move out entitytypeconfiguration * Use view for reading usersignaturekeypairs * Fix migration script * Fix migration script * Add initial get keys endpoint * Add sync response * Cleanup * Add query and fix types * Add tests and cleanup * Fix test * Drop view if exists * Add km queries * Cleanup * Enable nullable * Cleanup * Cleanup * Enable nullable * Fix incorrect namespace * Remove unused using * Fix test build * Fix build error * Fix build * Attempt to fix tests * Attempt to fix tests * Replace with create or alter view * Attempt to fix tests * Attempt to fix build * Rename to include async suffix * Fix test * Rename repo * Attempt to fix tests * Cleanup * Test * Undo test * Fix tests * Fix test * Switch go generatecomb * Switch to generatecomb * Move signature algorithm * Move useresignaturekeypairentitytypeconfiguration to km ownership * Move userSignatureKeyPair model * Unswap file names * Move sql files to km ownership * Add index on userid for signature keys * Fix wrong filename * Fix build * Remove string length limit * Regenerate EF migrations * Undo changes to program.cs * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update util/Migrator/DbScripts/2025-06-01_00_AddSignatureKeyPairTable.sql Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Rename dbset to plural * Update src/Infrastructure.EntityFramework/KeyManagement/Repositories/UserSignatureKeyPairRepository.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/KeyManagement/Models/Response/PrivateKeysResponseModel.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/KeyManagement/Controllers/UsersController.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Cleanup and move query to core * Fix test * Fix build * Fix tests * Update src/Api/KeyManagement/Models/Response/PrivateKeysResponseModel.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Switch away from primary constructor * Use argumentNullException * Add test * Pass user account keys directly to profileresponsemodel * Move registration to core * Update src/Api/Startup.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Api/Startup.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Remove empty line * Apply suggestions * Fix tests * Fix tests --------- Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
139 lines
6.4 KiB
C#
139 lines
6.4 KiB
C#
using Bit.Api.KeyManagement.Queries.Interfaces;
|
|
using Bit.Api.Vault.Models.Response;
|
|
using Bit.Core;
|
|
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.AdminConsole.Enums.Provider;
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
using Bit.Core.Auth.UserFeatures.TwoFactorAuth.Interfaces;
|
|
using Bit.Core.Context;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Settings;
|
|
using Bit.Core.Tools.Repositories;
|
|
using Bit.Core.Vault.Models.Data;
|
|
using Bit.Core.Vault.Repositories;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Bit.Api.Vault.Controllers;
|
|
|
|
[Route("sync")]
|
|
[Authorize("Application")]
|
|
public class SyncController : Controller
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IFolderRepository _folderRepository;
|
|
private readonly ICipherRepository _cipherRepository;
|
|
private readonly ICollectionRepository _collectionRepository;
|
|
private readonly ICollectionCipherRepository _collectionCipherRepository;
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
private readonly IProviderUserRepository _providerUserRepository;
|
|
private readonly IPolicyRepository _policyRepository;
|
|
private readonly ISendRepository _sendRepository;
|
|
private readonly GlobalSettings _globalSettings;
|
|
private readonly ICurrentContext _currentContext;
|
|
private readonly Version _sshKeyCipherMinimumVersion = new(Constants.SSHKeyCipherMinimumVersion);
|
|
private readonly IFeatureService _featureService;
|
|
private readonly IApplicationCacheService _applicationCacheService;
|
|
private readonly ITwoFactorIsEnabledQuery _twoFactorIsEnabledQuery;
|
|
private readonly IUserAccountKeysQuery _userAccountKeysQuery;
|
|
|
|
public SyncController(
|
|
IUserService userService,
|
|
IFolderRepository folderRepository,
|
|
ICipherRepository cipherRepository,
|
|
ICollectionRepository collectionRepository,
|
|
ICollectionCipherRepository collectionCipherRepository,
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
IProviderUserRepository providerUserRepository,
|
|
IPolicyRepository policyRepository,
|
|
ISendRepository sendRepository,
|
|
GlobalSettings globalSettings,
|
|
ICurrentContext currentContext,
|
|
IFeatureService featureService,
|
|
IApplicationCacheService applicationCacheService,
|
|
ITwoFactorIsEnabledQuery twoFactorIsEnabledQuery,
|
|
IUserAccountKeysQuery userAccountKeysQuery)
|
|
{
|
|
_userService = userService;
|
|
_folderRepository = folderRepository;
|
|
_cipherRepository = cipherRepository;
|
|
_collectionRepository = collectionRepository;
|
|
_collectionCipherRepository = collectionCipherRepository;
|
|
_organizationUserRepository = organizationUserRepository;
|
|
_providerUserRepository = providerUserRepository;
|
|
_policyRepository = policyRepository;
|
|
_sendRepository = sendRepository;
|
|
_globalSettings = globalSettings;
|
|
_currentContext = currentContext;
|
|
_featureService = featureService;
|
|
_applicationCacheService = applicationCacheService;
|
|
_twoFactorIsEnabledQuery = twoFactorIsEnabledQuery;
|
|
_userAccountKeysQuery = userAccountKeysQuery;
|
|
}
|
|
|
|
[HttpGet("")]
|
|
public async Task<SyncResponseModel> Get([FromQuery] bool excludeDomains = false)
|
|
{
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
if (user == null)
|
|
{
|
|
throw new BadRequestException("User not found.");
|
|
}
|
|
|
|
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id,
|
|
OrganizationUserStatusType.Confirmed);
|
|
var providerUserDetails = await _providerUserRepository.GetManyDetailsByUserAsync(user.Id,
|
|
ProviderUserStatusType.Confirmed);
|
|
var providerUserOrganizationDetails =
|
|
await _providerUserRepository.GetManyOrganizationDetailsByUserAsync(user.Id,
|
|
ProviderUserStatusType.Confirmed);
|
|
var hasEnabledOrgs = organizationUserDetails.Any(o => o.Enabled);
|
|
|
|
var folders = await _folderRepository.GetManyByUserIdAsync(user.Id);
|
|
var allCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id, withOrganizations: hasEnabledOrgs);
|
|
var ciphers = FilterSSHKeys(allCiphers);
|
|
var sends = await _sendRepository.GetManyByUserIdAsync(user.Id);
|
|
|
|
IEnumerable<CollectionDetails> collections = null;
|
|
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null;
|
|
IEnumerable<Policy> policies = await _policyRepository.GetManyByUserIdAsync(user.Id);
|
|
|
|
if (hasEnabledOrgs)
|
|
{
|
|
collections = await _collectionRepository.GetManyByUserIdAsync(user.Id);
|
|
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id);
|
|
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
|
|
}
|
|
|
|
var userTwoFactorEnabled = await _twoFactorIsEnabledQuery.TwoFactorIsEnabledAsync(user);
|
|
var userHasPremiumFromOrganization = await _userService.HasPremiumFromOrganization(user);
|
|
var organizationClaimingActiveUser = await _userService.GetOrganizationsClaimingUserAsync(user.Id);
|
|
var organizationIdsClaimingActiveUser = organizationClaimingActiveUser.Select(o => o.Id);
|
|
|
|
var organizationAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
|
|
var userAccountKeys = await _userAccountKeysQuery.Run(user);
|
|
|
|
var response = new SyncResponseModel(_globalSettings, user, userAccountKeys, userTwoFactorEnabled, userHasPremiumFromOrganization, organizationAbilities,
|
|
organizationIdsClaimingActiveUser, organizationUserDetails, providerUserDetails, providerUserOrganizationDetails,
|
|
folders, collections, ciphers, collectionCiphersGroupDict, excludeDomains, policies, sends);
|
|
return response;
|
|
}
|
|
|
|
private ICollection<CipherDetails> FilterSSHKeys(ICollection<CipherDetails> ciphers)
|
|
{
|
|
if (_currentContext.ClientVersion >= _sshKeyCipherMinimumVersion || _featureService.IsEnabled(FeatureFlagKeys.SSHVersionCheckQAOverride))
|
|
{
|
|
return ciphers;
|
|
}
|
|
else
|
|
{
|
|
return ciphers.Where(c => c.Type != Core.Vault.Enums.CipherType.SSHKey).ToList();
|
|
}
|
|
}
|
|
}
|