Esempio n. 1
0
 public LoginResponseModel(CipherDetails cipher, string obj = "login")
     : this(cipher as Cipher, obj)
 {
     FolderId = cipher.FolderId?.ToString();
     Favorite = cipher.Favorite;
     Edit     = cipher.Edit;
 }
Esempio n. 2
0
        public LoginResponseModel(CipherDetails cipher, string obj = "login")
            : base(obj)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (cipher.Type != Enums.CipherType.Login)
            {
                throw new ArgumentException(nameof(cipher.Type));
            }

            var data = new LoginDataModel(cipher);

            Id             = cipher.Id.ToString();
            OrganizationId = cipher.OrganizationId?.ToString();
            FolderId       = cipher.FolderId?.ToString();
            Favorite       = cipher.Favorite;
            Name           = data.Name;
            Uri            = data.Uri;
            Username       = data.Username;
            Password       = data.Password;
            Notes          = data.Notes;
            RevisionDate   = cipher.RevisionDate;
        }
Esempio n. 3
0
 public CipherDetails ToCipherDetails(CipherDetails existingCipher)
 {
     existingCipher.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
     existingCipher.Favorite = Favorite;
     existingCipher.Reprompt = Reprompt;
     ToCipher(existingCipher);
     return(existingCipher);
 }
Esempio n. 4
0
 public CipherResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "cipher")
     : base(cipher, globalSettings, cipher.OrganizationUseTotp, obj)
 {
     FolderId     = cipher.FolderId?.ToString();
     Favorite     = cipher.Favorite;
     Edit         = cipher.Edit;
     ViewPassword = cipher.ViewPassword;
 }
Esempio n. 5
0
 public CipherResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "cipher")
     : base(cipher, globalSettings, cipher.OrganizationUseTotp, obj)
 {
     FolderId     = cipher.FolderId?.ToString();
     Favorite     = cipher.Favorite;
     Edit         = cipher.Edit;
     ViewPassword = cipher.ViewPassword;
     Reprompt     = cipher.Reprompt.GetValueOrDefault(CipherRepromptType.None);
 }
Esempio n. 6
0
        public async Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId, DateTime?lastKnownRevisionDate,
                                           IEnumerable <Guid> collectionIds = null, bool skipPermissionCheck = false)
        {
            if (!skipPermissionCheck && !(await UserCanEditAsync(cipher, savingUserId)))
            {
                throw new BadRequestException("You do not have permissions to edit this.");
            }

            cipher.UserId = savingUserId;
            if (cipher.Id == default(Guid))
            {
                if (cipher.OrganizationId.HasValue && collectionIds != null)
                {
                    var existingCollectionIds = (await _collectionRepository.GetManyByOrganizationIdAsync(cipher.OrganizationId.Value)).Select(c => c.Id);
                    if (collectionIds.Except(existingCollectionIds).Any())
                    {
                        throw new BadRequestException("Specified CollectionId does not exist on the specified Organization.");
                    }
                    await _cipherRepository.CreateAsync(cipher, collectionIds);
                }
                else
                {
                    // Make sure the user can save new ciphers to their personal vault
                    var personalOwnershipPolicyCount = await _policyRepository.GetCountByTypeApplicableToUserIdAsync(savingUserId,
                                                                                                                     PolicyType.PersonalOwnership);

                    if (personalOwnershipPolicyCount > 0)
                    {
                        throw new BadRequestException("Due to an Enterprise Policy, you are restricted from saving items to your personal vault.");
                    }
                    await _cipherRepository.CreateAsync(cipher);
                }
                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Created);

                if (cipher.OrganizationId.HasValue)
                {
                    var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);

                    cipher.OrganizationUseTotp = org.UseTotp;
                }

                // push
                await _pushService.PushSyncCipherCreateAsync(cipher, null);
            }
            else
            {
                ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
                cipher.RevisionDate = DateTime.UtcNow;
                await _cipherRepository.ReplaceAsync(cipher);

                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);

                // push
                await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
            }
        }
Esempio n. 7
0
 public async Task ReplaceAsync(CipherDetails obj)
 {
     using (var connection = new SqlConnection(ConnectionString))
     {
         var results = await connection.ExecuteAsync(
             $"[{Schema}].[CipherDetails_Update]",
             obj,
             commandType : CommandType.StoredProcedure);
     }
 }
Esempio n. 8
0
        public async Task SaveDetailsAsync_WrongRevisionDate_Throws(SutProvider <CipherService> sutProvider,
                                                                    CipherDetails cipherDetails)
        {
            var lastKnownRevisionDate = cipherDetails.RevisionDate.AddDays(-1);

            var exception = await Assert.ThrowsAsync <BadRequestException>(
                () => sutProvider.Sut.SaveDetailsAsync(cipherDetails, cipherDetails.UserId.Value, lastKnownRevisionDate));

            Assert.Contains("out of date", exception.Message);
        }
