diff --git a/src/App/Abstractions/Repositories/ISettingsApiRepository.cs b/src/App/Abstractions/Repositories/ISettingsApiRepository.cs index b106d9ad8..5de8653ab 100644 --- a/src/App/Abstractions/Repositories/ISettingsApiRepository.cs +++ b/src/App/Abstractions/Repositories/ISettingsApiRepository.cs @@ -1,11 +1,10 @@ using System.Threading.Tasks; using Bit.App.Models.Api; -using Bit.App.Models.Api.Response; namespace Bit.App.Abstractions { public interface ISettingsApiRepository { - Task> GetDomains(bool excluded = false); + Task> GetDomains(bool excluded = false); } } \ No newline at end of file diff --git a/src/App/Abstractions/Repositories/ISettingsRepository.cs b/src/App/Abstractions/Repositories/ISettingsRepository.cs index e3f9b8c18..d0871a87c 100644 --- a/src/App/Abstractions/Repositories/ISettingsRepository.cs +++ b/src/App/Abstractions/Repositories/ISettingsRepository.cs @@ -1,9 +1,10 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Bit.App.Models.Data; namespace Bit.App.Abstractions { - public interface ISettingsRepository + public interface ISettingsRepository : IRepository { Task>> GetEquivablentDomains(string userId); } diff --git a/src/App/App.cs b/src/App/App.cs index 142864122..1f1a71698 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -108,7 +108,7 @@ namespace Bit.App if(string.IsNullOrWhiteSpace(_uri)) { var lastBuild = _settings.GetValueOrDefault(LastBuildKey); - if(lastBuild == null || lastBuild != _appInfoService.Build) + if(InDebugMode() || lastBuild == null || lastBuild != _appInfoService.Build) { _settings.AddOrUpdateValue(LastBuildKey, _appInfoService.Build); _databaseService.CreateTables(); @@ -160,6 +160,15 @@ namespace Bit.App } } + private bool InDebugMode() + { +#if DEBUG + return true; +#else + return false; +#endif + } + private void SetMainPageFromAutofill() { if(Device.OS != TargetPlatform.Android || string.IsNullOrWhiteSpace(_uri)) diff --git a/src/App/App.csproj b/src/App/App.csproj index 4c7028627..ed2bfcc3e 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -94,7 +94,7 @@ - + diff --git a/src/App/Models/Api/Response/DomainsReponse.cs b/src/App/Models/Api/Response/DomainsResponse.cs similarity index 85% rename from src/App/Models/Api/Response/DomainsReponse.cs rename to src/App/Models/Api/Response/DomainsResponse.cs index 61e77881b..2e68c38cb 100644 --- a/src/App/Models/Api/Response/DomainsReponse.cs +++ b/src/App/Models/Api/Response/DomainsResponse.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; -namespace Bit.App.Models.Api.Response +namespace Bit.App.Models.Api { - public class DomainsReponse + public class DomainsResponse { public IEnumerable> EquivalentDomains { get; set; } public IEnumerable GlobalEquivalentDomains { get; set; } diff --git a/src/App/Repositories/SettingsApiRepository.cs b/src/App/Repositories/SettingsApiRepository.cs index 3e2636d13..94d50f3e3 100644 --- a/src/App/Repositories/SettingsApiRepository.cs +++ b/src/App/Repositories/SettingsApiRepository.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Bit.App.Abstractions; using Bit.App.Models.Api; using Plugin.Connectivity.Abstractions; -using Bit.App.Models.Api.Response; using Newtonsoft.Json; namespace Bit.App.Repositories @@ -20,14 +19,14 @@ namespace Bit.App.Repositories protected override string ApiRoute => "settings"; - public virtual async Task> GetDomains(bool excluded = false) + public virtual async Task> GetDomains(bool excluded = false) { if(!Connectivity.IsConnected) { - return HandledNotConnected(); + return HandledNotConnected(); } - var tokenStateResponse = await HandleTokenStateAsync(); + var tokenStateResponse = await HandleTokenStateAsync(); if(!tokenStateResponse.Succeeded) { return tokenStateResponse; @@ -47,16 +46,16 @@ namespace Bit.App.Repositories var response = await client.SendAsync(requestMessage).ConfigureAwait(false); if(!response.IsSuccessStatusCode) { - return await HandleErrorAsync(response).ConfigureAwait(false); + return await HandleErrorAsync(response).ConfigureAwait(false); } var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - var responseObj = JsonConvert.DeserializeObject(responseContent); - return ApiResult.Success(responseObj, response.StatusCode); + var responseObj = JsonConvert.DeserializeObject(responseContent); + return ApiResult.Success(responseObj, response.StatusCode); } catch { - return HandledWebException(); + return HandledWebException(); } } } diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index 183dde87e..40ad719a8 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -7,6 +7,7 @@ using Plugin.Settings.Abstractions; using Bit.App.Models.Api; using System.Collections.Generic; using Xamarin.Forms; +using Newtonsoft.Json; namespace Bit.App.Services { @@ -16,8 +17,10 @@ namespace Bit.App.Services private readonly IFolderApiRepository _folderApiRepository; private readonly ILoginApiRepository _loginApiRepository; private readonly IAccountsApiRepository _accountsApiRepository; + private readonly ISettingsApiRepository _settingsApiRepository; private readonly IFolderRepository _folderRepository; private readonly ILoginRepository _loginRepository; + private readonly ISettingsRepository _settingsRepository; private readonly IAuthService _authService; private readonly ISettings _settings; @@ -26,8 +29,10 @@ namespace Bit.App.Services IFolderApiRepository folderApiRepository, ILoginApiRepository loginApiRepository, IAccountsApiRepository accountsApiRepository, + ISettingsApiRepository settingsApiRepository, IFolderRepository folderRepository, ILoginRepository loginRepository, + ISettingsRepository settingsRepository, IAuthService authService, ISettings settings) { @@ -35,8 +40,10 @@ namespace Bit.App.Services _folderApiRepository = folderApiRepository; _loginApiRepository = loginApiRepository; _accountsApiRepository = accountsApiRepository; + _settingsApiRepository = settingsApiRepository; _folderRepository = folderRepository; _loginRepository = loginRepository; + _settingsRepository = settingsRepository; _authService = authService; _settings = settings; } @@ -157,12 +164,20 @@ namespace Bit.App.Services var now = DateTime.UtcNow; var ciphers = await _cipherApiRepository.GetAsync().ConfigureAwait(false); - if(!ciphers.Succeeded) + var domains = await _settingsApiRepository.GetDomains(false).ConfigureAwait(false); + + if(!ciphers.Succeeded || !domains.Succeeded) { SyncCompleted(false); + if(Application.Current == null) + { + return false; + } - if(Application.Current != null && (ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden - || ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized)) + if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden || + ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized || + domains.StatusCode == System.Net.HttpStatusCode.Forbidden || + domains.StatusCode == System.Net.HttpStatusCode.Unauthorized) { MessagingCenter.Send(Application.Current, "Logout", (string)null); } @@ -175,9 +190,10 @@ namespace Bit.App.Services var loginTask = SyncLoginsAsync(logins, true); var folderTask = SyncFoldersAsync(folders, true); - await Task.WhenAll(loginTask, folderTask).ConfigureAwait(false); + var domainsTask = SyncDomainsAsync(domains.Result); + await Task.WhenAll(loginTask, folderTask, domainsTask).ConfigureAwait(false); - if(folderTask.Exception != null || loginTask.Exception != null) + if(folderTask.Exception != null || loginTask.Exception != null || domainsTask.Exception != null) { SyncCompleted(false); return false; @@ -294,6 +310,26 @@ namespace Bit.App.Services } } + private async Task SyncDomainsAsync(DomainsResponse serverDomains) + { + var eqDomains = new List>(); + if(serverDomains.EquivalentDomains != null) + { + eqDomains.AddRange(serverDomains.EquivalentDomains); + } + + if(serverDomains.GlobalEquivalentDomains != null) + { + eqDomains.AddRange(serverDomains.GlobalEquivalentDomains.Select(d => d.Domains)); + } + + await _settingsRepository.UpsertAsync(new SettingsData + { + Id = _authService.UserId, + EquivalentDomains = JsonConvert.SerializeObject(eqDomains) + }); + } + private void SyncStarted() { if(Application.Current == null)