1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-23 11:45:38 +01:00

cache ciphers

This commit is contained in:
Kyle Spearrin 2017-11-16 16:51:43 -05:00
parent d84627aa2c
commit c1ae3f1fb2

View File

@ -22,6 +22,8 @@ namespace Bit.App.Services
private readonly ISettingsService _settingsService; private readonly ISettingsService _settingsService;
private readonly ICryptoService _cryptoService; private readonly ICryptoService _cryptoService;
private List<Cipher> _cachedCiphers = null;
public CipherService( public CipherService(
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
IAttachmentRepository attachmentRepository, IAttachmentRepository attachmentRepository,
@ -53,11 +55,18 @@ namespace Bit.App.Services
public async Task<IEnumerable<Cipher>> GetAllAsync() public async Task<IEnumerable<Cipher>> GetAllAsync()
{ {
if(_cachedCiphers != null)
{
return _cachedCiphers;
}
var attachmentData = await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId); var attachmentData = await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId);
var attachmentDict = attachmentData.GroupBy(a => a.LoginId).ToDictionary(g => g.Key, g => g.ToList()); var attachmentDict = attachmentData.GroupBy(a => a.LoginId).ToDictionary(g => g.Key, g => g.ToList());
var data = await _cipherRepository.GetAllByUserIdAsync(_authService.UserId); var data = await _cipherRepository.GetAllByUserIdAsync(_authService.UserId);
var cipher = data.Select(f => new Cipher(f, attachmentDict.ContainsKey(f.Id) ? attachmentDict[f.Id] : null)); _cachedCiphers = data
return cipher; .Select(f => new Cipher(f, attachmentDict.ContainsKey(f.Id) ? attachmentDict[f.Id] : null))
.ToList();
return _cachedCiphers;
} }
public async Task<IEnumerable<Cipher>> GetAllAsync(bool favorites) public async Task<IEnumerable<Cipher>> GetAllAsync(bool favorites)
@ -76,12 +85,11 @@ namespace Bit.App.Services
return null; return null;
} }
Uri uri = null;
string domainName = null; string domainName = null;
var mobileApp = UriIsMobileApp(uriString); var mobileApp = UriIsMobileApp(uriString);
if(!mobileApp && if(!mobileApp &&
(!Uri.TryCreate(uriString, UriKind.Absolute, out uri) || (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri uri) ||
!DomainName.TryParseBaseDomain(uri.Host, out domainName))) !DomainName.TryParseBaseDomain(uri.Host, out domainName)))
{ {
return null; return null;
@ -127,15 +135,15 @@ namespace Bit.App.Services
var matchingFuzzyDomainsArray = matchingFuzzyDomains.ToArray(); var matchingFuzzyDomainsArray = matchingFuzzyDomains.ToArray();
var matchingLogins = new List<Cipher>(); var matchingLogins = new List<Cipher>();
var matchingFuzzyLogins = new List<Cipher>(); var matchingFuzzyLogins = new List<Cipher>();
var logins = await _cipherRepository.GetAllByUserIdAsync(_authService.UserId); var ciphers = await GetAllAsync();
foreach(var login in logins) foreach(var cipher in ciphers)
{ {
if(string.IsNullOrWhiteSpace(login.Uri)) if(cipher.Type != Enums.CipherType.Login || cipher.Login?.Uri == null)
{ {
continue; continue;
} }
var loginUriString = new CipherString(login.Uri).Decrypt(login.OrganizationId); var loginUriString = cipher.Login.Uri.Decrypt(cipher.OrganizationId);
if(string.IsNullOrWhiteSpace(loginUriString)) if(string.IsNullOrWhiteSpace(loginUriString))
{ {
continue; continue;
@ -143,12 +151,12 @@ namespace Bit.App.Services
if(Array.IndexOf(matchingDomainsArray, loginUriString) >= 0) if(Array.IndexOf(matchingDomainsArray, loginUriString) >= 0)
{ {
matchingLogins.Add(new Cipher(login)); matchingLogins.Add(cipher);
continue; continue;
} }
else if(mobileApp && Array.IndexOf(matchingFuzzyDomainsArray, loginUriString) >= 0) else if(mobileApp && Array.IndexOf(matchingFuzzyDomainsArray, loginUriString) >= 0)
{ {
matchingFuzzyLogins.Add(new Cipher(login)); matchingFuzzyLogins.Add(cipher);
continue; continue;
} }
else if(!mobileApp) else if(!mobileApp)
@ -156,26 +164,25 @@ namespace Bit.App.Services
var info = InfoFromMobileAppUri(loginUriString); var info = InfoFromMobileAppUri(loginUriString);
if(info?.Item1 != null && Array.IndexOf(matchingDomainsArray, info.Item1) >= 0) if(info?.Item1 != null && Array.IndexOf(matchingDomainsArray, info.Item1) >= 0)
{ {
matchingFuzzyLogins.Add(new Cipher(login)); matchingFuzzyLogins.Add(cipher);
continue; continue;
} }
} }
Uri loginUri;
string loginDomainName = null; string loginDomainName = null;
if(Uri.TryCreate(loginUriString, UriKind.Absolute, out loginUri) if(Uri.TryCreate(loginUriString, UriKind.Absolute, out Uri loginUri)
&& DomainName.TryParseBaseDomain(loginUri.Host, out loginDomainName)) && DomainName.TryParseBaseDomain(loginUri.Host, out loginDomainName))
{ {
loginDomainName = loginDomainName.ToLowerInvariant(); loginDomainName = loginDomainName.ToLowerInvariant();
if(Array.IndexOf(matchingDomainsArray, loginDomainName) >= 0) if(Array.IndexOf(matchingDomainsArray, loginDomainName) >= 0)
{ {
matchingLogins.Add(new Cipher(login)); matchingLogins.Add(cipher);
continue; continue;
} }
else if(mobileApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName) >= 0) else if(mobileApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName) >= 0)
{ {
matchingFuzzyLogins.Add(new Cipher(login)); matchingFuzzyLogins.Add(cipher);
continue; continue;
} }
} }
@ -183,8 +190,8 @@ namespace Bit.App.Services
if(mobileApp && mobileAppSearchTerms != null && mobileAppSearchTerms.Length > 0) if(mobileApp && mobileAppSearchTerms != null && mobileAppSearchTerms.Length > 0)
{ {
var addedFromSearchTerm = false; var addedFromSearchTerm = false;
var loginNameString = login.Name == null ? null : var loginNameString = cipher.Name == null ? null :
new CipherString(login.Name).Decrypt(login.OrganizationId)?.ToLowerInvariant(); cipher.Name.Decrypt(cipher.OrganizationId)?.ToLowerInvariant();
foreach(var term in mobileAppSearchTerms) foreach(var term in mobileAppSearchTerms)
{ {
addedFromSearchTerm = (loginDomainName != null && loginDomainName.Contains(term)) || addedFromSearchTerm = (loginDomainName != null && loginDomainName.Contains(term)) ||
@ -197,7 +204,7 @@ namespace Bit.App.Services
if(addedFromSearchTerm) if(addedFromSearchTerm)
{ {
matchingFuzzyLogins.Add(new Cipher(login)); matchingFuzzyLogins.Add(cipher);
break; break;
} }
} }
@ -238,6 +245,8 @@ namespace Bit.App.Services
{ {
await _cipherRepository.UpdateAsync(data); await _cipherRepository.UpdateAsync(data);
} }
_cachedCiphers = null;
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized) || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
@ -254,6 +263,7 @@ namespace Bit.App.Services
if(response.Succeeded) if(response.Succeeded)
{ {
await _cipherRepository.DeleteAsync(id); await _cipherRepository.DeleteAsync(id);
_cachedCiphers = null;
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized) || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)