Esempio n. 9
0
 public async Task UpsertAsync(CipherDetails cipher)
 {
     if (cipher.Id.Equals(default(Guid)))
     {
         await CreateAsync(cipher);
     }
     else
     {
         await ReplaceAsync(cipher);
     }
 }
Esempio n. 10
0
 public async Task CreateAsync(CipherDetails cipher)
 {
     cipher.SetNewId();
     using (var connection = new SqlConnection(ConnectionString))
     {
         var results = await connection.ExecuteAsync(
             $"[{Schema}].[CipherDetails_Create]",
             cipher,
             commandType : CommandType.StoredProcedure);
     }
 }
Esempio n. 11
0
        public async Task DeleteAsync(CipherDetails cipher, Guid deletingUserId)
        {
            if (!(await UserCanEditAsync(cipher, deletingUserId)))
            {
                throw new BadRequestException("Not an admin.");
            }

            await _cipherRepository.DeleteAsync(cipher);

            // push
            //await _pushService.PushSyncCipherDeleteAsync(cipher);
        }
Esempio n. 12
0
        public CipherDetails ToCipherDetails(CipherDetails existingLogin)
        {
            existingLogin.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
            existingLogin.Favorite = Favorite;

            existingLogin.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
                                                             new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            existingLogin.Type = Enums.CipherType.Login;

            return(existingLogin);
        }
Esempio n. 13
0
        public CipherDetails ToCipherDetails(Guid userId)
        {
            var cipher = new CipherDetails
            {
                Type           = Type,
                UserId         = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)userId : null,
                OrganizationId = null,
                Edit           = true
            };

            ToCipherDetails(cipher);
            return(cipher);
        }
Esempio n. 14
0
 public CipherDetailsResponseModel(CipherDetails cipher,
                                   IDictionary <Guid, IGrouping <Guid, SubvaultCipher> > subvaultCiphers, string obj = "cipherDetails")
     : base(cipher, obj)
 {
     if (subvaultCiphers.ContainsKey(cipher.Id))
     {
         SubvaultIds = subvaultCiphers[cipher.Id].Select(s => s.SubvaultId);
     }
     else
     {
         SubvaultIds = new Guid[] { };
     }
 }
Esempio n. 15
0
 public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
                                   IDictionary <Guid, IGrouping <Guid, CollectionCipher> > collectionCiphers, string obj = "cipherDetails")
     : base(cipher, globalSettings, obj)
 {
     if (collectionCiphers?.ContainsKey(cipher.Id) ?? false)
     {
         CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
     }
     else
     {
         CollectionIds = new Guid[] { };
     }
 }
        public CipherDetails ToCipherDetails(Guid userId, bool allowOrgIdSet = true)
        {
            var hasOrgId = !string.IsNullOrWhiteSpace(OrganizationId);
            var cipher   = new CipherDetails
            {
                Type           = Type,
                UserId         = !hasOrgId ? (Guid?)userId : null,
                OrganizationId = allowOrgIdSet && hasOrgId ? new Guid(OrganizationId) : (Guid?)null,
                Edit           = true
            };

            ToCipherDetails(cipher);
            return(cipher);
        }
Esempio n. 17
0
        public async Task CreateAsync(CipherDetails cipher, IEnumerable <Guid> collectionIds)
        {
            cipher.SetNewId();
            var objWithCollections = JsonConvert.DeserializeObject <CipherDetailsWithCollections>(
                JsonConvert.SerializeObject(cipher));

            objWithCollections.CollectionIds = collectionIds.ToGuidIdArrayTVP();
            using (var connection = new SqlConnection(ConnectionString))
            {
                var results = await connection.ExecuteAsync(
                    $"[{Schema}].[CipherDetails_CreateWithCollections]",
                    objWithCollections,
                    commandType : CommandType.StoredProcedure);
            }
        }
Esempio n. 18
0
        public LoginDataModel(CipherDetails cipher)
        {
            if (cipher.Type != Enums.CipherType.Login)
            {
                throw new ArgumentException("Cipher is not correct type.");
            }

            var data = JsonConvert.DeserializeObject <LoginDataModel>(cipher.Data);

            Name     = data.Name;
            Uri      = data.Uri;
            Username = data.Username;
            Password = data.Password;
            Notes    = data.Notes;
        }
