mirror of
https://github.com/bitwarden/mobile
synced 2025-12-11 05:43:30 +00:00
* [EC-426] Add watchOS PoC app (#2054) * EC-426 Added watchOS app, configured iOS.csproj to bundle the output of XCode build into the Xamarin iOS app and added some custom logic to use WCSession to communicate between the iOS and the watchOS apps * EC-426 Removed Info.plist from iOS.Core project given that it's not needed * [EC-426] Added new encrypted watch app profiles * EC-426 added configuration for building watchApp and bundle it up on the iOS one * EC-426 Fix build for watchOS * EC-426 Fix build for watchOS applied shell bash * EC-426 Fix build for watchOS echo * EC-426 Fix build for watchOS simplify * EC-426 Fix build for watchOS added workspace path * EC-426 Changed code sign identity of watchOS project to Apple Distribution * EC-426 added manual code sign style and specified the provisioning profile for the targets on the watch xcode project * EC-426 updated path to watchOS on release on iOS.csproj and disabled android and f-.droid * EC-426 fix build * EC-426 fix path and check listing of directory of watchOS output just in case * EC-426 Fix Apple Watch build to list the folder recursively just in case we need to change the path for the watch bundle * EC-426 TEMP Change texts on input on login and lock to show that the app is for the Watch PoC testing * EC-426 Fix WatchApp build path * EC-426 Added WatchOS AppIcons * EC-426 added gitignore for XCode project removed files supposed to be ignored * EC-426 Cleaned the code a bit to avoid misbehavior * EC-426 Code cleanup Co-authored-by: Joseph Flinn <joseph.s.flinn@gmail.com> * [EC-585] Added data, encryption and some helpers and structure to the Watch app (#2164) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * [EC-614] Apple Watch MVP Cipher list UI (#2175) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * EC-614 Implemented watch ciphers list UI * [EC-615] Apple Watch MVP Cipher details UI (#2192) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * EC-614 Implemented watch ciphers list UI * EC-615 Added cipher details UI to watch and also implemented logic and helpers to generate the TOTPs * EC-615 Added value transformer to login uris on the cipher entity * EC-617 Added state view on watch app and some state helpers and wired it on the CipherListView. Also added some images (#2195) * [EC-581] Implement Apple Watch MVP Sync (#2206) * EC-581 Implemented sync iPhone -> watchOS, fix some issues with the watch database and sync flows for login/locks/multiple accounts * EC-581 Added watch sync on unlocking and need setup state when no user is synced and the session is not active * EC-581 Removed unused method * EC-581 Fix format * EC-759 Added avatar row on cipher list header to display avatar icon and email (#2213) * [EC-786] Apple Watch MVP Sync fixes (#2214) * EC-786 Commented things that are not going to be included on the MVP and fixed issue on the dictionary sent on the applicationContext to have a changing key based on time * EC-786 Commented need unlock state * EC-579 Added logic for Connect To Watch on iOS settings and moved it to the correct place. Also improved the synchronization and watch session activation logic (#2218) * EC-616 Added search header for ciphers and polished the code (#2226) Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> Co-authored-by: Joseph Flinn <joseph.s.flinn@gmail.com>
434 lines
17 KiB
C#
434 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Bit.Core.Abstractions;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Models.Response;
|
|
using Bit.Core.Utilities;
|
|
|
|
namespace Bit.Core.Services
|
|
{
|
|
public class SyncService : ISyncService
|
|
{
|
|
private readonly IStateService _stateService;
|
|
private readonly IApiService _apiService;
|
|
private readonly ISettingsService _settingsService;
|
|
private readonly IFolderService _folderService;
|
|
private readonly ICipherService _cipherService;
|
|
private readonly ICryptoService _cryptoService;
|
|
private readonly ICollectionService _collectionService;
|
|
private readonly IOrganizationService _organizationService;
|
|
private readonly IMessagingService _messagingService;
|
|
private readonly IPolicyService _policyService;
|
|
private readonly ISendService _sendService;
|
|
private readonly IKeyConnectorService _keyConnectorService;
|
|
private readonly ILogger _logger;
|
|
private readonly Func<Tuple<string, bool, bool>, Task> _logoutCallbackAsync;
|
|
|
|
private readonly LazyResolve<IWatchDeviceService> _watchDeviceService = new LazyResolve<IWatchDeviceService>();
|
|
|
|
public SyncService(
|
|
IStateService stateService,
|
|
IApiService apiService,
|
|
ISettingsService settingsService,
|
|
IFolderService folderService,
|
|
ICipherService cipherService,
|
|
ICryptoService cryptoService,
|
|
ICollectionService collectionService,
|
|
IOrganizationService organizationService,
|
|
IMessagingService messagingService,
|
|
IPolicyService policyService,
|
|
ISendService sendService,
|
|
IKeyConnectorService keyConnectorService,
|
|
ILogger logger,
|
|
Func<Tuple<string, bool, bool>, Task> logoutCallbackAsync)
|
|
{
|
|
_stateService = stateService;
|
|
_apiService = apiService;
|
|
_settingsService = settingsService;
|
|
_folderService = folderService;
|
|
_cipherService = cipherService;
|
|
_cryptoService = cryptoService;
|
|
_collectionService = collectionService;
|
|
_organizationService = organizationService;
|
|
_messagingService = messagingService;
|
|
_policyService = policyService;
|
|
_sendService = sendService;
|
|
_keyConnectorService = keyConnectorService;
|
|
_logger = logger;
|
|
_logoutCallbackAsync = logoutCallbackAsync;
|
|
}
|
|
|
|
public bool SyncInProgress { get; set; }
|
|
|
|
public async Task<DateTime?> GetLastSyncAsync()
|
|
{
|
|
if (await _stateService.GetActiveUserIdAsync() == null)
|
|
{
|
|
return null;
|
|
}
|
|
return await _stateService.GetLastSyncAsync();
|
|
}
|
|
|
|
public async Task SetLastSyncAsync(DateTime date)
|
|
{
|
|
if (await _stateService.GetActiveUserIdAsync() == null)
|
|
{
|
|
return;
|
|
}
|
|
await _stateService.SetLastSyncAsync(date);
|
|
}
|
|
|
|
public async Task<bool> FullSyncAsync(bool forceSync, bool allowThrowOnError = false)
|
|
{
|
|
SyncStarted();
|
|
var isAuthenticated = await _stateService.IsAuthenticatedAsync();
|
|
if (!isAuthenticated)
|
|
{
|
|
return SyncCompleted(false);
|
|
}
|
|
var now = DateTime.UtcNow;
|
|
var needsSyncResult = await NeedsSyncingAsync(forceSync);
|
|
var needsSync = needsSyncResult.Item1;
|
|
var skipped = needsSyncResult.Item2;
|
|
if (skipped)
|
|
{
|
|
return SyncCompleted(false);
|
|
}
|
|
if (!needsSync)
|
|
{
|
|
await SetLastSyncAsync(now);
|
|
return SyncCompleted(false);
|
|
}
|
|
var userId = await _stateService.GetActiveUserIdAsync();
|
|
try
|
|
{
|
|
var response = await _apiService.GetSyncAsync();
|
|
await SyncProfileAsync(response.Profile);
|
|
await SyncFoldersAsync(userId, response.Folders);
|
|
await SyncCollectionsAsync(response.Collections);
|
|
await SyncCiphersAsync(userId, response.Ciphers);
|
|
await SyncSettingsAsync(userId, response.Domains);
|
|
await SyncPoliciesAsync(response.Policies);
|
|
await SyncSendsAsync(userId, response.Sends);
|
|
await SetLastSyncAsync(now);
|
|
_watchDeviceService.Value.SyncDataToWatchAsync().FireAndForget();
|
|
|
|
return SyncCompleted(true);
|
|
}
|
|
catch
|
|
{
|
|
if (allowThrowOnError)
|
|
{
|
|
throw;
|
|
}
|
|
else
|
|
{
|
|
return SyncCompleted(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<bool> SyncUpsertFolderAsync(SyncFolderNotification notification, bool isEdit)
|
|
{
|
|
SyncStarted();
|
|
if (await _stateService.IsAuthenticatedAsync())
|
|
{
|
|
try
|
|
{
|
|
var localFolder = await _folderService.GetAsync(notification.Id);
|
|
if ((!isEdit && localFolder == null) ||
|
|
(isEdit && localFolder != null && localFolder.RevisionDate < notification.RevisionDate))
|
|
{
|
|
var remoteFolder = await _apiService.GetFolderAsync(notification.Id);
|
|
if (remoteFolder != null)
|
|
{
|
|
var userId = await _stateService.GetActiveUserIdAsync();
|
|
await _folderService.UpsertAsync(new FolderData(remoteFolder, userId));
|
|
_messagingService.Send("syncedUpsertedFolder", new Dictionary<string, string>
|
|
{
|
|
["folderId"] = notification.Id
|
|
});
|
|
return SyncCompleted(true);
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
return SyncCompleted(false);
|
|
}
|
|
|
|
public async Task<bool> SyncDeleteFolderAsync(SyncFolderNotification notification)
|
|
{
|
|
SyncStarted();
|
|
if (await _stateService.IsAuthenticatedAsync())
|
|
{
|
|
await _folderService.DeleteAsync(notification.Id);
|
|
_messagingService.Send("syncedDeletedFolder", new Dictionary<string, string>
|
|
{
|
|
["folderId"] = notification.Id
|
|
});
|
|
return SyncCompleted(true);
|
|
}
|
|
return SyncCompleted(false);
|
|
}
|
|
|
|
public async Task<bool> SyncUpsertCipherAsync(SyncCipherNotification notification, bool isEdit)
|
|
{
|
|
SyncStarted();
|
|
if (await _stateService.IsAuthenticatedAsync())
|
|
{
|
|
try
|
|
{
|
|
var shouldUpdate = true;
|
|
var localCipher = await _cipherService.GetAsync(notification.Id);
|
|
if (localCipher != null && localCipher.RevisionDate >= notification.RevisionDate)
|
|
{
|
|
shouldUpdate = false;
|
|
}
|
|
|
|
var checkCollections = false;
|
|
if (shouldUpdate)
|
|
{
|
|
if (isEdit)
|
|
{
|
|
shouldUpdate = localCipher != null;
|
|
checkCollections = true;
|
|
}
|
|
else
|
|
{
|
|
if (notification.CollectionIds == null || notification.OrganizationId == null)
|
|
{
|
|
shouldUpdate = localCipher == null;
|
|
}
|
|
else
|
|
{
|
|
shouldUpdate = false;
|
|
checkCollections = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!shouldUpdate && checkCollections && notification.OrganizationId != null &&
|
|
notification.CollectionIds != null && notification.CollectionIds.Any())
|
|
{
|
|
var collections = await _collectionService.GetAllAsync();
|
|
if (collections != null)
|
|
{
|
|
foreach (var c in collections)
|
|
{
|
|
if (notification.CollectionIds.Contains(c.Id))
|
|
{
|
|
shouldUpdate = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (shouldUpdate)
|
|
{
|
|
var remoteCipher = await _apiService.GetCipherAsync(notification.Id);
|
|
if (remoteCipher != null)
|
|
{
|
|
var userId = await _stateService.GetActiveUserIdAsync();
|
|
await _cipherService.UpsertAsync(new CipherData(remoteCipher, userId));
|
|
_messagingService.Send("syncedUpsertedCipher", new Dictionary<string, string>
|
|
{
|
|
["cipherId"] = notification.Id
|
|
});
|
|
return SyncCompleted(true);
|
|
}
|
|
}
|
|
}
|
|
catch (ApiException e)
|
|
{
|
|
if (e.Error != null && e.Error.StatusCode == System.Net.HttpStatusCode.NotFound && isEdit)
|
|
{
|
|
await _cipherService.DeleteAsync(notification.Id);
|
|
_messagingService.Send("syncedDeletedCipher", new Dictionary<string, string>
|
|
{
|
|
["cipherId"] = notification.Id
|
|
});
|
|
return SyncCompleted(true);
|
|
}
|
|
}
|
|
}
|
|
return SyncCompleted(false);
|
|
}
|
|
|
|
public async Task<bool> SyncDeleteCipherAsync(SyncCipherNotification notification)
|
|
{
|
|
SyncStarted();
|
|
if (await _stateService.IsAuthenticatedAsync())
|
|
{
|
|
await _cipherService.DeleteAsync(notification.Id);
|
|
_messagingService.Send("syncedDeletedCipher", new Dictionary<string, string>
|
|
{
|
|
["cipherId"] = notification.Id
|
|
});
|
|
return SyncCompleted(true);
|
|
}
|
|
return SyncCompleted(false);
|
|
}
|
|
|
|
// Helpers
|
|
|
|
private void SyncStarted()
|
|
{
|
|
SyncInProgress = true;
|
|
_messagingService.Send("syncStarted");
|
|
}
|
|
|
|
private bool SyncCompleted(bool successfully)
|
|
{
|
|
SyncInProgress = false;
|
|
_messagingService.Send("syncCompleted", new Dictionary<string, object> { ["successfully"] = successfully });
|
|
return successfully;
|
|
}
|
|
|
|
private async Task<Tuple<bool, bool>> NeedsSyncingAsync(bool forceSync)
|
|
{
|
|
if (forceSync)
|
|
{
|
|
return new Tuple<bool, bool>(true, false);
|
|
}
|
|
var lastSync = await GetLastSyncAsync();
|
|
if (lastSync == null || lastSync == DateTime.MinValue)
|
|
{
|
|
return new Tuple<bool, bool>(true, false);
|
|
}
|
|
try
|
|
{
|
|
var response = await _apiService.GetAccountRevisionDateAsync();
|
|
var d = CoreHelpers.Epoc.AddMilliseconds(response);
|
|
if (d <= lastSync.Value)
|
|
{
|
|
return new Tuple<bool, bool>(false, false);
|
|
}
|
|
return new Tuple<bool, bool>(true, false);
|
|
}
|
|
catch
|
|
{
|
|
return new Tuple<bool, bool>(false, true);
|
|
}
|
|
}
|
|
|
|
private async Task SyncProfileAsync(ProfileResponse response)
|
|
{
|
|
var stamp = await _stateService.GetSecurityStampAsync();
|
|
if (stamp != null && stamp != response.SecurityStamp)
|
|
{
|
|
if (_logoutCallbackAsync != null)
|
|
{
|
|
await _logoutCallbackAsync(new Tuple<string, bool, bool>(response.Id, false, true));
|
|
}
|
|
return;
|
|
}
|
|
await _cryptoService.SetEncKeyAsync(response.Key);
|
|
await _cryptoService.SetEncPrivateKeyAsync(response.PrivateKey);
|
|
await _cryptoService.SetOrgKeysAsync(response.Organizations);
|
|
await _stateService.SetSecurityStampAsync(response.SecurityStamp);
|
|
var organizations = response.Organizations.ToDictionary(o => o.Id, o => new OrganizationData(o));
|
|
await _organizationService.ReplaceAsync(organizations);
|
|
await _stateService.SetEmailVerifiedAsync(response.EmailVerified);
|
|
await _stateService.SetNameAsync(response.Name);
|
|
await _keyConnectorService.SetUsesKeyConnector(response.UsesKeyConnector);
|
|
}
|
|
|
|
private async Task SyncFoldersAsync(string userId, List<FolderResponse> response)
|
|
{
|
|
var folders = response.ToDictionary(f => f.Id, f => new FolderData(f, userId));
|
|
await _folderService.ReplaceAsync(folders);
|
|
}
|
|
|
|
private async Task SyncCollectionsAsync(List<CollectionDetailsResponse> response)
|
|
{
|
|
var collections = response.ToDictionary(c => c.Id, c => new CollectionData(c));
|
|
await _collectionService.ReplaceAsync(collections);
|
|
}
|
|
|
|
private async Task SyncCiphersAsync(string userId, List<CipherResponse> response)
|
|
{
|
|
var ciphers = response.ToDictionary(c => c.Id, c => new CipherData(c, userId));
|
|
await _cipherService.ReplaceAsync(ciphers);
|
|
}
|
|
|
|
private async Task SyncSettingsAsync(string userId, DomainsResponse response)
|
|
{
|
|
var eqDomains = new List<List<string>>();
|
|
if (response != null && response.EquivalentDomains != null)
|
|
{
|
|
eqDomains = eqDomains.Concat(response.EquivalentDomains).ToList();
|
|
}
|
|
if (response != null && response.GlobalEquivalentDomains != null)
|
|
{
|
|
foreach (var global in response.GlobalEquivalentDomains)
|
|
{
|
|
if (global.Domains.Any())
|
|
{
|
|
eqDomains.Add(global.Domains);
|
|
}
|
|
}
|
|
}
|
|
await _settingsService.SetEquivalentDomainsAsync(eqDomains);
|
|
}
|
|
|
|
private async Task SyncPoliciesAsync(List<PolicyResponse> response)
|
|
{
|
|
var policies = response?.ToDictionary(p => p.Id, p => new PolicyData(p)) ??
|
|
new Dictionary<string, PolicyData>();
|
|
await _policyService.Replace(policies);
|
|
}
|
|
|
|
private async Task SyncSendsAsync(string userId, List<SendResponse> response)
|
|
{
|
|
var sends = response?.ToDictionary(s => s.Id, s => new SendData(s, userId)) ??
|
|
new Dictionary<string, SendData>();
|
|
await _sendService.ReplaceAsync(sends);
|
|
}
|
|
|
|
public async Task SyncPasswordlessLoginRequestsAsync()
|
|
{
|
|
try
|
|
{
|
|
var userId = await _stateService.GetActiveUserIdAsync();
|
|
// if the user has not enabled passwordless logins ignore requests
|
|
if (!await _stateService.GetApprovePasswordlessLoginsAsync(userId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var loginRequests = await _apiService.GetAuthRequestAsync();
|
|
if (loginRequests == null || !loginRequests.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var validLoginRequest = loginRequests.Where(l => !l.IsAnswered && !l.IsExpired)
|
|
.OrderByDescending(x => x.CreationDate)
|
|
.FirstOrDefault();
|
|
|
|
if (validLoginRequest is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await _stateService.SetPasswordlessLoginNotificationAsync(new PasswordlessRequestNotification()
|
|
{
|
|
Id = validLoginRequest.Id,
|
|
UserId = userId
|
|
});
|
|
|
|
_messagingService.Send(Constants.PasswordlessLoginRequestKey);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Exception(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|