1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-28 03:57:43 +02:00

fix for logging out active account from switcher and cleanup (#1830)

This commit is contained in:
Matt Portune 2022-03-07 15:15:21 -05:00 committed by GitHub
parent fcc94d85af
commit 17cdc96352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 42 deletions

View File

@ -73,11 +73,9 @@ namespace Bit.App
} }
else if (message.Command == "locked") else if (message.Command == "locked")
{ {
var extras = message.Data as Tuple<string, bool>; var (userId, userInitiated) =
var userId = extras?.Item1; message.Data as Tuple<string, bool> ?? new Tuple<string, bool>(null, false);
var userInitiated = extras?.Item2; Device.BeginInvokeOnMainThread(async () => await LockedAsync(userId, userInitiated));
Device.BeginInvokeOnMainThread(async () =>
await LockedAsync(userId, userInitiated.GetValueOrDefault()));
} }
else if (message.Command == "lockVault") else if (message.Command == "lockVault")
{ {
@ -85,12 +83,9 @@ namespace Bit.App
} }
else if (message.Command == "logout") else if (message.Command == "logout")
{ {
var extras = message.Data as Tuple<string, bool, bool>; var (userId, userInitiated, expired) =
var userId = extras?.Item1; message.Data as Tuple<string, bool, bool> ?? new Tuple<string, bool, bool>(null, true, false);
var userInitiated = extras?.Item2; Device.BeginInvokeOnMainThread(async () => await LogOutAsync(userId, userInitiated, expired));
var expired = extras?.Item3;
Device.BeginInvokeOnMainThread(async () =>
await LogOutAsync(userId, userInitiated, expired));
} }
else if (message.Command == "loggedOut") else if (message.Command == "loggedOut")
{ {
@ -262,13 +257,13 @@ namespace Bit.App
new System.Globalization.UmAlQuraCalendar(); 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(); await SetMainPageAsync();
_authService.LogOut(() => _authService.LogOut(() =>
{ {
if (expired.GetValueOrDefault()) if (expired)
{ {
_platformUtilsService.ShowToast("warning", null, AppResources.LoginExpired); _platformUtilsService.ShowToast("warning", null, AppResources.LoginExpired);
} }
@ -432,7 +427,7 @@ namespace Bit.App
private async Task LockedAsync(string userId, bool autoPromptBiometric) private async Task LockedAsync(string userId, bool autoPromptBiometric)
{ {
if (!await _stateService.IsActiveAccount(userId)) if (!await _stateService.IsActiveAccountAsync(userId))
{ {
_platformUtilsService.ShowToast("info", null, AppResources.AccountLockedSuccessfully); _platformUtilsService.ShowToast("info", null, AppResources.AccountLockedSuccessfully);
return; return;

View File

@ -240,6 +240,13 @@ namespace Bit.App.Utilities
await platformUtilsService.ShowDialogAsync(text, title, AppResources.Yes, AppResources.Cancel); await platformUtilsService.ShowDialogAsync(text, title, AppResources.Yes, AppResources.Cancel);
if (confirmed) if (confirmed)
{ {
var stateService = ServiceContainer.Resolve<IStateService>("stateService");
if (await stateService.IsActiveAccountAsync(userId))
{
var messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
messagingService.Send("logout");
return selection;
}
await LogOutAsync(userId, true); await LogOutAsync(userId, true);
} }
} }
@ -509,7 +516,7 @@ namespace Bit.App.Utilities
var policyService = ServiceContainer.Resolve<IPolicyService>("policyService"); var policyService = ServiceContainer.Resolve<IPolicyService>("policyService");
var searchService = ServiceContainer.Resolve<ISearchService>("searchService"); var searchService = ServiceContainer.Resolve<ISearchService>("searchService");
var isActiveAccount = await stateService.IsActiveAccount(userId); var isActiveAccount = await stateService.IsActiveAccountAsync(userId);
if (userId == null) if (userId == null)
{ {

View File

@ -12,7 +12,7 @@ namespace Bit.Core.Abstractions
{ {
List<AccountView> AccountViews { get; } List<AccountView> AccountViews { get; }
Task<string> GetActiveUserIdAsync(); Task<string> GetActiveUserIdAsync();
Task<bool> IsActiveAccount(string userId = null); Task<bool> IsActiveAccountAsync(string userId = null);
Task SetActiveUserAsync(string userId); Task SetActiveUserAsync(string userId);
Task<bool> IsAuthenticatedAsync(string userId = null); Task<bool> IsAuthenticatedAsync(string userId = null);
Task<string> GetUserIdAsync(string email); Task<string> GetUserIdAsync(string email);

View File

@ -42,7 +42,7 @@ namespace Bit.Core.Services
return activeUserId; return activeUserId;
} }
public async Task<bool> IsActiveAccount(string userId = null) public async Task<bool> IsActiveAccountAsync(string userId = null)
{ {
if (userId == null) if (userId == null)
{ {

View File

@ -19,8 +19,8 @@ namespace Bit.Core.Services
private readonly ITokenService _tokenService; private readonly ITokenService _tokenService;
private readonly IPolicyService _policyService; private readonly IPolicyService _policyService;
private readonly IKeyConnectorService _keyConnectorService; private readonly IKeyConnectorService _keyConnectorService;
private readonly Func<Tuple<string, bool>, Task> _lockedCallback; private readonly Func<(string userId, bool userInitiated), Task> _lockedCallback;
private readonly Func<Tuple<string, bool, bool>, Task> _loggedOutCallback; private readonly Func<(string userId, bool userInitiated, bool expired), Task> _loggedOutCallback;
public VaultTimeoutService( public VaultTimeoutService(
ICryptoService cryptoService, ICryptoService cryptoService,
@ -34,8 +34,8 @@ namespace Bit.Core.Services
ITokenService tokenService, ITokenService tokenService,
IPolicyService policyService, IPolicyService policyService,
IKeyConnectorService keyConnectorService, IKeyConnectorService keyConnectorService,
Func<Tuple<string, bool>, Task> lockedCallback, Func<(string userId, bool userInitiated), Task> lockedCallback,
Func<Tuple<string, bool, bool>, Task> loggedOutCallback) Func<(string userId, bool userInitiated, bool expired), Task> loggedOutCallback)
{ {
_cryptoService = cryptoService; _cryptoService = cryptoService;
_stateService = stateService; _stateService = stateService;
@ -70,12 +70,9 @@ namespace Bit.Core.Services
public async Task<bool> ShouldLockAsync(string userId = null) public async Task<bool> ShouldLockAsync(string userId = null)
{ {
if (await ShouldTimeoutAsync(userId)) return await ShouldTimeoutAsync(userId)
{ &&
var action = await _stateService.GetVaultTimeoutActionAsync(userId); await _stateService.GetVaultTimeoutActionAsync(userId) == VaultTimeoutAction.Lock;
return action == VaultTimeoutAction.Lock;
}
return false;
} }
public async Task<bool> IsLoggedOutByTimeoutAsync(string userId = null) public async Task<bool> IsLoggedOutByTimeoutAsync(string userId = null)
@ -87,12 +84,9 @@ namespace Bit.Core.Services
public async Task<bool> ShouldLogOutByTimeoutAsync(string userId = null) public async Task<bool> ShouldLogOutByTimeoutAsync(string userId = null)
{ {
if (await ShouldTimeoutAsync(userId)) return await ShouldTimeoutAsync(userId)
{ &&
var action = await _stateService.GetVaultTimeoutActionAsync(userId); await _stateService.GetVaultTimeoutActionAsync(userId) == VaultTimeoutAction.Logout;
return action == VaultTimeoutAction.Logout;
}
return false;
} }
public async Task CheckVaultTimeoutAsync() public async Task CheckVaultTimeoutAsync()
@ -164,7 +158,7 @@ namespace Bit.Core.Services
return; return;
} }
var isActiveAccount = await _stateService.IsActiveAccount(userId); var isActiveAccount = await _stateService.IsActiveAccountAsync(userId);
if (userId == null) if (userId == null)
{ {
@ -189,7 +183,7 @@ namespace Bit.Core.Services
await _stateService.SetBiometricLockedAsync(isBiometricLockSet, userId); await _stateService.SetBiometricLockedAsync(isBiometricLockSet, userId);
if (isBiometricLockSet) if (isBiometricLockSet)
{ {
_lockedCallback?.Invoke(new Tuple<string, bool>(userId, userInitiated)); _lockedCallback?.Invoke((userId, userInitiated));
return; return;
} }
} }
@ -206,14 +200,14 @@ namespace Bit.Core.Services
_collectionService.ClearCache(); _collectionService.ClearCache();
_searchService.ClearIndex(); _searchService.ClearIndex();
} }
_lockedCallback?.Invoke(new Tuple<string, bool>(userId, userInitiated)); _lockedCallback?.Invoke((userId, userInitiated));
} }
public async Task LogOutAsync(bool userInitiated = true, string userId = null) public async Task LogOutAsync(bool userInitiated = true, string userId = null)
{ {
if(_loggedOutCallback != null) if(_loggedOutCallback != null)
{ {
await _loggedOutCallback.Invoke(new Tuple<string, bool, bool>(userId, userInitiated, false)); await _loggedOutCallback.Invoke((userId, userInitiated, false));
} }
} }

View File

@ -33,7 +33,7 @@ namespace Bit.Core.Utilities
var apiService = new ApiService(tokenService, platformUtilsService, (extras) => var apiService = new ApiService(tokenService, platformUtilsService, (extras) =>
{ {
messagingService.Send("logout", extras); messagingService.Send("logout", extras);
return Task.FromResult(0); return Task.CompletedTask;
}, customUserAgent); }, customUserAgent);
var appIdService = new AppIdService(storageService); var appIdService = new AppIdService(storageService);
var organizationService = new OrganizationService(stateService); var organizationService = new OrganizationService(stateService);
@ -56,19 +56,19 @@ namespace Bit.Core.Utilities
(extras) => (extras) =>
{ {
messagingService.Send("locked", extras); messagingService.Send("locked", extras);
return Task.FromResult(0); return Task.CompletedTask;
}, },
(extras) => (extras) =>
{ {
messagingService.Send("logout", extras); messagingService.Send("logout", extras);
return Task.FromResult(0); return Task.CompletedTask;
}); });
var syncService = new SyncService(stateService, apiService, settingsService, folderService, cipherService, var syncService = new SyncService(stateService, apiService, settingsService, folderService, cipherService,
cryptoService, collectionService, organizationService, messagingService, policyService, sendService, cryptoService, collectionService, organizationService, messagingService, policyService, sendService,
keyConnectorService, (extras) => keyConnectorService, (extras) =>
{ {
messagingService.Send("logout", extras); messagingService.Send("logout", extras);
return Task.FromResult(0); return Task.CompletedTask;
}); });
var passwordGenerationService = new PasswordGenerationService(cryptoService, stateService, var passwordGenerationService = new PasswordGenerationService(cryptoService, stateService,
cryptoFunctionService, policyService); cryptoFunctionService, policyService);