Esempio n. 19
0
        public async Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId, DateTime?lastKnownRevisionDate,
                                           IEnumerable <Guid> collectionIds = null, bool skipPermissionCheck = false)
        {
            if (!skipPermissionCheck && !(await UserCanEditAsync(cipher, savingUserId)))
            {
                throw new BadRequestException("You do not have permissions to edit this.");
            }

            cipher.UserId = savingUserId;
            if (cipher.Id == default(Guid))
            {
                if (cipher.OrganizationId.HasValue && collectionIds != null)
                {
                    await _cipherRepository.CreateAsync(cipher, collectionIds);
                }
                else
                {
                    await _cipherRepository.CreateAsync(cipher);
                }
                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Created);

                if (cipher.OrganizationId.HasValue)
                {
                    var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);

                    cipher.OrganizationUseTotp = org.UseTotp;
                }

                // push
                await _pushService.PushSyncCipherCreateAsync(cipher, null);
            }
            else
            {
                if (collectionIds != null)
                {
                    throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
                }
                ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
                cipher.RevisionDate = DateTime.UtcNow;
                await _cipherRepository.ReplaceAsync(cipher);

                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);

                // push
                await _pushService.PushSyncCipherUpdateAsync(cipher, null);
            }
        }
Esempio n. 20
0
        public async Task SaveAsync(CipherDetails cipher)
        {
            if (cipher.Id == default(Guid))
            {
                await _cipherRepository.CreateAsync(cipher);

                // push
                //await _pushService.PushSyncCipherCreateAsync(cipher);
            }
            else
            {
                cipher.RevisionDate = DateTime.UtcNow;
                await _cipherRepository.ReplaceAsync(cipher);

                // push
                //await _pushService.PushSyncCipherUpdateAsync(cipher);
            }
        }
Esempio n. 21
0
        private async Task <CipherDetails> CreateAsyncReturnCipher(CipherDetails cipher)
        {
            cipher.SetNewId();
            using (var scope = ServiceScopeFactory.CreateScope())
            {
                var dbContext = GetDatabaseContext(scope);
                var userIdKey = $"\"{cipher.UserId}\"";
                cipher.UserId    = cipher.OrganizationId.HasValue ? null : cipher.UserId;
                cipher.Favorites = cipher.Favorite ?
                                   $"{{{userIdKey}:true}}" :
                                   null;
                cipher.Folders = cipher.FolderId.HasValue ?
                                 $"{{{userIdKey}:\"{cipher.FolderId}\"}}" :
                                 null;
                var entity = Mapper.Map <EfModel.Cipher>((TableModel.Cipher)cipher);
                await dbContext.AddAsync(entity);

                await dbContext.SaveChangesAsync();
            }
            await UserBumpAccountRevisionDateByCipherId(cipher);

            return(cipher);
        }
Esempio n. 22
0
        public async Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId)
        {
            if (!(await UserCanEditAsync(cipher, savingUserId)))
            {
                throw new BadRequestException("You do not have permissions to edit this.");
            }

            cipher.UserId = savingUserId;
            if (cipher.Id == default(Guid))
            {
                await _cipherRepository.CreateAsync(cipher);

                // push
                await _pushService.PushSyncCipherCreateAsync(cipher);
            }
            else
            {
                cipher.RevisionDate = DateTime.UtcNow;
                await _cipherRepository.ReplaceAsync(cipher);

                // push
                await _pushService.PushSyncCipherUpdateAsync(cipher);
            }
        }
Esempio n. 23
0
 public async Task UpsertAsync(CipherDetails cipher)
 {
     if (cipher.Id.Equals(default))
Esempio n. 24
0
        public async Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId, DateTime?lastKnownRevisionDate,
                                           IEnumerable <Guid> collectionIds = null, bool skipPermissionCheck = false)
        {
            if (!skipPermissionCheck && !(await UserCanEditAsync(cipher, savingUserId)))
            {
                throw new BadRequestException("You do not have permissions to edit this.");
            }

            cipher.UserId = savingUserId;
            if (cipher.Id == default(Guid))
            {
                if (cipher.OrganizationId.HasValue && collectionIds != null)
                {
                    await _cipherRepository.CreateAsync(cipher, collectionIds);
                }
                else
                {
                    // Make sure the user can save new ciphers to their personal vault
                    var userPolicies = await _policyRepository.GetManyByUserIdAsync(savingUserId);

                    if (userPolicies != null)
                    {
                        foreach (var policy in userPolicies.Where(p => p.Enabled && p.Type == PolicyType.PersonalOwnership))
                        {
                            var org = await _organizationUserRepository.GetDetailsByUserAsync(savingUserId, policy.OrganizationId,
                                                                                              OrganizationUserStatusType.Confirmed);

                            if (org != null && org.Enabled && org.UsePolicies &&
                                org.Type != OrganizationUserType.Admin && org.Type != OrganizationUserType.Owner)
                            {
                                throw new BadRequestException("Due to an Enterprise Policy, you are restricted from saving items to your personal vault.");
                            }
                        }
                    }
                    await _cipherRepository.CreateAsync(cipher);
                }
                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Created);

                if (cipher.OrganizationId.HasValue)
                {
                    var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);

                    cipher.OrganizationUseTotp = org.UseTotp;
                }

                // push
                await _pushService.PushSyncCipherCreateAsync(cipher, null);
            }
            else
            {
                if (collectionIds != null)
                {
                    throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
                }
                ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
                cipher.RevisionDate = DateTime.UtcNow;
                await _cipherRepository.ReplaceAsync(cipher);

                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);

                // push
                await _pushService.PushSyncCipherUpdateAsync(cipher, null);
            }
        }
