Ejemplo n.º 1
0
        private void BackupEntrySecrets()
        {
            var accountSettings = _model.ServerAccountSettings.Query().First();

            if (!accountSettings.BackupRecoveryPasswordHashSet)
                return;

            var secrets = _model.EntriesSharedSecrets.Query().ToList();
            foreach (var secret in secrets)
            {
                var entry = _model.Entries.Query().First(r => r.Id == secret.EntryId);
                var database = _model.Links.Query().First(r => r.Id == entry.LinkId);

                if (secret.ToBeDeleted)
                {
                    var deleteRequest = new DeleteDatabaseEntryDeviceSecret
                    {
                        LinkIdentifier = database.Identifier,
                        SecretIdentifier = secret.SecretIdentifier
                    };

                    try
                    {
                        deleteRequest.GetResponse(GetApiClient());
                    }
                    catch (RequestException)
                    {
                        // Try again next time.
                        continue;
                    }

                    _model.EntriesSharedSecretsData.Delete(secret.EntrySecretDataId);
                    _model.EntriesSharedSecrets.Delete(secret.Id);

                    continue;
                }

                // Is there an update request for this entry?
                var updateRequests = _model.EntriesSharedSecretsSync.Query()
                    .Where(r => r.EntrySecretId == secret.Id).ToList();

                if (updateRequests.Count == 0)
                    continue;

                var updateRequestsSorted = updateRequests.OrderByDescending(update => update.CreatedAt);
                var latestUpdateRequest = updateRequestsSorted.First();
                
                // Remove duplicate update requests
                foreach (var updateRequest in updateRequestsSorted)
                {
                    if (updateRequest.Id == latestUpdateRequest.Id)
                        continue;

                    _model.EntriesSharedSecretsSync.Delete(updateRequest.Id);
                }
                
                var secretData = _model.EntriesSharedSecretsData.Query().First(r => r.Id == secret.EntrySecretDataId);
                var serializedEntry = JsonConvert.SerializeObject(secretData);
                var encryptedData = new EncryptedDataWithPassword(
                    Compression.Compress(serializedEntry), accountSettings.BackupRecoveryPasswordHash).ToString();
                
                var request = new SetDatabaseEntryDeviceSecret
                {
                    LinkIdentifier = database.Identifier,
                    EntryIdentifier = entry.Identifier,
                    SecretIdentifier = secret.SecretIdentifier,
                    DataType = "ModelJsonGz",
                    Data = encryptedData
                };

                try
                {
                    request.GetResponse(GetApiClient());
                }
                catch (RequestException)
                {
                    // Try again later
                    continue;
                }
                
                // Sync completed
                _model.EntriesSharedSecretsSync.Delete(latestUpdateRequest.Id);
            }
        }
Ejemplo n.º 2
0
        private void BackupEntrySecrets()
        {
            var accountSettings = _model.ServerAccountSettings.Query().First();

            if (!accountSettings.BackupRecoveryPasswordHashSet)
            {
                return;
            }

            var secrets = _model.EntriesSharedSecrets.Query().ToList();

            foreach (var secret in secrets)
            {
                var entry    = _model.Entries.Query().First(r => r.Id == secret.EntryId);
                var database = _model.Links.Query().First(r => r.Id == entry.LinkId);

                if (secret.ToBeDeleted)
                {
                    var deleteRequest = new DeleteDatabaseEntryDeviceSecret
                    {
                        LinkIdentifier   = database.Identifier,
                        SecretIdentifier = secret.SecretIdentifier
                    };

                    try
                    {
                        deleteRequest.GetResponse(GetApiClient());
                    }
                    catch (RequestException)
                    {
                        // Try again next time.
                        continue;
                    }

                    _model.EntriesSharedSecretsData.Delete(secret.EntrySecretDataId);
                    _model.EntriesSharedSecrets.Delete(secret.Id);

                    continue;
                }

                // Is there an update request for this entry?
                var updateRequests = _model.EntriesSharedSecretsSync.Query()
                                     .Where(r => r.EntrySecretId == secret.Id).ToList();

                if (updateRequests.Count == 0)
                {
                    continue;
                }

                var updateRequestsSorted = updateRequests.OrderByDescending(update => update.CreatedAt);
                var latestUpdateRequest  = updateRequestsSorted.First();

                // Remove duplicate update requests
                foreach (var updateRequest in updateRequestsSorted)
                {
                    if (updateRequest.Id == latestUpdateRequest.Id)
                    {
                        continue;
                    }

                    _model.EntriesSharedSecretsSync.Delete(updateRequest.Id);
                }

                var secretData      = _model.EntriesSharedSecretsData.Query().First(r => r.Id == secret.EntrySecretDataId);
                var serializedEntry = JsonConvert.SerializeObject(secretData);
                var encryptedData   = new EncryptedDataWithPassword(
                    Compression.Compress(serializedEntry), accountSettings.BackupRecoveryPasswordHash).ToString();

                var request = new SetDatabaseEntryDeviceSecret
                {
                    LinkIdentifier   = database.Identifier,
                    EntryIdentifier  = entry.Identifier,
                    SecretIdentifier = secret.SecretIdentifier,
                    DataType         = "ModelJsonGz",
                    Data             = encryptedData
                };

                try
                {
                    request.GetResponse(GetApiClient());
                }
                catch (RequestException)
                {
                    // Try again later
                    continue;
                }

                // Sync completed
                _model.EntriesSharedSecretsSync.Delete(latestUpdateRequest.Id);
            }
        }