From 17cdc963521a367c39e032ee5b535e3f6f19cf25 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Mon, 7 Mar 2022 15:15:21 -0500 Subject: [PATCH] fix for logging out active account from switcher and cleanup (#1830) --- src/App/App.xaml.cs | 25 +++++++---------- src/App/Utilities/AppHelpers.cs | 9 ++++++- src/Core/Abstractions/IStateService.cs | 2 +- src/Core/Services/StateService.cs | 2 +- src/Core/Services/VaultTimeoutService.cs | 34 ++++++++++-------------- src/Core/Utilities/ServiceContainer.cs | 8 +++--- 6 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs index bb0083155..a7e35f3ab 100644 --- a/src/App/App.xaml.cs +++ b/src/App/App.xaml.cs @@ -73,11 +73,9 @@ namespace Bit.App } else if (message.Command == "locked") { - var extras = message.Data as Tuple; - var userId = extras?.Item1; - var userInitiated = extras?.Item2; - Device.BeginInvokeOnMainThread(async () => - await LockedAsync(userId, userInitiated.GetValueOrDefault())); + var (userId, userInitiated) = + message.Data as Tuple ?? new Tuple(null, false); + Device.BeginInvokeOnMainThread(async () => await LockedAsync(userId, userInitiated)); } else if (message.Command == "lockVault") { @@ -85,12 +83,9 @@ namespace Bit.App } else if (message.Command == "logout") { - var extras = message.Data as Tuple; - var userId = extras?.Item1; - var userInitiated = extras?.Item2; - var expired = extras?.Item3; - Device.BeginInvokeOnMainThread(async () => - await LogOutAsync(userId, userInitiated, expired)); + var (userId, userInitiated, expired) = + message.Data as Tuple ?? new Tuple(null, true, false); + Device.BeginInvokeOnMainThread(async () => await LogOutAsync(userId, userInitiated, expired)); } else if (message.Command == "loggedOut") { @@ -262,13 +257,13 @@ namespace Bit.App new System.Globalization.UmAlQuraCalendar(); } - private async Task LogOutAsync(string userId, bool? userInitiated, bool? expired) + private async Task LogOutAsync(string userId, bool userInitiated, bool expired) { - await AppHelpers.LogOutAsync(userId, userInitiated.GetValueOrDefault(true)); + await AppHelpers.LogOutAsync(userId, userInitiated); await SetMainPageAsync(); _authService.LogOut(() => { - if (expired.GetValueOrDefault()) + if (expired) { _platformUtilsService.ShowToast("warning", null, AppResources.LoginExpired); } @@ -432,7 +427,7 @@ namespace Bit.App private async Task LockedAsync(string userId, bool autoPromptBiometric) { - if (!await _stateService.IsActiveAccount(userId)) + if (!await _stateService.IsActiveAccountAsync(userId)) { _platformUtilsService.ShowToast("info", null, AppResources.AccountLockedSuccessfully); return; diff --git a/src/App/Utilities/AppHelpers.cs b/src/App/Utilities/AppHelpers.cs index 68dabe47b..18ee52aac 100644 --- a/src/App/Utilities/AppHelpers.cs +++ b/src/App/Utilities/AppHelpers.cs @@ -240,6 +240,13 @@ namespace Bit.App.Utilities await platformUtilsService.ShowDialogAsync(text, title, AppResources.Yes, AppResources.Cancel); if (confirmed) { + var stateService = ServiceContainer.Resolve("stateService"); + if (await stateService.IsActiveAccountAsync(userId)) + { + var messagingService = ServiceContainer.Resolve("messagingService"); + messagingService.Send("logout"); + return selection; + } await LogOutAsync(userId, true); } } @@ -509,7 +516,7 @@ namespace Bit.App.Utilities var policyService = ServiceContainer.Resolve("policyService"); var searchService = ServiceContainer.Resolve("searchService"); - var isActiveAccount = await stateService.IsActiveAccount(userId); + var isActiveAccount = await stateService.IsActiveAccountAsync(userId); if (userId == null) { diff --git a/src/Core/Abstractions/IStateService.cs b/src/Core/Abstractions/IStateService.cs index ce05d2aed..670a250b9 100644 --- a/src/Core/Abstractions/IStateService.cs +++ b/src/Core/Abstractions/IStateService.cs @@ -12,7 +12,7 @@ namespace Bit.Core.Abstractions { List AccountViews { get; } Task GetActiveUserIdAsync(); - Task IsActiveAccount(string userId = null); + Task IsActiveAccountAsync(string userId = null); Task SetActiveUserAsync(string userId); Task IsAuthenticatedAsync(string userId = null); Task GetUserIdAsync(string email); diff --git a/src/Core/Services/StateService.cs b/src/Core/Services/StateService.cs index 6597b61fb..3f5d25e58 100644 --- a/src/Core/Services/StateService.cs +++ b/src/Core/Services/StateService.cs @@ -42,7 +42,7 @@ namespace Bit.Core.Services return activeUserId; } - public async Task IsActiveAccount(string userId = null) + public async Task IsActiveAccountAsync(string userId = null) { if (userId == null) { diff --git a/src/Core/Services/VaultTimeoutService.cs b/src/Core/Services/VaultTimeoutService.cs index 055c24b70..d602ec260 100644 --- a/src/Core/Services/VaultTimeoutService.cs +++ b/src/Core/Services/VaultTimeoutService.cs @@ -19,8 +19,8 @@ namespace Bit.Core.Services private readonly ITokenService _tokenService; private readonly IPolicyService _policyService; private readonly IKeyConnectorService _keyConnectorService; - private readonly Func, Task> _lockedCallback; - private readonly Func, Task> _loggedOutCallback; + private readonly Func<(string userId, bool userInitiated), Task> _lockedCallback; + private readonly Func<(string userId, bool userInitiated, bool expired), Task> _loggedOutCallback; public VaultTimeoutService( ICryptoService cryptoService, @@ -34,8 +34,8 @@ namespace Bit.Core.Services ITokenService tokenService, IPolicyService policyService, IKeyConnectorService keyConnectorService, - Func, Task> lockedCallback, - Func, Task> loggedOutCallback) + Func<(string userId, bool userInitiated), Task> lockedCallback, + Func<(string userId, bool userInitiated, bool expired), Task> loggedOutCallback) { _cryptoService = cryptoService; _stateService = stateService; @@ -70,12 +70,9 @@ namespace Bit.Core.Services public async Task ShouldLockAsync(string userId = null) { - if (await ShouldTimeoutAsync(userId)) - { - var action = await _stateService.GetVaultTimeoutActionAsync(userId); - return action == VaultTimeoutAction.Lock; - } - return false; + return await ShouldTimeoutAsync(userId) + && + await _stateService.GetVaultTimeoutActionAsync(userId) == VaultTimeoutAction.Lock; } public async Task IsLoggedOutByTimeoutAsync(string userId = null) @@ -87,12 +84,9 @@ namespace Bit.Core.Services public async Task ShouldLogOutByTimeoutAsync(string userId = null) { - if (await ShouldTimeoutAsync(userId)) - { - var action = await _stateService.GetVaultTimeoutActionAsync(userId); - return action == VaultTimeoutAction.Logout; - } - return false; + return await ShouldTimeoutAsync(userId) + && + await _stateService.GetVaultTimeoutActionAsync(userId) == VaultTimeoutAction.Logout; } public async Task CheckVaultTimeoutAsync() @@ -164,7 +158,7 @@ namespace Bit.Core.Services return; } - var isActiveAccount = await _stateService.IsActiveAccount(userId); + var isActiveAccount = await _stateService.IsActiveAccountAsync(userId); if (userId == null) { @@ -189,7 +183,7 @@ namespace Bit.Core.Services await _stateService.SetBiometricLockedAsync(isBiometricLockSet, userId); if (isBiometricLockSet) { - _lockedCallback?.Invoke(new Tuple(userId, userInitiated)); + _lockedCallback?.Invoke((userId, userInitiated)); return; } } @@ -206,14 +200,14 @@ namespace Bit.Core.Services _collectionService.ClearCache(); _searchService.ClearIndex(); } - _lockedCallback?.Invoke(new Tuple(userId, userInitiated)); + _lockedCallback?.Invoke((userId, userInitiated)); } public async Task LogOutAsync(bool userInitiated = true, string userId = null) { if(_loggedOutCallback != null) { - await _loggedOutCallback.Invoke(new Tuple(userId, userInitiated, false)); + await _loggedOutCallback.Invoke((userId, userInitiated, false)); } } diff --git a/src/Core/Utilities/ServiceContainer.cs b/src/Core/Utilities/ServiceContainer.cs index 9ec35dc1f..3d2087087 100644 --- a/src/Core/Utilities/ServiceContainer.cs +++ b/src/Core/Utilities/ServiceContainer.cs @@ -33,7 +33,7 @@ namespace Bit.Core.Utilities var apiService = new ApiService(tokenService, platformUtilsService, (extras) => { messagingService.Send("logout", extras); - return Task.FromResult(0); + return Task.CompletedTask; }, customUserAgent); var appIdService = new AppIdService(storageService); var organizationService = new OrganizationService(stateService); @@ -56,19 +56,19 @@ namespace Bit.Core.Utilities (extras) => { messagingService.Send("locked", extras); - return Task.FromResult(0); + return Task.CompletedTask; }, (extras) => { messagingService.Send("logout", extras); - return Task.FromResult(0); + return Task.CompletedTask; }); var syncService = new SyncService(stateService, apiService, settingsService, folderService, cipherService, cryptoService, collectionService, organizationService, messagingService, policyService, sendService, keyConnectorService, (extras) => { messagingService.Send("logout", extras); - return Task.FromResult(0); + return Task.CompletedTask; }); var passwordGenerationService = new PasswordGenerationService(cryptoService, stateService, cryptoFunctionService, policyService);