1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-05 23:53:33 +00:00

clear cipher cache when replacing ios autofill identities (#1112)

* clear cipher cache when replacing ios autofill identities

* changed to be service-centric

* support for multiple cache keys

* async suffix

* added cache keys for android
This commit is contained in:
Matt Portune
2020-10-13 15:39:36 -04:00
committed by GitHub
parent 37e19d9a60
commit 0b7e07ebab
11 changed files with 66 additions and 39 deletions

View File

@@ -33,6 +33,8 @@ namespace Bit.Core.Services
private readonly IStorageService _storageService;
private readonly II18nService _i18nService;
private readonly Func<ISearchService> _searchService;
private readonly string _clearCipherCacheKey;
private readonly string[] _allClearCipherCacheKeys;
private Dictionary<string, HashSet<string>> _domainMatchBlacklist = new Dictionary<string, HashSet<string>>
{
["google.com"] = new HashSet<string> { "script.google.com" }
@@ -47,7 +49,9 @@ namespace Bit.Core.Services
IApiService apiService,
IStorageService storageService,
II18nService i18nService,
Func<ISearchService> searchService)
Func<ISearchService> searchService,
string clearCipherCacheKey,
string[] allClearCipherCacheKeys)
{
_cryptoService = cryptoService;
_userService = userService;
@@ -56,6 +60,8 @@ namespace Bit.Core.Services
_storageService = storageService;
_i18nService = i18nService;
_searchService = searchService;
_clearCipherCacheKey = clearCipherCacheKey;
_allClearCipherCacheKeys = allClearCipherCacheKeys;
}
private List<CipherView> DecryptedCipherCache
@@ -82,9 +88,16 @@ namespace Bit.Core.Services
}
}
public void ClearCache()
public async Task ClearCacheAsync()
{
DecryptedCipherCache = null;
if (_allClearCipherCacheKeys != null && _allClearCipherCacheKeys.Length > 0)
{
foreach (var key in _allClearCipherCacheKeys)
{
await _storageService.SaveAsync(key, true);
}
}
}
public async Task<Cipher> EncryptAsync(CipherView model, SymmetricCryptoKey key = null,
@@ -218,15 +231,24 @@ namespace Bit.Core.Services
return response?.ToList() ?? new List<Cipher>();
}
public Task<List<CipherView>> GetAllDecryptedAsync()
public async Task<List<CipherView>> GetAllDecryptedAsync()
{
if (_clearCipherCacheKey != null)
{
var clearCache = await _storageService.GetAsync<bool?>(_clearCipherCacheKey);
if (clearCache.GetValueOrDefault())
{
DecryptedCipherCache = null;
await _storageService.RemoveAsync(_clearCipherCacheKey);
}
}
if (DecryptedCipherCache != null)
{
return Task.FromResult(DecryptedCipherCache);
return DecryptedCipherCache;
}
if (_getAllDecryptedTask != null && !_getAllDecryptedTask.IsCompleted && !_getAllDecryptedTask.IsFaulted)
{
return _getAllDecryptedTask;
return await _getAllDecryptedTask;
}
async Task<List<CipherView>> doTask()
{
@@ -257,7 +279,7 @@ namespace Bit.Core.Services
finally { }
}
_getAllDecryptedTask = doTask();
return _getAllDecryptedTask;
return await _getAllDecryptedTask;
}
public async Task<List<CipherView>> GetAllDecryptedForGroupingAsync(string groupingId, bool folder = true)
@@ -569,7 +591,7 @@ namespace Bit.Core.Services
}
ciphers[cipher.Id] = cipher;
await _storageService.SaveAsync(storageKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task UpsertAsync(List<CipherData> cipher)
@@ -590,20 +612,20 @@ namespace Bit.Core.Services
ciphers[c.Id] = c;
}
await _storageService.SaveAsync(storageKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task ReplaceAsync(Dictionary<string, CipherData> ciphers)
{
var userId = await _userService.GetUserIdAsync();
await _storageService.SaveAsync(string.Format(Keys_CiphersFormat, userId), ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task ClearAsync(string userId)
{
await _storageService.RemoveAsync(string.Format(Keys_CiphersFormat, userId));
ClearCache();
await ClearCacheAsync();
}
public async Task DeleteAsync(string id)
@@ -621,7 +643,7 @@ namespace Bit.Core.Services
}
ciphers.Remove(id);
await _storageService.SaveAsync(cipherKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task DeleteAsync(List<string> ids)
@@ -642,7 +664,7 @@ namespace Bit.Core.Services
ciphers.Remove(id);
}
await _storageService.SaveAsync(cipherKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task DeleteWithServerAsync(string id)
@@ -666,7 +688,7 @@ namespace Bit.Core.Services
ciphers[id].Attachments.Remove(attachment);
}
await _storageService.SaveAsync(cipherKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task DeleteAttachmentWithServerAsync(string id, string attachmentId)
@@ -721,7 +743,7 @@ namespace Bit.Core.Services
await _apiService.PutDeleteCipherAsync(id);
ciphers[id].DeletedDate = DateTime.UtcNow;
await _storageService.SaveAsync(cipherKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
public async Task RestoreWithServerAsync(string id)
@@ -740,7 +762,7 @@ namespace Bit.Core.Services
await _apiService.PutRestoreCipherAsync(id);
ciphers[id].DeletedDate = null;
await _storageService.SaveAsync(cipherKey, ciphers);
DecryptedCipherCache = null;
await ClearCacheAsync();
}
// Helpers