1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-27 12:26:31 +01:00

sync domain settings

This commit is contained in:
Kyle Spearrin 2017-02-08 22:04:07 -05:00
parent 2c1ebc0439
commit 2a1bd92e1a
7 changed files with 64 additions and 20 deletions

View File

@ -1,11 +1,10 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using Bit.App.Models.Api.Response;
namespace Bit.App.Abstractions namespace Bit.App.Abstractions
{ {
public interface ISettingsApiRepository public interface ISettingsApiRepository
{ {
Task<ApiResult<DomainsReponse>> GetDomains(bool excluded = false); Task<ApiResult<DomainsResponse>> GetDomains(bool excluded = false);
} }
} }

View File

@ -1,9 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.App.Models.Data;
namespace Bit.App.Abstractions namespace Bit.App.Abstractions
{ {
public interface ISettingsRepository public interface ISettingsRepository : IRepository<SettingsData, string>
{ {
Task<IEnumerable<IEnumerable<string>>> GetEquivablentDomains(string userId); Task<IEnumerable<IEnumerable<string>>> GetEquivablentDomains(string userId);
} }

View File

@ -108,7 +108,7 @@ namespace Bit.App
if(string.IsNullOrWhiteSpace(_uri)) if(string.IsNullOrWhiteSpace(_uri))
{ {
var lastBuild = _settings.GetValueOrDefault<string>(LastBuildKey); var lastBuild = _settings.GetValueOrDefault<string>(LastBuildKey);
if(lastBuild == null || lastBuild != _appInfoService.Build) if(InDebugMode() || lastBuild == null || lastBuild != _appInfoService.Build)
{ {
_settings.AddOrUpdateValue(LastBuildKey, _appInfoService.Build); _settings.AddOrUpdateValue(LastBuildKey, _appInfoService.Build);
_databaseService.CreateTables(); _databaseService.CreateTables();
@ -160,6 +160,15 @@ namespace Bit.App
} }
} }
private bool InDebugMode()
{
#if DEBUG
return true;
#else
return false;
#endif
}
private void SetMainPageFromAutofill() private void SetMainPageFromAutofill()
{ {
if(Device.OS != TargetPlatform.Android || string.IsNullOrWhiteSpace(_uri)) if(Device.OS != TargetPlatform.Android || string.IsNullOrWhiteSpace(_uri))

View File

@ -94,7 +94,7 @@
<Compile Include="Models\Api\Request\TokenRequest.cs" /> <Compile Include="Models\Api\Request\TokenRequest.cs" />
<Compile Include="Models\Api\Response\CipherHistoryResponse.cs" /> <Compile Include="Models\Api\Response\CipherHistoryResponse.cs" />
<Compile Include="Models\Api\Response\CipherResponse.cs" /> <Compile Include="Models\Api\Response\CipherResponse.cs" />
<Compile Include="Models\Api\Response\DomainsReponse.cs" /> <Compile Include="Models\Api\Response\DomainsResponse.cs" />
<Compile Include="Models\Api\Response\ErrorResponse.cs" /> <Compile Include="Models\Api\Response\ErrorResponse.cs" />
<Compile Include="Models\Api\Response\FolderResponse.cs" /> <Compile Include="Models\Api\Response\FolderResponse.cs" />
<Compile Include="Models\Api\Response\ListResponse.cs" /> <Compile Include="Models\Api\Response\ListResponse.cs" />

View File

@ -1,8 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace Bit.App.Models.Api.Response namespace Bit.App.Models.Api
{ {
public class DomainsReponse public class DomainsResponse
{ {
public IEnumerable<IEnumerable<string>> EquivalentDomains { get; set; } public IEnumerable<IEnumerable<string>> EquivalentDomains { get; set; }
public IEnumerable<GlobalDomains> GlobalEquivalentDomains { get; set; } public IEnumerable<GlobalDomains> GlobalEquivalentDomains { get; set; }

View File

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using Bit.App.Abstractions; using Bit.App.Abstractions;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Bit.App.Models.Api.Response;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Bit.App.Repositories namespace Bit.App.Repositories
@ -20,14 +19,14 @@ namespace Bit.App.Repositories
protected override string ApiRoute => "settings"; protected override string ApiRoute => "settings";
public virtual async Task<ApiResult<DomainsReponse>> GetDomains(bool excluded = false) public virtual async Task<ApiResult<DomainsResponse>> GetDomains(bool excluded = false)
{ {
if(!Connectivity.IsConnected) if(!Connectivity.IsConnected)
{ {
return HandledNotConnected<DomainsReponse>(); return HandledNotConnected<DomainsResponse>();
} }
var tokenStateResponse = await HandleTokenStateAsync<DomainsReponse>(); var tokenStateResponse = await HandleTokenStateAsync<DomainsResponse>();
if(!tokenStateResponse.Succeeded) if(!tokenStateResponse.Succeeded)
{ {
return tokenStateResponse; return tokenStateResponse;
@ -47,16 +46,16 @@ namespace Bit.App.Repositories
var response = await client.SendAsync(requestMessage).ConfigureAwait(false); var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode) if(!response.IsSuccessStatusCode)
{ {
return await HandleErrorAsync<DomainsReponse>(response).ConfigureAwait(false); return await HandleErrorAsync<DomainsResponse>(response).ConfigureAwait(false);
} }
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<DomainsReponse>(responseContent); var responseObj = JsonConvert.DeserializeObject<DomainsResponse>(responseContent);
return ApiResult<DomainsReponse>.Success(responseObj, response.StatusCode); return ApiResult<DomainsResponse>.Success(responseObj, response.StatusCode);
} }
catch catch
{ {
return HandledWebException<DomainsReponse>(); return HandledWebException<DomainsResponse>();
} }
} }
} }

View File

@ -7,6 +7,7 @@ using Plugin.Settings.Abstractions;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using System.Collections.Generic; using System.Collections.Generic;
using Xamarin.Forms; using Xamarin.Forms;
using Newtonsoft.Json;
namespace Bit.App.Services namespace Bit.App.Services
{ {
@ -16,8 +17,10 @@ namespace Bit.App.Services
private readonly IFolderApiRepository _folderApiRepository; private readonly IFolderApiRepository _folderApiRepository;
private readonly ILoginApiRepository _loginApiRepository; private readonly ILoginApiRepository _loginApiRepository;
private readonly IAccountsApiRepository _accountsApiRepository; private readonly IAccountsApiRepository _accountsApiRepository;
private readonly ISettingsApiRepository _settingsApiRepository;
private readonly IFolderRepository _folderRepository; private readonly IFolderRepository _folderRepository;
private readonly ILoginRepository _loginRepository; private readonly ILoginRepository _loginRepository;
private readonly ISettingsRepository _settingsRepository;
private readonly IAuthService _authService; private readonly IAuthService _authService;
private readonly ISettings _settings; private readonly ISettings _settings;
@ -26,8 +29,10 @@ namespace Bit.App.Services
IFolderApiRepository folderApiRepository, IFolderApiRepository folderApiRepository,
ILoginApiRepository loginApiRepository, ILoginApiRepository loginApiRepository,
IAccountsApiRepository accountsApiRepository, IAccountsApiRepository accountsApiRepository,
ISettingsApiRepository settingsApiRepository,
IFolderRepository folderRepository, IFolderRepository folderRepository,
ILoginRepository loginRepository, ILoginRepository loginRepository,
ISettingsRepository settingsRepository,
IAuthService authService, IAuthService authService,
ISettings settings) ISettings settings)
{ {
@ -35,8 +40,10 @@ namespace Bit.App.Services
_folderApiRepository = folderApiRepository; _folderApiRepository = folderApiRepository;
_loginApiRepository = loginApiRepository; _loginApiRepository = loginApiRepository;
_accountsApiRepository = accountsApiRepository; _accountsApiRepository = accountsApiRepository;
_settingsApiRepository = settingsApiRepository;
_folderRepository = folderRepository; _folderRepository = folderRepository;
_loginRepository = loginRepository; _loginRepository = loginRepository;
_settingsRepository = settingsRepository;
_authService = authService; _authService = authService;
_settings = settings; _settings = settings;
} }
@ -157,12 +164,20 @@ namespace Bit.App.Services
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var ciphers = await _cipherApiRepository.GetAsync().ConfigureAwait(false); 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); SyncCompleted(false);
if(Application.Current == null)
{
return false;
}
if(Application.Current != null && (ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden ||
|| ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized)) 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); MessagingCenter.Send(Application.Current, "Logout", (string)null);
} }
@ -175,9 +190,10 @@ namespace Bit.App.Services
var loginTask = SyncLoginsAsync(logins, true); var loginTask = SyncLoginsAsync(logins, true);
var folderTask = SyncFoldersAsync(folders, 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); SyncCompleted(false);
return false; return false;
@ -294,6 +310,26 @@ namespace Bit.App.Services
} }
} }
private async Task SyncDomainsAsync(DomainsResponse serverDomains)
{
var eqDomains = new List<IEnumerable<string>>();
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() private void SyncStarted()
{ {
if(Application.Current == null) if(Application.Current == null)