From ebf65ecb96a8125f8253b5415d52522958431d87 Mon Sep 17 00:00:00 2001 From: Todd Martin <106564991+trmartin4@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:07:04 -0500 Subject: [PATCH] Set push token state values to be user-specific (#2200) * Changed the current push token and last registration time to be user-based * Fixed compile error. * Fixed interface implementation. * Fixed compile error for Android. * Refactored to handle getting active user ID within state service * Refactored methods allow existing logic to handle getting the active user. * Updated to reconcile options. * Updated naming and fixed issue with UserId. * Removed space between constants. --- src/App/Services/MobileStorageService.cs | 2 -- src/Core/Abstractions/IStateService.cs | 8 +++--- src/Core/Constants.cs | 4 +-- src/Core/Services/StateService.cs | 32 ++++++++++++------------ 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/App/Services/MobileStorageService.cs b/src/App/Services/MobileStorageService.cs index d510f3130..77951767a 100644 --- a/src/App/Services/MobileStorageService.cs +++ b/src/App/Services/MobileStorageService.cs @@ -19,9 +19,7 @@ namespace Bit.App.Services Constants.AddSitePromptShownKey, Constants.PushInitialPromptShownKey, Constants.LastFileCacheClearKey, - Constants.PushLastRegistrationDateKey, Constants.PushRegisteredTokenKey, - Constants.PushCurrentTokenKey, Constants.LastBuildKey, Constants.ClearCiphersCacheKey, Constants.BiometricIntegrityKey, diff --git a/src/Core/Abstractions/IStateService.cs b/src/Core/Abstractions/IStateService.cs index 9525ed481..5f9b33e2d 100644 --- a/src/Core/Abstractions/IStateService.cs +++ b/src/Core/Abstractions/IStateService.cs @@ -119,12 +119,12 @@ namespace Bit.Core.Abstractions Task SetAddSitePromptShownAsync(bool? value, string userId = null); Task GetPushInitialPromptShownAsync(); Task SetPushInitialPromptShownAsync(bool? value); - Task GetPushLastRegistrationDateAsync(); - Task SetPushLastRegistrationDateAsync(DateTime? value); + Task GetPushLastRegistrationDateAsync(string userId = null); + Task SetPushLastRegistrationDateAsync(DateTime? value, string userId = null); Task GetPushInstallationRegistrationErrorAsync(); Task SetPushInstallationRegistrationErrorAsync(string value); - Task GetPushCurrentTokenAsync(); - Task SetPushCurrentTokenAsync(string value); + Task GetPushCurrentTokenAsync(string userId = null); + Task SetPushCurrentTokenAsync(string value, string userId = null); Task> GetEventCollectionAsync(); Task SetEventCollectionAsync(List value); Task> GetEncryptedFoldersAsync(string userId = null); diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs index 06d3a19a7..4547ec091 100644 --- a/src/Core/Constants.cs +++ b/src/Core/Constants.cs @@ -12,8 +12,6 @@ public static string LastFileCacheClearKey = "lastFileCacheClear"; public static string AutofillTileAdded = "autofillTileAdded"; public static string PushRegisteredTokenKey = "pushRegisteredToken"; - public static string PushCurrentTokenKey = "pushCurrentToken"; - public static string PushLastRegistrationDateKey = "pushLastRegistrationDate"; public static string PushInitialPromptShownKey = "pushInitialPromptShown"; public static string PushInstallationRegistrationErrorKey = "pushInstallationRegistrationError"; public static string LastBuildKey = "lastBuild"; @@ -96,6 +94,8 @@ public static string BiometricUnlockKey(string userId) => $"biometricUnlock_{userId}"; public static string ApprovePasswordlessLoginsKey(string userId) => $"approvePasswordlessLogins_{userId}"; public static string UsernameGenOptionsKey(string userId) => $"usernameGenerationOptions_{userId}"; + public static string PushLastRegistrationDateKey(string userId) => $"pushLastRegistrationDate_{userId}"; + public static string PushCurrentTokenKey(string userId) => $"pushCurrentToken_{userId}"; public static string ShouldConnectToWatchKey(string userId) => $"shouldConnectToWatch_{userId}"; } } diff --git a/src/Core/Services/StateService.cs b/src/Core/Services/StateService.cs index 72da24ff0..9b7371f55 100644 --- a/src/Core/Services/StateService.cs +++ b/src/Core/Services/StateService.cs @@ -1009,18 +1009,18 @@ namespace Bit.Core.Services await SetValueAsync(key, value, options); } - public async Task GetPushLastRegistrationDateAsync() + public async Task GetPushLastRegistrationDateAsync(string userId = null) { - var options = await GetDefaultStorageOptionsAsync(); - var key = Constants.PushLastRegistrationDateKey; - return await GetValueAsync(key, options); + var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()); + var key = Constants.PushLastRegistrationDateKey(reconciledOptions.UserId); + return await GetValueAsync(key, reconciledOptions); } - public async Task SetPushLastRegistrationDateAsync(DateTime? value) + public async Task SetPushLastRegistrationDateAsync(DateTime? value, string userId = null) { - var options = await GetDefaultStorageOptionsAsync(); - var key = Constants.PushLastRegistrationDateKey; - await SetValueAsync(key, value, options); + var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()); + var key = Constants.PushLastRegistrationDateKey(reconciledOptions.UserId); + await SetValueAsync(key, value, reconciledOptions); } public async Task GetPushInstallationRegistrationErrorAsync() @@ -1037,18 +1037,18 @@ namespace Bit.Core.Services await SetValueAsync(key, value, options); } - public async Task GetPushCurrentTokenAsync() + public async Task GetPushCurrentTokenAsync(string userId = null) { - var options = await GetDefaultStorageOptionsAsync(); - var key = Constants.PushCurrentTokenKey; - return await GetValueAsync(key, options); + var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()); + var key = Constants.PushCurrentTokenKey(reconciledOptions.UserId); + return await GetValueAsync(key, reconciledOptions); } - public async Task SetPushCurrentTokenAsync(string value) + public async Task SetPushCurrentTokenAsync(string value, string userId = null) { - var options = await GetDefaultStorageOptionsAsync(); - var key = Constants.PushCurrentTokenKey; - await SetValueAsync(key, value, options); + var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()); + var key = Constants.PushCurrentTokenKey(reconciledOptions.UserId); + await SetValueAsync(key, value, reconciledOptions); } public async Task> GetEventCollectionAsync()