Esempio n. 25
0
 public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
                                   IEnumerable <CollectionCipher> collectionCiphers, string obj = "cipherDetails")
     : base(cipher, globalSettings, obj)
 {
     CollectionIds = collectionCiphers?.Select(c => c.CollectionId) ?? new List <Guid>();
 }
Esempio n. 26
0
 public CipherResponseModel(CipherDetails cipher, string obj = "cipher")
     : base(cipher, obj)
 {
     FolderId = cipher.FolderId?.ToString();
     Favorite = cipher.Favorite;
 }
Esempio n. 27
0
        public async Task ReplaceAsync(CipherDetails cipher)
        {
            cipher.UserId = cipher.OrganizationId.HasValue ?
                            null :
                            cipher.UserId;
            using (var scope = ServiceScopeFactory.CreateScope())
            {
                var dbContext = GetDatabaseContext(scope);
                var entity    = await dbContext.Ciphers.FindAsync(cipher.Id);

                if (entity != null)
                {
                    var userIdKey = $"\"{cipher.UserId}\"";
                    if (cipher.Favorite)
                    {
                        if (cipher.Favorites == null)
                        {
                            cipher.Favorites = $"{{{userIdKey}:true}}";
                        }
                        else
                        {
                            var favorites = CoreHelpers.LoadClassFromJsonData <Dictionary <Guid, bool> >(cipher.Favorites);
                            favorites.Add(cipher.UserId.Value, true);
                            cipher.Favorites = JsonConvert.SerializeObject(favorites);
                        }
                    }
                    else
                    {
                        if (cipher.Favorites != null && cipher.Favorites.Contains(cipher.UserId.Value.ToString()))
                        {
                            var favorites = CoreHelpers.LoadClassFromJsonData <Dictionary <Guid, bool> >(cipher.Favorites);
                            favorites.Remove(cipher.UserId.Value);
                            cipher.Favorites = JsonConvert.SerializeObject(favorites);
                        }
                    }
                    if (cipher.FolderId.HasValue)
                    {
                        if (cipher.Folders == null)
                        {
                            cipher.Folders = $"{{{userIdKey}:\"{cipher.FolderId}\"}}";
                        }
                        else
                        {
                            var folders = CoreHelpers.LoadClassFromJsonData <Dictionary <Guid, Guid> >(cipher.Folders);
                            folders.Add(cipher.UserId.Value, cipher.FolderId.Value);
                            cipher.Folders = JsonConvert.SerializeObject(folders);
                        }
                    }
                    else
                    {
                        if (cipher.Folders != null && cipher.Folders.Contains(cipher.UserId.Value.ToString()))
                        {
                            var folders = CoreHelpers.LoadClassFromJsonData <Dictionary <Guid, bool> >(cipher.Favorites);
                            folders.Remove(cipher.UserId.Value);
                            cipher.Favorites = JsonConvert.SerializeObject(folders);
                        }
                    }
                    var mappedEntity = Mapper.Map <EfModel.Cipher>((TableModel.Cipher)cipher);
                    dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
                    await UserBumpAccountRevisionDateByCipherId(cipher);

                    await dbContext.SaveChangesAsync();
                }
            }
        }
Esempio n. 28
0
 public async Task CreateAsync(CipherDetails cipher)
 {
     await CreateAsyncReturnCipher(cipher);
 }
Esempio n. 29
0
        public async Task SaveDetailsAsync_CorrectRevisionDate_Passes(string revisionDateString,
                                                                      SutProvider <CipherService> sutProvider, CipherDetails cipherDetails)
        {
            var lastKnownRevisionDate = string.IsNullOrEmpty(revisionDateString) ? (DateTime?)null : cipherDetails.RevisionDate;

            await sutProvider.Sut.SaveDetailsAsync(cipherDetails, cipherDetails.UserId.Value, lastKnownRevisionDate);

            await sutProvider.GetDependency <ICipherRepository>().Received(1).ReplaceAsync(cipherDetails);
        }
Esempio n. 30
0
 public CipherDetailsResponseModel(CipherDetails cipher, IEnumerable <SubvaultCipher> subvaultCiphers,
                                   string obj = "cipherDetails")
     : base(cipher, obj)
 {
     SubvaultIds = subvaultCiphers.Select(s => s.SubvaultId);
 }