2022-02-23 18:40:17 +01:00
|
|
|
|
using System;
|
2019-04-09 23:01:55 +02:00
|
|
|
|
using System.Collections.Generic;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
using System.Linq;
|
2019-04-09 23:01:55 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
using Bit.Core.Models.Domain;
|
2022-09-26 19:27:57 +02:00
|
|
|
|
using Bit.Core.Models.Response;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
using Bit.Core.Models.View;
|
|
|
|
|
using Bit.Core.Utilities;
|
2019-04-09 23:01:55 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class StateService : IStateService
|
|
|
|
|
{
|
2023-03-01 17:28:28 +01:00
|
|
|
|
// TODO: Refactor this removing all storage services and use the IStorageMediatorService instead
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private readonly IStorageService _storageService;
|
|
|
|
|
private readonly IStorageService _secureStorageService;
|
2023-03-01 17:28:28 +01:00
|
|
|
|
private readonly IStorageMediatorService _storageMediatorService;
|
2022-06-15 18:44:25 +02:00
|
|
|
|
private readonly IMessagingService _messagingService;
|
2019-04-09 23:01:55 +02:00
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private State _state;
|
|
|
|
|
private bool _migrationChecked;
|
|
|
|
|
|
|
|
|
|
public List<AccountView> AccountViews { get; set; }
|
|
|
|
|
|
2022-06-15 18:44:25 +02:00
|
|
|
|
public StateService(IStorageService storageService,
|
|
|
|
|
IStorageService secureStorageService,
|
2023-03-01 17:28:28 +01:00
|
|
|
|
IStorageMediatorService storageMediatorService,
|
2022-06-15 18:44:25 +02:00
|
|
|
|
IMessagingService messagingService)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
|
|
|
|
_storageService = storageService;
|
|
|
|
|
_secureStorageService = secureStorageService;
|
2023-03-01 17:28:28 +01:00
|
|
|
|
_storageMediatorService = storageMediatorService;
|
2022-06-15 18:44:25 +02:00
|
|
|
|
_messagingService = messagingService;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetActiveUserIdAsync()
|
|
|
|
|
{
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
|
|
|
|
|
var activeUserId = _state?.ActiveUserId;
|
|
|
|
|
if (activeUserId == null)
|
|
|
|
|
{
|
|
|
|
|
var state = await GetStateFromStorageAsync();
|
|
|
|
|
activeUserId = state?.ActiveUserId;
|
|
|
|
|
}
|
|
|
|
|
return activeUserId;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 19:14:30 +01:00
|
|
|
|
public async Task<string> GetActiveUserEmailAsync()
|
|
|
|
|
{
|
|
|
|
|
var activeUserId = await GetActiveUserIdAsync();
|
|
|
|
|
return await GetEmailAsync(activeUserId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 17:39:20 +01:00
|
|
|
|
public async Task<T> GetActiveUserCustomDataAsync<T>(Func<Account, T> dataMapper)
|
|
|
|
|
{
|
|
|
|
|
var userId = await GetActiveUserIdAsync();
|
|
|
|
|
var account = await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
);
|
|
|
|
|
return dataMapper(account);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 21:15:21 +01:00
|
|
|
|
public async Task<bool> IsActiveAccountAsync(string userId = null)
|
2022-03-07 18:28:06 +01:00
|
|
|
|
{
|
|
|
|
|
if (userId == null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return userId == await GetActiveUserIdAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task SetActiveUserAsync(string userId)
|
|
|
|
|
{
|
|
|
|
|
if (userId != null)
|
|
|
|
|
{
|
|
|
|
|
await ValidateUserAsync(userId);
|
|
|
|
|
}
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
var state = await GetStateFromStorageAsync();
|
|
|
|
|
state.ActiveUserId = userId;
|
|
|
|
|
await SaveStateToStorageAsync(state);
|
|
|
|
|
_state.ActiveUserId = userId;
|
|
|
|
|
|
|
|
|
|
// Update pre-auth settings based on now-active user
|
|
|
|
|
await SetRememberedOrgIdentifierAsync(await GetRememberedOrgIdentifierAsync());
|
|
|
|
|
await SetPreAuthEnvironmentUrlsAsync(await GetEnvironmentUrlsAsync());
|
2022-12-15 19:21:29 +01:00
|
|
|
|
|
|
|
|
|
await SetLastUserShouldConnectToWatchAsync();
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 18:44:25 +02:00
|
|
|
|
public async Task CheckExtensionActiveUserAndSwitchIfNeededAsync()
|
|
|
|
|
{
|
|
|
|
|
var extensionUserId = await GetExtensionActiveUserIdFromStorageAsync();
|
|
|
|
|
if (string.IsNullOrEmpty(extensionUserId))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_state?.ActiveUserId == extensionUserId)
|
|
|
|
|
{
|
|
|
|
|
// Clear the value in case the user changes the active user from the app
|
|
|
|
|
// so if that happens and the user sends the app to background and comes back
|
|
|
|
|
// the user is not changed again.
|
|
|
|
|
await SaveExtensionActiveUserIdToStorageAsync(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await SetActiveUserAsync(extensionUserId);
|
|
|
|
|
await SaveExtensionActiveUserIdToStorageAsync(null);
|
|
|
|
|
_messagingService.Send(AccountsManagerMessageCommands.SWITCHED_ACCOUNT);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task<bool> IsAuthenticatedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return await GetAccessTokenAsync(userId) != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetUserIdAsync(string email)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(email))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
if (_state?.Accounts != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var account in _state.Accounts)
|
|
|
|
|
{
|
|
|
|
|
var accountEmail = account.Value?.Profile?.Email;
|
|
|
|
|
if (accountEmail == email)
|
|
|
|
|
{
|
|
|
|
|
return account.Value.Profile.UserId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RefreshAccountViewsAsync(bool allowAddAccountRow)
|
|
|
|
|
{
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
|
|
|
|
|
if (AccountViews == null)
|
|
|
|
|
{
|
|
|
|
|
AccountViews = new List<AccountView>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AccountViews.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var accountList = _state?.Accounts?.Values.ToList();
|
|
|
|
|
if (accountList == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var vaultTimeoutService = ServiceContainer.Resolve<IVaultTimeoutService>("vaultTimeoutService");
|
|
|
|
|
foreach (var account in accountList)
|
|
|
|
|
{
|
|
|
|
|
var isActiveAccount = account.Profile.UserId == _state.ActiveUserId;
|
|
|
|
|
var accountView = new AccountView(account, isActiveAccount);
|
2022-03-07 18:28:06 +01:00
|
|
|
|
|
|
|
|
|
if (await vaultTimeoutService.IsLoggedOutByTimeoutAsync(accountView.UserId) ||
|
|
|
|
|
await vaultTimeoutService.ShouldLogOutByTimeoutAsync(accountView.UserId))
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2022-03-07 18:28:06 +01:00
|
|
|
|
accountView.AuthStatus = AuthenticationStatus.LoggedOut;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
2022-03-07 18:28:06 +01:00
|
|
|
|
else if (await vaultTimeoutService.IsLockedAsync(accountView.UserId) ||
|
|
|
|
|
await vaultTimeoutService.ShouldLockAsync(accountView.UserId))
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2022-03-07 18:28:06 +01:00
|
|
|
|
accountView.AuthStatus = AuthenticationStatus.Locked;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
accountView.AuthStatus = AuthenticationStatus.Unlocked;
|
|
|
|
|
}
|
|
|
|
|
AccountViews.Add(accountView);
|
|
|
|
|
}
|
|
|
|
|
if (allowAddAccountRow && AccountViews.Count < Constants.MaxAccounts)
|
|
|
|
|
{
|
|
|
|
|
AccountViews.Add(new AccountView());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddAccountAsync(Account account)
|
|
|
|
|
{
|
|
|
|
|
await ScaffoldNewAccountAsync(account);
|
|
|
|
|
await SetActiveUserAsync(account.Profile.UserId);
|
|
|
|
|
await RefreshAccountViewsAsync(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task LogoutAccountAsync(string userId, bool userInitiated)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(userId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
await RemoveAccountAsync(userId, userInitiated);
|
|
|
|
|
|
2022-03-10 15:02:01 +01:00
|
|
|
|
// If user initiated logout (not vault timeout) and ActiveUserId is null after account removal, find the
|
|
|
|
|
// next user to make active, if any
|
|
|
|
|
if (userInitiated && _state?.ActiveUserId == null && _state?.Accounts != null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
|
|
|
|
foreach (var account in _state.Accounts)
|
|
|
|
|
{
|
|
|
|
|
var uid = account.Value?.Profile?.UserId;
|
|
|
|
|
if (uid == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
await SetActiveUserAsync(uid);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EnvironmentUrlData> GetPreAuthEnvironmentUrlsAsync()
|
|
|
|
|
{
|
|
|
|
|
return await GetValueAsync<EnvironmentUrlData>(
|
|
|
|
|
Constants.PreAuthEnvironmentUrlsKey, await GetDefaultStorageOptionsAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPreAuthEnvironmentUrlsAsync(EnvironmentUrlData value)
|
|
|
|
|
{
|
|
|
|
|
await SetValueAsync(
|
|
|
|
|
Constants.PreAuthEnvironmentUrlsKey, value, await GetDefaultStorageOptionsAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EnvironmentUrlData> GetEnvironmentUrlsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Settings?.EnvironmentUrls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetBiometricUnlockAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.BiometricUnlockKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetBiometricUnlockAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.BiometricUnlockKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
2022-04-26 17:21:17 +02:00
|
|
|
|
|
2022-03-01 20:04:17 +01:00
|
|
|
|
public async Task<bool> GetBiometricLockedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultInMemoryOptionsAsync())
|
|
|
|
|
))?.VolatileData?.BiometricLocked ?? true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetBiometricLockedAsync(bool value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultInMemoryOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.VolatileData.BiometricLocked = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
public async Task<bool> CanAccessPremiumAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
if (userId == null)
|
|
|
|
|
{
|
|
|
|
|
userId = await GetActiveUserIdAsync();
|
|
|
|
|
}
|
|
|
|
|
if (!await IsAuthenticatedAsync(userId))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var account = await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()));
|
|
|
|
|
if (account?.Profile?.HasPremiumPersonally.GetValueOrDefault() ?? false)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var organizationService = ServiceContainer.Resolve<IOrganizationService>("organizationService");
|
|
|
|
|
var organizations = await organizationService.GetAllAsync(userId);
|
|
|
|
|
return organizations?.Any(o => o.UsersGetPremium && o.Enabled) ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 13:03:02 +01:00
|
|
|
|
public async Task SetPersonalPremiumAsync(bool value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
if (account?.Profile == null || account.Profile.HasPremiumPersonally.GetValueOrDefault() == value)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
account.Profile.HasPremiumPersonally = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task<string> GetProtectedPinAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.ProtectedPinKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetProtectedPinAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ProtectedPinKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetPinProtectedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.PinProtectedKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPinProtectedAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PinProtectedKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EncString> GetPinProtectedKeyAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultInMemoryOptionsAsync())
|
2022-03-01 20:04:17 +01:00
|
|
|
|
))?.VolatileData?.PinProtectedKey;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPinProtectedKeyAsync(EncString value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultInMemoryOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
2022-03-01 20:04:17 +01:00
|
|
|
|
account.VolatileData.PinProtectedKey = value;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 17:34:50 +01:00
|
|
|
|
public async Task SetKdfConfigurationAsync(KdfConfig config, string userId = null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
2023-01-30 17:34:50 +01:00
|
|
|
|
account.Profile.KdfType = config.Type;
|
|
|
|
|
account.Profile.KdfIterations = config.Iterations;
|
|
|
|
|
account.Profile.KdfMemory = config.Memory;
|
|
|
|
|
account.Profile.KdfParallelism = config.Parallelism;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetKeyEncryptedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultSecureStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.KeyKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetKeyEncryptedAsync(string value, string userId)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultSecureStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.KeyKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<SymmetricCryptoKey> GetKeyDecryptedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultInMemoryOptionsAsync())
|
2022-03-01 20:04:17 +01:00
|
|
|
|
))?.VolatileData?.Key;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetKeyDecryptedAsync(SymmetricCryptoKey value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultInMemoryOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
2022-03-01 20:04:17 +01:00
|
|
|
|
account.VolatileData.Key = value;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetKeyHashAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.KeyHashKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetKeyHashAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.KeyHashKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetEncKeyEncryptedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.EncKeyKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncKeyEncryptedAsync(string value, string userId)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.EncKeyKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, string>> GetOrgKeysEncryptedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, string>>(Constants.EncOrgKeysKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetOrgKeysEncryptedAsync(Dictionary<string, string> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.EncOrgKeysKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetPrivateKeyEncryptedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.EncPrivateKeyKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPrivateKeyEncryptedAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.EncPrivateKeyKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<string>> GetAutofillBlacklistedUrisAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<List<string>>(Constants.AutofillBlacklistedUrisKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetAutofillBlacklistedUrisAsync(List<string> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.AutofillBlacklistedUrisKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetAutofillTileAddedAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.AutofillTileAdded, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetAutofillTileAddedAsync(bool? value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.AutofillTileAdded, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetEmailAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.Email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetNameAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 15:00:04 +01:00
|
|
|
|
public async Task SetNameAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Profile.Name = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task<string> GetOrgIdentifierAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.OrgIdentifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<long?> GetLastActiveTimeAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<long?>(Constants.LastActiveTimeKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetLastActiveTimeAsync(long? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LastActiveTimeKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int?> GetVaultTimeoutAsync(string userId = null)
|
|
|
|
|
{
|
2023-02-15 18:50:02 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-03-24 21:34:48 +01:00
|
|
|
|
return await GetValueAsync<int?>(Constants.VaultTimeoutKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetVaultTimeoutAsync(int? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.VaultTimeoutKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<VaultTimeoutAction?> GetVaultTimeoutActionAsync(string userId = null)
|
|
|
|
|
{
|
2023-02-15 18:50:02 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<VaultTimeoutAction?>(Constants.VaultTimeoutActionKey(reconciledOptions.UserId),
|
2023-03-24 21:34:48 +01:00
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetVaultTimeoutActionAsync(VaultTimeoutAction? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.VaultTimeoutActionKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:13:10 +02:00
|
|
|
|
public async Task<bool> GetScreenCaptureAllowedAsync(string userId = null)
|
|
|
|
|
{
|
2023-02-15 18:50:02 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.ScreenCaptureAllowedKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? CoreHelpers.InDebugMode();
|
2022-07-15 17:13:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetScreenCaptureAllowedAsync(bool value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ScreenCaptureAllowedKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-07-15 17:13:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 15:02:01 +01:00
|
|
|
|
public async Task<DateTime?> GetLastFileCacheClearAsync()
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<DateTime?>(Constants.LastFileCacheClearKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 15:02:01 +01:00
|
|
|
|
public async Task SetLastFileCacheClearAsync(DateTime? value)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LastFileCacheClearKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<PreviousPageInfo> GetPreviousPageInfoAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<PreviousPageInfo>(Constants.PreviousPageKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPreviousPageInfoAsync(PreviousPageInfo value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PreviousPageKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetInvalidUnlockAttemptsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<int>(Constants.InvalidUnlockAttemptsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetInvalidUnlockAttemptsAsync(int? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.InvalidUnlockAttemptsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetLastBuildAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.LastBuildKey, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetLastBuildAsync(string value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LastBuildKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task<bool?> GetDisableFaviconAsync()
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.DisableFaviconKey, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task SetDisableFaviconAsync(bool? value)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.DisableFaviconKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetDisableAutoTotpCopyAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.DisableAutoTotpCopyKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetDisableAutoTotpCopyAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.DisableAutoTotpCopyKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetInlineAutofillEnabledAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.InlineAutofillEnabledKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetInlineAutofillEnabledAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.InlineAutofillEnabledKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetAutofillDisableSavePromptAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.AutofillDisableSavePromptKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetAutofillDisableSavePromptAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.AutofillDisableSavePromptKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, Dictionary<string, object>>> GetLocalDataAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, Dictionary<string, object>>>(
|
|
|
|
|
Constants.LocalDataKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetLocalDataAsync(Dictionary<string, Dictionary<string, object>> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LocalDataKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, CipherData>> GetEncryptedCiphersAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, CipherData>>(Constants.CiphersKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedCiphersAsync(Dictionary<string, CipherData> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.CiphersKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int?> GetDefaultUriMatchAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<int?>(Constants.DefaultUriMatchKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetDefaultUriMatchAsync(int? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.DefaultUriMatchKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HashSet<string>> GetNeverDomainsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<HashSet<string>>(Constants.NeverDomainsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetNeverDomainsAsync(HashSet<string> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.NeverDomainsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int?> GetClearClipboardAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<int?>(Constants.ClearClipboardKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetClearClipboardAsync(int? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ClearClipboardKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, CollectionData>> GetEncryptedCollectionsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, CollectionData>>(
|
|
|
|
|
Constants.CollectionsKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedCollectionsAsync(Dictionary<string, CollectionData> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.CollectionsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetPasswordRepromptAutofillAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.PasswordRepromptAutofillKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPasswordRepromptAutofillAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PasswordRepromptAutofillKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetPasswordVerifiedAutofillAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.PasswordVerifiedAutofillKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPasswordVerifiedAutofillAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PasswordVerifiedAutofillKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<DateTime?> GetLastSyncAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<DateTime?>(Constants.LastSyncKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetLastSyncAsync(DateTime? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LastSyncKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetSecurityStampAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.Stamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetSecurityStampAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Profile.Stamp = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetEmailVerifiedAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.EmailVerified ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEmailVerifiedAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Profile.EmailVerified = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetSyncOnRefreshAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.SyncOnRefreshKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetSyncOnRefreshAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.SyncOnRefreshKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetRememberedEmailAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.RememberedEmailKey, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetRememberedEmailAsync(string value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.RememberedEmailKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetRememberedOrgIdentifierAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.RememberedOrgIdentifierKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetRememberedOrgIdentifierAsync(string value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.RememberedOrgIdentifierKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task<string> GetThemeAsync()
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.ThemeKey, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task SetThemeAsync(string value)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ThemeKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task<string> GetAutoDarkThemeAsync()
|
2022-06-21 20:59:30 +02:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.AutoDarkThemeKey, await GetDefaultStorageOptionsAsync());
|
2022-06-21 20:59:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:50:02 +01:00
|
|
|
|
public async Task SetAutoDarkThemeAsync(string value)
|
2022-06-21 20:59:30 +02:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.AutoDarkThemeKey, value, await GetDefaultStorageOptionsAsync());
|
2022-06-21 20:59:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
public string GetLocale()
|
|
|
|
|
{
|
|
|
|
|
return _storageMediatorService.Get<string>(Constants.AppLocaleKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetLocale(string locale)
|
|
|
|
|
{
|
|
|
|
|
_storageMediatorService.Save(Constants.AppLocaleKey, locale);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task<bool?> GetAddSitePromptShownAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.AddSitePromptShownKey, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetAddSitePromptShownAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.AddSitePromptShownKey, value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool?> GetPushInitialPromptShownAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.PushInitialPromptShownKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPushInitialPromptShownAsync(bool? value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PushInitialPromptShownKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 22:07:04 +01:00
|
|
|
|
public async Task<DateTime?> GetPushLastRegistrationDateAsync(string userId = null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
return await GetValueAsync<DateTime?>(Constants.PushLastRegistrationDateKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 22:07:04 +01:00
|
|
|
|
public async Task SetPushLastRegistrationDateAsync(DateTime? value, string userId = null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
await SetValueAsync(Constants.PushLastRegistrationDateKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetPushInstallationRegistrationErrorAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.PushInstallationRegistrationErrorKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPushInstallationRegistrationErrorAsync(string value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PushInstallationRegistrationErrorKey, value,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 22:07:04 +01:00
|
|
|
|
public async Task<string> GetPushCurrentTokenAsync(string userId = null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
return await GetValueAsync<string>(Constants.PushCurrentTokenKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 22:07:04 +01:00
|
|
|
|
public async Task SetPushCurrentTokenAsync(string value, string userId = null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
await SetValueAsync(Constants.PushCurrentTokenKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<EventData>> GetEventCollectionAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<List<EventData>>(Constants.EventCollectionKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEventCollectionAsync(List<EventData> value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.EventCollectionKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, FolderData>> GetEncryptedFoldersAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, FolderData>>(Constants.FoldersKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedFoldersAsync(Dictionary<string, FolderData> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.FoldersKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, PolicyData>> GetEncryptedPoliciesAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, PolicyData>>(Constants.PoliciesKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedPoliciesAsync(Dictionary<string, PolicyData> value, string userId)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PoliciesKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetPushRegisteredTokenAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.PushRegisteredTokenKey, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPushRegisteredTokenAsync(string value)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PushRegisteredTokenKey, value, await GetDefaultStorageOptionsAsync());
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetUsesKeyConnectorAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.UsesKeyConnectorKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetUsesKeyConnectorAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.UsesKeyConnectorKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, OrganizationData>> GetOrganizationsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, OrganizationData>>(
|
|
|
|
|
Constants.OrganizationsKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetOrganizationsAsync(Dictionary<string, OrganizationData> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.OrganizationsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<PasswordGenerationOptions> GetPasswordGenerationOptionsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<PasswordGenerationOptions>(Constants.PassGenOptionsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPasswordGenerationOptionsAsync(PasswordGenerationOptions value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PassGenOptionsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 20:32:02 +02:00
|
|
|
|
public async Task<UsernameGenerationOptions> GetUsernameGenerationOptionsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<UsernameGenerationOptions>(
|
|
|
|
|
Constants.UsernameGenOptionsKey(reconciledOptions.UserId), reconciledOptions);
|
2022-08-26 20:32:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetUsernameGenerationOptionsAsync(UsernameGenerationOptions value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.UsernameGenOptionsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-08-26 20:32:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
public async Task<List<GeneratedPasswordHistory>> GetEncryptedPasswordGenerationHistory(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<List<GeneratedPasswordHistory>>(
|
|
|
|
|
Constants.PassGenHistoryKey(reconciledOptions.UserId), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedPasswordGenerationHistoryAsync(List<GeneratedPasswordHistory> value,
|
|
|
|
|
string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PassGenHistoryKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, SendData>> GetEncryptedSendsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, SendData>>(Constants.SendsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetEncryptedSendsAsync(Dictionary<string, SendData> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.SendsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, object>> GetSettingsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<Dictionary<string, object>>(Constants.SettingsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetSettingsAsync(Dictionary<string, object> value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.SettingsKey(reconciledOptions.UserId), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetAccessTokenAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Tokens?.AccessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetAccessTokenAsync(string value, bool skipTokenStorage, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(
|
|
|
|
|
new StorageOptions { UserId = userId, SkipTokenStorage = skipTokenStorage },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Tokens.AccessToken = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetRefreshTokenAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Tokens?.RefreshToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetRefreshTokenAsync(string value, bool skipTokenStorage, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(
|
|
|
|
|
new StorageOptions { UserId = userId, SkipTokenStorage = skipTokenStorage },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Tokens.RefreshToken = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetTwoFactorTokenAsync(string email = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions =
|
|
|
|
|
ReconcileOptions(new StorageOptions { Email = email }, await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<string>(Constants.TwoFactorTokenKey(reconciledOptions.Email), reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetTwoFactorTokenAsync(string value, string email = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions =
|
|
|
|
|
ReconcileOptions(new StorageOptions { Email = email }, await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.TwoFactorTokenKey(reconciledOptions.Email), value, reconciledOptions);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 19:27:57 +02:00
|
|
|
|
public async Task<bool> GetApprovePasswordlessLoginsAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.ApprovePasswordlessLoginsKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-09-26 19:27:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetApprovePasswordlessLoginsAsync(bool? value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ApprovePasswordlessLoginsKey(reconciledOptions.UserId), value,
|
|
|
|
|
reconciledOptions);
|
2022-09-26 19:27:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 18:21:45 +02:00
|
|
|
|
public async Task<PasswordlessRequestNotification> GetPasswordlessLoginNotificationAsync()
|
2022-09-26 19:27:57 +02:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<PasswordlessRequestNotification>(Constants.PasswordlessLoginNotificationKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-09-26 19:27:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 18:21:45 +02:00
|
|
|
|
public async Task SetPasswordlessLoginNotificationAsync(PasswordlessRequestNotification value)
|
2022-09-26 19:27:57 +02:00
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.PasswordlessLoginNotificationKey, value,
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
2022-09-26 19:27:57 +02:00
|
|
|
|
}
|
2023-01-12 19:27:10 +01:00
|
|
|
|
|
|
|
|
|
public async Task SetAvatarColorAsync(string value, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
|
|
|
|
|
await GetDefaultStorageOptionsAsync());
|
|
|
|
|
var account = await GetAccountAsync(reconciledOptions);
|
|
|
|
|
account.Profile.AvatarColor = value;
|
|
|
|
|
await SaveAccountAsync(account, reconciledOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetAvatarColorAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return (await GetAccountAsync(
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync())
|
|
|
|
|
))?.Profile?.AvatarColor;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 15:49:20 +01:00
|
|
|
|
public async Task<string> GetPreLoginEmailAsync()
|
|
|
|
|
{
|
|
|
|
|
var options = await GetDefaultStorageOptionsAsync();
|
|
|
|
|
return await GetValueAsync<string>(Constants.PreLoginEmailKey, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetPreLoginEmailAsync(string value)
|
|
|
|
|
{
|
|
|
|
|
var options = await GetDefaultStorageOptionsAsync();
|
|
|
|
|
await SetValueAsync(Constants.PreLoginEmailKey, value, options);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
// Helpers
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
[Obsolete("Use IStorageMediatorService instead")]
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task<T> GetValueAsync<T>(string key, StorageOptions options)
|
|
|
|
|
{
|
2022-03-21 21:44:54 +01:00
|
|
|
|
return await GetStorageService(options).GetAsync<T>(key);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
[Obsolete("Use IStorageMediatorService instead")]
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task SetValueAsync<T>(string key, T value, StorageOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
await GetStorageService(options).RemoveAsync(key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await GetStorageService(options).SaveAsync(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
[Obsolete("Use IStorageMediatorService instead")]
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private IStorageService GetStorageService(StorageOptions options)
|
|
|
|
|
{
|
|
|
|
|
return options.UseSecureStorage.GetValueOrDefault(false) ? _secureStorageService : _storageService;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
private async Task<string> ComposeKeyAsync(Func<string, string> userDependantKey, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
return userDependantKey(userId ?? await GetActiveUserIdAsync());
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task<Account> GetAccountAsync(StorageOptions options)
|
|
|
|
|
{
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
|
|
|
|
|
if (options?.UserId == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Memory
|
|
|
|
|
if (_state?.Accounts?.ContainsKey(options.UserId) ?? false)
|
|
|
|
|
{
|
2022-03-01 20:04:17 +01:00
|
|
|
|
if (_state.Accounts[options.UserId].VolatileData == null)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2022-03-01 20:04:17 +01:00
|
|
|
|
_state.Accounts[options.UserId].VolatileData = new Account.AccountVolatileData();
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
return _state.Accounts[options.UserId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage
|
2022-03-10 15:02:01 +01:00
|
|
|
|
var state = await GetStateFromStorageAsync();
|
|
|
|
|
if (state?.Accounts?.ContainsKey(options.UserId) ?? false)
|
2022-02-23 18:40:17 +01:00
|
|
|
|
{
|
2022-03-10 15:02:01 +01:00
|
|
|
|
state.Accounts[options.UserId].VolatileData = new Account.AccountVolatileData();
|
|
|
|
|
return state.Accounts[options.UserId];
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SaveAccountAsync(Account account, StorageOptions options = null)
|
|
|
|
|
{
|
|
|
|
|
if (account?.Profile?.UserId == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("account?.Profile?.UserId cannot be null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
|
|
|
|
|
// Memory
|
|
|
|
|
if (UseMemory(options))
|
|
|
|
|
{
|
|
|
|
|
if (_state.Accounts == null)
|
|
|
|
|
{
|
|
|
|
|
_state.Accounts = new Dictionary<string, Account>();
|
|
|
|
|
}
|
|
|
|
|
_state.Accounts[account.Profile.UserId] = account;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
if (UseDisk(options))
|
|
|
|
|
{
|
|
|
|
|
var state = await GetStateFromStorageAsync() ?? new State();
|
|
|
|
|
if (state.Accounts == null)
|
|
|
|
|
{
|
|
|
|
|
state.Accounts = new Dictionary<string, Account>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use Account copy constructor to clone with keys excluded (for storage)
|
|
|
|
|
state.Accounts[account.Profile.UserId] = new Account(account);
|
|
|
|
|
|
|
|
|
|
// If we have a vault timeout and the action is log out, don't store token
|
|
|
|
|
if (options?.SkipTokenStorage.GetValueOrDefault() ?? false)
|
|
|
|
|
{
|
|
|
|
|
state.Accounts[account.Profile.UserId].Tokens.AccessToken = null;
|
|
|
|
|
state.Accounts[account.Profile.UserId].Tokens.RefreshToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await SaveStateToStorageAsync(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RemoveAccountAsync(string userId, bool userInitiated)
|
2019-04-09 23:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(userId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var email = await GetEmailAsync(userId);
|
|
|
|
|
|
|
|
|
|
// Memory
|
|
|
|
|
if (_state?.Accounts?.ContainsKey(userId) ?? false)
|
|
|
|
|
{
|
|
|
|
|
if (userInitiated)
|
|
|
|
|
{
|
|
|
|
|
_state.Accounts.Remove(userId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_state.Accounts[userId].Tokens.AccessToken = null;
|
|
|
|
|
_state.Accounts[userId].Tokens.RefreshToken = null;
|
2022-03-10 15:02:01 +01:00
|
|
|
|
_state.Accounts[userId].VolatileData = null;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (userInitiated && _state?.ActiveUserId == userId)
|
|
|
|
|
{
|
|
|
|
|
_state.ActiveUserId = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
var stateModified = false;
|
|
|
|
|
var state = await GetStateFromStorageAsync();
|
|
|
|
|
if (state?.Accounts?.ContainsKey(userId) ?? false)
|
|
|
|
|
{
|
|
|
|
|
if (userInitiated)
|
|
|
|
|
{
|
|
|
|
|
state.Accounts.Remove(userId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
state.Accounts[userId].Tokens.AccessToken = null;
|
|
|
|
|
state.Accounts[userId].Tokens.RefreshToken = null;
|
|
|
|
|
}
|
|
|
|
|
stateModified = true;
|
|
|
|
|
}
|
|
|
|
|
if (userInitiated && state?.ActiveUserId == userId)
|
|
|
|
|
{
|
|
|
|
|
state.ActiveUserId = null;
|
|
|
|
|
stateModified = true;
|
|
|
|
|
}
|
|
|
|
|
if (stateModified)
|
|
|
|
|
{
|
|
|
|
|
await SaveStateToStorageAsync(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Non-state storage
|
|
|
|
|
await SetProtectedPinAsync(null, userId);
|
|
|
|
|
await SetPinProtectedAsync(null, userId);
|
|
|
|
|
await SetKeyEncryptedAsync(null, userId);
|
|
|
|
|
await SetKeyHashAsync(null, userId);
|
|
|
|
|
await SetEncKeyEncryptedAsync(null, userId);
|
|
|
|
|
await SetOrgKeysEncryptedAsync(null, userId);
|
|
|
|
|
await SetPrivateKeyEncryptedAsync(null, userId);
|
|
|
|
|
await SetLastActiveTimeAsync(null, userId);
|
|
|
|
|
await SetPreviousPageInfoAsync(null, userId);
|
|
|
|
|
await SetInvalidUnlockAttemptsAsync(null, userId);
|
|
|
|
|
await SetLocalDataAsync(null, userId);
|
|
|
|
|
await SetEncryptedCiphersAsync(null, userId);
|
|
|
|
|
await SetEncryptedCollectionsAsync(null, userId);
|
|
|
|
|
await SetLastSyncAsync(null, userId);
|
|
|
|
|
await SetEncryptedFoldersAsync(null, userId);
|
|
|
|
|
await SetEncryptedPoliciesAsync(null, userId);
|
|
|
|
|
await SetUsesKeyConnectorAsync(null, userId);
|
|
|
|
|
await SetOrganizationsAsync(null, userId);
|
|
|
|
|
await SetEncryptedPasswordGenerationHistoryAsync(null, userId);
|
|
|
|
|
await SetEncryptedSendsAsync(null, userId);
|
|
|
|
|
await SetSettingsAsync(null, userId);
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task ScaffoldNewAccountAsync(Account account)
|
2019-04-09 23:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
|
|
|
|
|
account.Settings.EnvironmentUrls = await GetPreAuthEnvironmentUrlsAsync();
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
var state = await GetStateFromStorageAsync() ?? new State();
|
|
|
|
|
if (state.Accounts == null)
|
|
|
|
|
{
|
|
|
|
|
state.Accounts = new Dictionary<string, Account>();
|
|
|
|
|
}
|
2022-04-26 17:21:17 +02:00
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
state.Accounts[account.Profile.UserId] = account;
|
|
|
|
|
await SaveStateToStorageAsync(state);
|
|
|
|
|
|
|
|
|
|
// Memory
|
|
|
|
|
if (_state == null)
|
|
|
|
|
{
|
|
|
|
|
_state = state;
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
if (_state.Accounts == null)
|
|
|
|
|
{
|
|
|
|
|
_state.Accounts = new Dictionary<string, Account>();
|
|
|
|
|
}
|
|
|
|
|
_state.Accounts[account.Profile.UserId] = account;
|
|
|
|
|
}
|
2023-03-24 21:34:48 +01:00
|
|
|
|
|
|
|
|
|
// Check if account has logged in before by checking a guaranteed non-null pref
|
|
|
|
|
if (await GetVaultTimeoutActionAsync(account.Profile.UserId) == null)
|
|
|
|
|
{
|
|
|
|
|
// Account has never logged in, set defaults
|
|
|
|
|
await SetVaultTimeoutAsync(Constants.VaultTimeoutDefault, account.Profile.UserId);
|
|
|
|
|
await SetVaultTimeoutActionAsync(VaultTimeoutAction.Lock, account.Profile.UserId);
|
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private StorageOptions ReconcileOptions(StorageOptions requestedOptions, StorageOptions defaultOptions)
|
|
|
|
|
{
|
|
|
|
|
if (requestedOptions == null)
|
|
|
|
|
{
|
|
|
|
|
return defaultOptions;
|
|
|
|
|
}
|
|
|
|
|
requestedOptions.StorageLocation = requestedOptions.StorageLocation ?? defaultOptions.StorageLocation;
|
|
|
|
|
requestedOptions.UseSecureStorage = requestedOptions.UseSecureStorage ?? defaultOptions.UseSecureStorage;
|
|
|
|
|
requestedOptions.UserId = requestedOptions.UserId ?? defaultOptions.UserId;
|
|
|
|
|
requestedOptions.Email = requestedOptions.Email ?? defaultOptions.Email;
|
|
|
|
|
requestedOptions.SkipTokenStorage = requestedOptions.SkipTokenStorage ?? defaultOptions.SkipTokenStorage;
|
|
|
|
|
return requestedOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:28:28 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the default options for storage.
|
|
|
|
|
/// If it's only used for composing the constant key with the user id
|
|
|
|
|
/// then use <see cref="ComposeKeyAsync(Func{string, string}, string)"/> instead
|
|
|
|
|
/// which saves time if the user id is already known
|
|
|
|
|
/// </summary>
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task<StorageOptions> GetDefaultStorageOptionsAsync()
|
|
|
|
|
{
|
|
|
|
|
return new StorageOptions()
|
|
|
|
|
{
|
|
|
|
|
StorageLocation = StorageLocation.Both,
|
|
|
|
|
UserId = await GetActiveUserIdAsync(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<StorageOptions> GetDefaultSecureStorageOptionsAsync()
|
|
|
|
|
{
|
|
|
|
|
return new StorageOptions()
|
|
|
|
|
{
|
|
|
|
|
StorageLocation = StorageLocation.Disk,
|
|
|
|
|
UseSecureStorage = true,
|
|
|
|
|
UserId = await GetActiveUserIdAsync(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<StorageOptions> GetDefaultInMemoryOptionsAsync()
|
|
|
|
|
{
|
|
|
|
|
return new StorageOptions()
|
|
|
|
|
{
|
|
|
|
|
StorageLocation = StorageLocation.Memory,
|
|
|
|
|
UserId = await GetActiveUserIdAsync(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool UseMemory(StorageOptions options)
|
|
|
|
|
{
|
|
|
|
|
return options?.StorageLocation == StorageLocation.Memory ||
|
|
|
|
|
options?.StorageLocation == StorageLocation.Both;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool UseDisk(StorageOptions options)
|
|
|
|
|
{
|
|
|
|
|
return options?.StorageLocation == StorageLocation.Disk ||
|
|
|
|
|
options?.StorageLocation == StorageLocation.Both;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<State> GetStateFromStorageAsync()
|
|
|
|
|
{
|
2022-03-21 21:44:54 +01:00
|
|
|
|
return await _storageService.GetAsync<State>(Constants.StateKey);
|
2022-02-23 18:40:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SaveStateToStorageAsync(State state)
|
|
|
|
|
{
|
|
|
|
|
await _storageService.SaveAsync(Constants.StateKey, state);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 18:44:25 +02:00
|
|
|
|
private async Task<string> GetExtensionActiveUserIdFromStorageAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _storageService.GetAsync<string>(Constants.iOSExtensionActiveUserIdKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SaveExtensionActiveUserIdToStorageAsync(string userId)
|
|
|
|
|
{
|
|
|
|
|
await _storageService.SaveAsync(Constants.iOSExtensionActiveUserIdKey, userId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task CheckStateAsync()
|
|
|
|
|
{
|
|
|
|
|
if (!_migrationChecked)
|
|
|
|
|
{
|
|
|
|
|
var migrationService = ServiceContainer.Resolve<IStateMigrationService>("stateMigrationService");
|
|
|
|
|
if (await migrationService.NeedsMigration())
|
|
|
|
|
{
|
|
|
|
|
await migrationService.Migrate();
|
|
|
|
|
}
|
|
|
|
|
_migrationChecked = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_state == null)
|
|
|
|
|
{
|
|
|
|
|
_state = await GetStateFromStorageAsync() ?? new State();
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private async Task ValidateUserAsync(string userId)
|
2019-04-09 23:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(userId));
|
|
|
|
|
}
|
|
|
|
|
await CheckStateAsync();
|
|
|
|
|
var accounts = _state?.Accounts;
|
|
|
|
|
if (accounts == null || !accounts.Any())
|
2019-04-09 23:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
throw new Exception("At least one account required to validate user");
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
foreach (var account in accounts)
|
|
|
|
|
{
|
|
|
|
|
if (account.Key == userId)
|
|
|
|
|
{
|
|
|
|
|
// found match, user is valid
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("User does not exist in account list");
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
2022-12-07 17:39:20 +01:00
|
|
|
|
|
|
|
|
|
public async Task<bool> GetShouldConnectToWatchAsync(string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions =
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.ShouldConnectToWatchKey(reconciledOptions.UserId),
|
|
|
|
|
reconciledOptions) ?? false;
|
2022-12-07 17:39:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetShouldConnectToWatchAsync(bool shouldConnect, string userId = null)
|
|
|
|
|
{
|
|
|
|
|
var reconciledOptions =
|
|
|
|
|
ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync());
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.ShouldConnectToWatchKey(reconciledOptions.UserId), shouldConnect,
|
|
|
|
|
reconciledOptions);
|
2022-12-15 19:21:29 +01:00
|
|
|
|
await SetLastUserShouldConnectToWatchAsync(shouldConnect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> GetLastUserShouldConnectToWatchAsync()
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
return await GetValueAsync<bool?>(Constants.LastUserShouldConnectToWatchKey,
|
|
|
|
|
await GetDefaultStorageOptionsAsync()) ?? false;
|
2022-12-15 19:21:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SetLastUserShouldConnectToWatchAsync(bool? shouldConnect = null)
|
|
|
|
|
{
|
2023-02-16 20:44:45 +01:00
|
|
|
|
await SetValueAsync(Constants.LastUserShouldConnectToWatchKey,
|
|
|
|
|
shouldConnect ?? await GetShouldConnectToWatchAsync(), await GetDefaultStorageOptionsAsync());
|
2022-12-07 17:39:20 +01:00
|
|
|
|
}
|
2019-04-09 23:01:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|