new push notification changes and syncing

This commit is contained in:
Kyle Spearrin 2017-04-21 14:57:23 -04:00
parent 1be4f6e20c
commit 439370e25a
7 changed files with 127 additions and 14 deletions

View File

@ -12,6 +12,7 @@ namespace Bit.App.Abstractions
string Email { get; set; }
string PIN { get; set; }
bool BelongsToOrganization(string orgId);
void LogOut();
Task<FullLoginResult> TokenPostAsync(string email, string masterPassword);
Task<LoginResult> TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, CryptoKey key);

View File

@ -7,8 +7,10 @@ namespace Bit.App.Abstractions
{
bool SyncInProgress { get; }
Task<bool> SyncCipherAsync(string id);
Task<bool> SyncFolderAsync(string id);
Task<bool> SyncDeleteFolderAsync(string id, DateTime revisionDate);
Task<bool> SyncDeleteLoginAsync(string id);
Task<bool> SyncSettingsAsync();
Task<bool> FullSyncAsync(bool forceSync = false);
Task<bool> FullSyncAsync(TimeSpan syncThreshold, bool forceSync = false);
}

View File

@ -6,6 +6,13 @@
SyncCipherCreate = 1,
SyncLoginDelete = 2,
SyncFolderDelete = 3,
SyncCiphers = 4
SyncCiphers = 4,
SyncVault = 5,
SyncOrgKeys = 6,
SyncFolderCreate = 7,
SyncFolderUpdate = 8,
SyncCipherDelete = 9,
SyncSettings = 10
}
}

View File

@ -8,19 +8,24 @@ namespace Bit.App.Models
public PushType Type { get; set; }
}
public abstract class SyncPushNotification : PushNotification
{
public string UserId { get; set; }
}
public class SyncCipherPushNotification : SyncPushNotification
public class SyncCipherPushNotification : PushNotification
{
public string Id { get; set; }
public string UserId { get; set; }
public string OrganizationId { get; set; }
public DateTime RevisionDate { get; set; }
}
public class SyncCiphersPushNotification : SyncPushNotification
public class SyncFolderPushNotification : PushNotification
{
public string Id { get; set; }
public string UserId { get; set; }
public DateTime RevisionDate { get; set; }
}
public class SyncUserPushNotification : PushNotification
{
public string UserId { get; set; }
public DateTime Date { get; set; }
}
}

View File

@ -191,6 +191,11 @@ namespace Bit.App.Services
}
}
public bool BelongsToOrganization(string orgId)
{
return !string.IsNullOrWhiteSpace(orgId) && (_cryptoService.OrgKeys?.ContainsKey(orgId) ?? false);
}
public void LogOut()
{
_tokenService.Token = null;

View File

@ -59,15 +59,28 @@ namespace Bit.App.Services
{
case Enums.PushType.SyncCipherUpdate:
case Enums.PushType.SyncCipherCreate:
var createUpdateMessage = values.ToObject<SyncCipherPushNotification>();
if(createUpdateMessage.UserId != _authService.UserId)
var cipherCreateUpdateMessage = values.ToObject<SyncCipherPushNotification>();
if(cipherCreateUpdateMessage.UserId != null && cipherCreateUpdateMessage.UserId != _authService.UserId)
{
break;
}
_syncService.SyncCipherAsync(createUpdateMessage.Id);
else if(!_authService.BelongsToOrganization(cipherCreateUpdateMessage.OrganizationId))
{
break;
}
_syncService.SyncCipherAsync(cipherCreateUpdateMessage.Id);
break;
case Enums.PushType.SyncFolderUpdate:
case Enums.PushType.SyncFolderCreate:
var folderCreateUpdateMessage = values.ToObject<SyncFolderPushNotification>();
if(folderCreateUpdateMessage.UserId != _authService.UserId)
{
break;
}
_syncService.SyncFolderAsync(folderCreateUpdateMessage.Id);
break;
case Enums.PushType.SyncFolderDelete:
var folderDeleteMessage = values.ToObject<SyncCipherPushNotification>();
var folderDeleteMessage = values.ToObject<SyncFolderPushNotification>();
if(folderDeleteMessage.UserId != _authService.UserId)
{
break;
@ -76,20 +89,33 @@ namespace Bit.App.Services
break;
case Enums.PushType.SyncLoginDelete:
var loginDeleteMessage = values.ToObject<SyncCipherPushNotification>();
if(loginDeleteMessage.UserId != _authService.UserId)
if(loginDeleteMessage.UserId != null && loginDeleteMessage.UserId != _authService.UserId)
{
break;
}
else if(!_authService.BelongsToOrganization(loginDeleteMessage.OrganizationId))
{
break;
}
_syncService.SyncDeleteLoginAsync(loginDeleteMessage.Id);
break;
case Enums.PushType.SyncCiphers:
var cipherMessage = values.ToObject<SyncCiphersPushNotification>();
case Enums.PushType.SyncVault:
var cipherMessage = values.ToObject<SyncUserPushNotification>();
if(cipherMessage.UserId != _authService.UserId)
{
break;
}
_syncService.FullSyncAsync(true);
break;
case Enums.PushType.SyncSettings:
var domainMessage = values.ToObject<SyncUserPushNotification>();
if(domainMessage.UserId != _authService.UserId)
{
break;
}
_syncService.SyncSettingsAsync();
break;
default:
break;
}

View File

@ -96,6 +96,44 @@ namespace Bit.App.Services
return true;
}
public async Task<bool> SyncFolderAsync(string id)
{
if(!_authService.IsAuthenticated)
{
return false;
}
SyncStarted();
var folder = await _folderApiRepository.GetByIdAsync(id).ConfigureAwait(false);
if(!folder.Succeeded)
{
SyncCompleted(false);
if(Application.Current != null && (folder.StatusCode == System.Net.HttpStatusCode.Forbidden
|| folder.StatusCode == System.Net.HttpStatusCode.Unauthorized))
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return false;
}
try
{
var folderData = new FolderData(folder.Result, _authService.UserId);
await _folderRepository.UpsertAsync(folderData).ConfigureAwait(false);
}
catch(SQLite.SQLiteException)
{
SyncCompleted(false);
return false;
}
SyncCompleted(true);
return true;
}
public async Task<bool> SyncDeleteFolderAsync(string id, DateTime revisionDate)
{
if(!_authService.IsAuthenticated)
@ -140,6 +178,35 @@ namespace Bit.App.Services
}
}
public async Task<bool> SyncSettingsAsync()
{
if(!_authService.IsAuthenticated)
{
return false;
}
SyncStarted();
var domains = await _settingsApiRepository.GetDomains(false).ConfigureAwait(false);
if(!domains.Succeeded)
{
SyncCompleted(false);
if(Application.Current != null && (domains.StatusCode == System.Net.HttpStatusCode.Forbidden
|| domains.StatusCode == System.Net.HttpStatusCode.Unauthorized))
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return false;
}
await SyncDomainsAsync(domains.Result);
SyncCompleted(true);
return true;
}
public async Task<bool> FullSyncAsync(TimeSpan syncThreshold, bool forceSync = false)
{
DateTime? lastSync = _settings.GetValueOrDefault<DateTime?>(Constants.LastSync, null);