From 38184e489369db1f5f93eafb304c83805e2dd0d4 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 5 Aug 2016 23:58:31 -0400 Subject: [PATCH] Added "first load" check on site list to wait for sync to complete if key changed. Renamed "other" constants. --- src/App/App.cs | 2 +- src/App/Constants.cs | 10 ++++++---- src/App/Pages/Lock/LockFingerprintPage.cs | 2 +- src/App/Pages/Lock/LockPasswordPage.cs | 2 +- src/App/Pages/Lock/LockPinPage.cs | 2 +- src/App/Pages/LoginPage.cs | 4 ++-- src/App/Pages/Settings/SettingsPage.cs | 2 +- src/App/Pages/Settings/SettingsSyncPage.cs | 2 +- src/App/Pages/Vault/VaultListSitesPage.cs | 13 +++++++++++-- src/App/Services/AuthService.cs | 1 + src/App/Services/CryptoService.cs | 18 +++++++++++++++++- src/App/Services/LockService.cs | 4 ++-- src/App/Services/SyncService.cs | 8 ++++---- .../LockFingerprintViewController.cs | 2 +- .../LockPasswordViewController.cs | 2 +- src/iOS.Extension/LockPinViewController.cs | 2 +- src/iOS.Extension/SiteListViewController.cs | 2 +- src/iOS/AppDelegate.cs | 2 +- 18 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/App/App.cs b/src/App/App.cs index c63532789..789113f9d 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -98,7 +98,7 @@ namespace Bit.App if(Device.OS == TargetPlatform.Android) { - _settings.AddOrUpdateValue(Constants.SettingLastBackgroundedDate, DateTime.UtcNow); + _settings.AddOrUpdateValue(Constants.LastBackgroundedDate, DateTime.UtcNow); } } diff --git a/src/App/Constants.cs b/src/App/Constants.cs index 8f6654318..82fb41fa6 100644 --- a/src/App/Constants.cs +++ b/src/App/Constants.cs @@ -5,10 +5,6 @@ public const string SettingFingerprintUnlockOn = "setting:fingerprintUnlockOn"; public const string SettingPinUnlockOn = "setting:pinUnlockOn"; public const string SettingLockSeconds = "setting:lockSeconds"; - public const string SettingLastBackgroundedDate = "lastBackgroundedDate"; - public const string SettingLocked = "locked"; - public const string SettingLastLoginEmail = "lastLoginEmail"; - public const string SettingLastSync = "lastSync"; public const string PasswordGeneratorLength = "pwGenerator:length"; public const string PasswordGeneratorUppercase = "pwGenerator:uppercase"; @@ -24,5 +20,11 @@ public const string ExtensionStarted = "extension:started"; public const string ExtensionActivated = "extension:activated"; + + public const string FirstVaultLoad = "other:firstVaultLoad"; + public const string LastBackgroundedDate = "other:lastBackgroundedDate"; + public const string Locked = "other:locked"; + public const string LastLoginEmail = "other:lastLoginEmail"; + public const string LastSync = "other:lastSync"; } } diff --git a/src/App/Pages/Lock/LockFingerprintPage.cs b/src/App/Pages/Lock/LockFingerprintPage.cs index 6773bba46..b2a8c7748 100644 --- a/src/App/Pages/Lock/LockFingerprintPage.cs +++ b/src/App/Pages/Lock/LockFingerprintPage.cs @@ -97,7 +97,7 @@ namespace Bit.App.Pages var result = await _fingerprint.AuthenticateAsync("Use your fingerprint to verify."); if(result.Authenticated) { - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); await Navigation.PopModalAsync(); } else if(result.Status == FingerprintAuthenticationResultStatus.FallbackRequested) diff --git a/src/App/Pages/Lock/LockPasswordPage.cs b/src/App/Pages/Lock/LockPasswordPage.cs index 7a9a9488d..4c4809e4c 100644 --- a/src/App/Pages/Lock/LockPasswordPage.cs +++ b/src/App/Pages/Lock/LockPasswordPage.cs @@ -107,7 +107,7 @@ namespace Bit.App.Pages var key = _cryptoService.MakeKeyFromPassword(PasswordCell.Entry.Text, _authService.Email); if(key.SequenceEqual(_cryptoService.Key)) { - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); await Navigation.PopModalAsync(); } else diff --git a/src/App/Pages/Lock/LockPinPage.cs b/src/App/Pages/Lock/LockPinPage.cs index 18cc41a7f..85f5a7245 100644 --- a/src/App/Pages/Lock/LockPinPage.cs +++ b/src/App/Pages/Lock/LockPinPage.cs @@ -92,7 +92,7 @@ namespace Bit.App.Pages { if(Model.PIN == _authService.PIN) { - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); PinControl.Entry.Unfocus(); Navigation.PopModalAsync(); } diff --git a/src/App/Pages/LoginPage.cs b/src/App/Pages/LoginPage.cs index 9033d646e..e2784afcc 100644 --- a/src/App/Pages/LoginPage.cs +++ b/src/App/Pages/LoginPage.cs @@ -53,7 +53,7 @@ namespace Bit.App.Pages entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope", containerPadding: padding); - var lastLoginEmail = _settings.GetValueOrDefault(Constants.SettingLastLoginEmail); + var lastLoginEmail = _settings.GetValueOrDefault(Constants.LastLoginEmail); if(!string.IsNullOrWhiteSpace(lastLoginEmail)) { EmailCell.Entry.Text = lastLoginEmail; @@ -179,7 +179,7 @@ namespace Bit.App.Pages _authService.Token = response.Result.Token; _authService.UserId = response.Result?.Profile?.Id; _authService.Email = response.Result?.Profile?.Email; - _settings.AddOrUpdateValue(Constants.SettingLastLoginEmail, _authService.Email); + _settings.AddOrUpdateValue(Constants.LastLoginEmail, _authService.Email); _googleAnalyticsService.RefreshUserId(); _googleAnalyticsService.TrackAppEvent("LoggedIn"); diff --git a/src/App/Pages/Settings/SettingsPage.cs b/src/App/Pages/Settings/SettingsPage.cs index fd945c485..b79faa36d 100644 --- a/src/App/Pages/Settings/SettingsPage.cs +++ b/src/App/Pages/Settings/SettingsPage.cs @@ -256,7 +256,7 @@ namespace Bit.App.Pages private void LockCell_Tapped(object sender, EventArgs e) { _googleAnalyticsService.TrackAppEvent("Locked"); - _settings.AddOrUpdateValue(Constants.SettingLocked, true); + _settings.AddOrUpdateValue(Constants.Locked, true); MessagingCenter.Send(Application.Current, "Lock", true); } diff --git a/src/App/Pages/Settings/SettingsSyncPage.cs b/src/App/Pages/Settings/SettingsSyncPage.cs index 0932a7aa7..4cafeeb75 100644 --- a/src/App/Pages/Settings/SettingsSyncPage.cs +++ b/src/App/Pages/Settings/SettingsSyncPage.cs @@ -72,7 +72,7 @@ namespace Bit.App.Pages private void SetLastSync() { - var lastSyncDate = _settings.GetValueOrDefault(Constants.SettingLastSync); + var lastSyncDate = _settings.GetValueOrDefault(Constants.LastSync); LastSyncLabel.Text = "Last Sync: " + lastSyncDate?.ToLocalTime().ToString() ?? "Never"; } diff --git a/src/App/Pages/Vault/VaultListSitesPage.cs b/src/App/Pages/Vault/VaultListSitesPage.cs index 7ed1f6bbd..6492e7fe2 100644 --- a/src/App/Pages/Vault/VaultListSitesPage.cs +++ b/src/App/Pages/Vault/VaultListSitesPage.cs @@ -28,6 +28,7 @@ namespace Bit.App.Pages private readonly IPushNotification _pushNotification; private readonly ISettings _settings; private readonly bool _favorites; + private bool _loadExistingData; public VaultListSitesPage(bool favorites) { @@ -41,6 +42,9 @@ namespace Bit.App.Pages _pushNotification = Resolver.Resolve(); _settings = Resolver.Resolve(); + var cryptoService = Resolver.Resolve(); + _loadExistingData = !_settings.GetValueOrDefault(Constants.FirstVaultLoad, true) || !cryptoService.KeyChanged; + Init(); } @@ -150,7 +154,10 @@ namespace Bit.App.Pages protected override void OnAppearing() { base.OnAppearing(); - FetchAndLoadVault(); + if(_loadExistingData) + { + FetchAndLoadVault(); + } if(_connectivity.IsConnected && Device.OS == TargetPlatform.iOS && !_favorites) { @@ -181,12 +188,14 @@ namespace Bit.App.Pages // Check push registration once per day registerAction(); } - } } private void FetchAndLoadVault() { + _settings.AddOrUpdateValue(Constants.FirstVaultLoad, false); + _loadExistingData = true; + if(PresentationFolders.Count > 0 && _syncService.SyncInProgress) { return; diff --git a/src/App/Services/AuthService.cs b/src/App/Services/AuthService.cs index ef22a2c98..ebcffdf44 100644 --- a/src/App/Services/AuthService.cs +++ b/src/App/Services/AuthService.cs @@ -206,6 +206,7 @@ namespace Bit.App.Services UserId = null; Email = null; _cryptoService.Key = null; + _settings.Remove(Constants.FirstVaultLoad); } public async Task> TokenPostAsync(TokenRequest request) diff --git a/src/App/Services/CryptoService.cs b/src/App/Services/CryptoService.cs index ee4860934..dd597f9c7 100644 --- a/src/App/Services/CryptoService.cs +++ b/src/App/Services/CryptoService.cs @@ -88,7 +88,23 @@ namespace Bit.App.Services } } - public bool KeyChanged => !PreviousKey?.SequenceEqual(Key) ?? Key == null ? false : true; + public bool KeyChanged + { + get + { + if(Key == null) + { + throw new InvalidOperationException("Key must be set before asking if it has changed."); + } + + if(PreviousKey == null) + { + return Key != null; + } + + return !PreviousKey.SequenceEqual(Key); + } + } public CipherString Encrypt(string plaintextValue) { diff --git a/src/App/Services/LockService.cs b/src/App/Services/LockService.cs index 6d2012e9e..cf89251fa 100644 --- a/src/App/Services/LockService.cs +++ b/src/App/Services/LockService.cs @@ -31,7 +31,7 @@ namespace Bit.App.Services } // Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately) - if(!forceLock && !_settings.GetValueOrDefault(Constants.SettingLocked, false)) + if(!forceLock && !_settings.GetValueOrDefault(Constants.Locked, false)) { // Lock seconds tells if they want to lock the app or not var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15); @@ -42,7 +42,7 @@ namespace Bit.App.Services // Has it been longer than lockSeconds since the last time the app was backgrounded? var now = DateTime.UtcNow; - var lastBackground = _settings.GetValueOrDefault(Constants.SettingLastBackgroundedDate, now.AddYears(-1)); + var lastBackground = _settings.GetValueOrDefault(Constants.LastBackgroundedDate, now.AddYears(-1)); if((now - lastBackground).TotalSeconds < lockSeconds) { return LockType.None; diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index e4bf9eee0..5c14b06bd 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -160,14 +160,14 @@ namespace Bit.App.Services return false; } - _settings.AddOrUpdateValue(Constants.SettingLastSync, now); + _settings.AddOrUpdateValue(Constants.LastSync, now); SyncCompleted(true); return true; } public async Task IncrementalSyncAsync(TimeSpan syncThreshold) { - DateTime? lastSync = _settings.GetValueOrDefault(Constants.SettingLastSync); + DateTime? lastSync = _settings.GetValueOrDefault(Constants.LastSync); if(lastSync != null && DateTime.UtcNow - lastSync.Value < syncThreshold) { return false; @@ -184,7 +184,7 @@ namespace Bit.App.Services } var now = DateTime.UtcNow; - DateTime? lastSync = _settings.GetValueOrDefault(Constants.SettingLastSync); + DateTime? lastSync = _settings.GetValueOrDefault(Constants.LastSync); if(lastSync == null) { return await FullSyncAsync(); @@ -217,7 +217,7 @@ namespace Bit.App.Services return false; } - _settings.AddOrUpdateValue(Constants.SettingLastSync, now); + _settings.AddOrUpdateValue(Constants.LastSync, now); SyncCompleted(true); return true; } diff --git a/src/iOS.Extension/LockFingerprintViewController.cs b/src/iOS.Extension/LockFingerprintViewController.cs index b370de482..678e30cc3 100644 --- a/src/iOS.Extension/LockFingerprintViewController.cs +++ b/src/iOS.Extension/LockFingerprintViewController.cs @@ -70,7 +70,7 @@ namespace Bit.iOS.Extension var result = await _fingerprint.AuthenticateAsync("Use your fingerprint to verify."); if(result.Authenticated) { - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); LoadingController.DismissLockAndContinue(); } } diff --git a/src/iOS.Extension/LockPasswordViewController.cs b/src/iOS.Extension/LockPasswordViewController.cs index 27451919f..ae646bcef 100644 --- a/src/iOS.Extension/LockPasswordViewController.cs +++ b/src/iOS.Extension/LockPasswordViewController.cs @@ -85,7 +85,7 @@ namespace Bit.iOS.Extension var key = _cryptoService.MakeKeyFromPassword(MasterPasswordCell.TextField.Text, _authService.Email); if(key.SequenceEqual(_cryptoService.Key)) { - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); MasterPasswordCell.TextField.ResignFirstResponder(); LoadingController.DismissLockAndContinue(); } diff --git a/src/iOS.Extension/LockPinViewController.cs b/src/iOS.Extension/LockPinViewController.cs index 8da31df65..219875f12 100644 --- a/src/iOS.Extension/LockPinViewController.cs +++ b/src/iOS.Extension/LockPinViewController.cs @@ -65,7 +65,7 @@ namespace Bit.iOS.Extension if(PinTextField.Text == _authService.PIN) { Debug.WriteLine("BW Log, Start Dismiss PIN controller."); - _settings.AddOrUpdateValue(Constants.SettingLocked, false); + _settings.AddOrUpdateValue(Constants.Locked, false); PinTextField.ResignFirstResponder(); LoadingController.DismissLockAndContinue(); } diff --git a/src/iOS.Extension/SiteListViewController.cs b/src/iOS.Extension/SiteListViewController.cs index 245d6b82a..578ce2f10 100644 --- a/src/iOS.Extension/SiteListViewController.cs +++ b/src/iOS.Extension/SiteListViewController.cs @@ -174,7 +174,7 @@ namespace Bit.iOS.Extension return; } - Resolver.Resolve().AddOrUpdateValue(App.Constants.SettingLastBackgroundedDate, DateTime.UtcNow); + Resolver.Resolve().AddOrUpdateValue(App.Constants.LastBackgroundedDate, DateTime.UtcNow); var item = _tableItems.ElementAt(indexPath.Row); if(item == null) diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index 0f97835dd..feea6ae1c 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -141,7 +141,7 @@ namespace Bit.iOS UIApplication.SharedApplication.SetStatusBarHidden(true, false); // Log the date/time we last backgrounded - Settings.AddOrUpdateValue(App.Constants.SettingLastBackgroundedDate, DateTime.UtcNow); + Settings.AddOrUpdateValue(App.Constants.LastBackgroundedDate, DateTime.UtcNow); // Dispatch Google Analytics SendGoogleAnalyticsHitsInBackground();