mirror of
https://github.com/bitwarden/mobile
synced 2025-12-22 11:13:49 +00:00
[PM-3349] [PM-3350] MAUI Migration Initial (#2806)
* PM-3349 PM-3350 MAUI Migration Initial * PM-3349 PM-3350 MAUI Migration fix nullable exception bindings and AsyncCommand canExecute null exception * PM-3349 PM-3350 MAUI Migration fix nullable bindings and fallbacks * PM-3349: Android Added CustomTabbedPageHandler for Android to handle the tab "reselection" for PopToRoot. Commented support for Windows in App.csproj Disabled Interpreter on Android to avoid very slow app in Debug (during Login for example) Added some null checks that were causing crashes (on GeneratorPageVM and PickerVM) Minor TabsPage cleanup * TabBarEffect removed and it's behavior is now taken care of by CustomTabbedPageHandler * PM-3349 PM-3350 Add null checks on CipherDetailsPageVM to avoid crash opening Secure Notes. * PM-3349 PM-3350 MAUI Migration Start iOS extensions * Changes to solution to hopefully fix Config Mappings * PM-3349 Removed Deploy from iOS.Autofill to allow running Android Changed MainApplication SpecialFolder.Personal to SpecialFolder.LocalApplicationData * PM-3350 MAUI Migration Fix iOS Autofill extension * PM-3349 Changed UseMauiApp init so that Android Handlers still get added * PM-3349 Implemented HybridWebViewHandler for Android which enables 2nd factor auth flows Ensured CustomTabbedPageHandler had it's DisconnectHandler called Some minor code upgrades of older obsolete Xamarin Forms code. * PM-3349 Implemented HybridWebViewHandler for iOS * hardcoded AccountViewCell Avatar image to 40x40 to avoid current iOS/Android bugs where they fill much larger space. * PM-3349 PM-3350 Added (migrated) CustomNavigationHandler (which should partially fix the AvatarIcon in the NavBar in iOS) Added (migrated) CustomContentPageHandler (which should mostly place the AvatarIcon in the navBar in the correct place for iOS) Added Task.Delay (workaround) to allow the Avatar to load in iOS on the LoginPage Added workaround for iOS bug with the toolbar size (more info in comment in AvatarImageSource.cs) Went through the AccountViewCell MAUI-Migration comments. (and deleted/added more comments as needed) Migrated some Device calls to DeviceInfo and MainThread Added (migrated) CustomTabbedHandler (for managing the iOS TabBar) * PM-3349 Replaced the FabShadowEffect with the new MAUI Shadow to fix the buggy shadows on the Android Fab Button. * PM-3349 ToolbarHandler created for setting text on Android go back buttons. * PM-3350 Migrated the CustomViewCellRenderer for iOS * PM-3350 Removed ButtonHandlerMappings and some other code related with fonts as MAUI is taking care of Accessibility and no custom code should be needed Migrated SelectableLabelRenderer to Handler Cleaned LabelHandlerMappings and added logic to migrate the CustomLabelRenderer * Enabled argon2Id for iOS * PM-3349 Added Argon libraries for Android minor change to gitignore so that the Argon x86 lib is not ignored on the Android platform * PM-3350 Migrated some Device to DeviceInfo and added temporary workaround with some comments to be able to see the Generated Password on iOS * PM-3350 Added some missing images in iOS * PM-3349 PRM-3350 Replaced XZing with Camera.MAUI for QRCodes * Checked some [MAUI-Migration] and deleted when it's working as intended. SearchBarHandlerMapping: IME options working as intended SliderHandlerMappings: The MAUI "replacement" for Color.Default seems to be White so the old use case doesn't seem to be needed anymore. * PM-3350 Checked some [MAUI-Migration] and changed as needed. TimePickerHandlerMappings: Remove old code for forcing the Wheel. After testing without it wheel picker is still used so this code shouldn't be needed anymore. AppDelegate.ContinueUserActivity: Uncommented and changed the iOS ContinueUserActivity. It needs to call Platform.ContinueUserActivity according with Xamarin Essentials migration docs. * PM-3349 Fixed white tint color not appearing on images added as MAU IImage SVG PM-3349 PM-3350 Fix for Avatar text not adjusting to white/black color correctly * PM-3350 Removed MAUI Splash Screen. Fixed iOS Privacy Screen logo (hardcoded image to avoid it getting cropped) * PM-3350 Quick workaround to allow 2nd factor auth to not get stuck in iOS in modals. Updated some older "Device" code to the newer MAUI code. * PM-3350 Removed duplicate reference to LaunchScreen.storyboard * PM-3349 PM-3350 Minor change to HomePage to set fixed Image height otherwise it takes more space than it did in the old Xamarin Forms app. Added HIdeSoftInputOnTapped on several pages (the ones with Entry controls) to allow hiding the keyboard when tapping "outside" of it. (just like we did in Xamarin Forms app) * PM-3350 Added Scrollview on HomePage so that the "Create account" button can be accessed in smaller devices like iPhone SE. * PM-3349 Added Handler that enables the ExtendedDatePicker to get IsFocused events in Android. This is a workaround for fixing the current bug where it's not possible to select the "current day" in the expiration date of a Send. Fix for TimePicker not displaying default Time Value Updated some "Device" code to the new MAUI "DeviceInfo" * PM-3349 PM-3350 Migrated IconLabelButton Frames to Borders to fix issue with TapGestureRecognizer in Android Also fixed some minor "styles" for normal Button and IconLabelButton (both Android and iOS) * PM-3349 Fix for TabGestureRecognizer not working inside the StackLayout area of IconLabelButton * PM-3349 Fix for Android buttons having all letters in Caps * PM-3349 PM-3350 Started using OnNavigatedTo/From instead of On(Dis)Appearing for LoginPage and LoginSSOPage to avoid the "Modal loading" issues in iOS Also had to add IsInitialized logic to these pages because OnNavigatedTo can be called twice in some scenario. Some minor migrations of Device to DeviceInfo was also done * PM-3350 Fixed iOS extensions (iOS.Extension and iOS.ShareExtension) to load and commented argon2id from debug configuration until we have the .a compiled again with the new platform/arch * PM-3350 Added configurations for Release mode (no FDroid yet) * PM-3349 PM-3350 Migrated remaining AutomationProperties to SemanticProperties. All 'IsInAccessibleTree="True"' were deleted. 'IsInAccessibleTree="False"' were kept and stayed in code. * PM-3349 PM-3350 Changed binding set for CipherViewCell so it updates accordingly * PM-3349 PM-3350 Changed AccountViewCell and its binding to be directly against the ViewModel * PM-3349 Fix for HTML Label on Android. Color hex doesn't need to be cropped anymore. * PM-3350 Fix for colored html text on iOS * PM-3349 PM-3350 Added the partial MAUI Community Toolkit implementation for TouchEffect. This is a temporary solution until they finalize this and add it to their nuget package. This allows implementing the LongPressCommand in AccountSwitchingOverlay and also have the "Ripple effect" animation when touching an item in Android * PM-3349 PM-3350 Changed SendViewCell and its binding to be directly against the ViewModel * PM-3350 Fixed iOS Share extension lazy views loading and an issue with the avatar loading. Also discovered issue with TapGestureRecognizer not working on MAUI Embedding * PM-3350 Fixed iOS Extensions navigation to several pages and improved avoiding duplicate calls to OnNavigatedTo * PM-3350 Updated PCL Crypto to latest alpha version to fix "Dll not found NCrypt" issue * PM-3350 Removed workaround for iOS issue with Avatar icon as it's now fixed in latest .Net8 release. * PM-3349 PM-3350 Removed AsyncCommand "wrapper" and added AsyncRelayCommand directly in all ViewModels that were using the other one. * PM-3350 Added watchOS app to main project and fixed some csproj conditions for runtime identifiers on iOS. * PM-3350 Fixed/Updated all MAUI-Migration TODOs * PM-3350 Fixed account toolbar item and TitleView on SendAddOnlyPage, also removed comments on AvatarImageSource given the workaround is not needed anymore to draw the image successfully. * PM-3350 Updated AppCenter package to latest version 5.0.3 and updated some things into MAUI style * PM-3350 Added workaround for iOS Avatar icon again. * PM-3349 Added workaround for Android to avoid issues with setting MainPage when app is in background. They are now kept on a Queue to be executed after the app has resumed. Updated some things on App.xaml.cs to the new MAUI style * PM-3349 PM-3350 Fixed issue where creating an account with weak/exposed password would get stuck after the Captcha (if a captcha is shown) Changed App.xaml.cs NavigateImpl to be private * PM-3349 Started to configure build.yml for MAUI Android * PM-3349 build.yml update paths for MAUI Android * PM-3349 build.yml commented verify format and just set qa as variant on MAUI Android for faster checks on CI * PM-3349 PM-3350 build.cake updated paths * PM-3349 build.yml updated env helpers variables and set specific csproj to build on Android so not to build iOS extensions * PM-3349 build.yml add Android "prod" variant * PM-3350 build.yml updated iOS build and ignore Android build to try the CI faster * PM-3350 build.yml changed nuget restore for dotnet restore on iOS build to fix issue on restoring due to msbuild * PM-3350 build.yml Upgraded iOS build to run on macos-13 image which has XCode 15, and set the XCode 15 version as currently the default one is 14.x * PM-3350 build.yml try to fix ILLINK warnings and changed image to be macos-13-arm64 to see if the build is faster * PM-3350 build.yml changed image back to be macos-13 to see if the build is faster * PM-3350 Added Document.Build.props to disable trimming on publish * PM-3350 build.yml disable trimming on publish so it's faster * PM-3350 added linkskip for iOS csprojs * PM-3350 iOS projs disable linking and set Newstandkit as weak framework * Update build.yml disabling iOS job to avoid long running process of publish until we can fix that * PM-3349 PM-3350 Workaround to fix issues with text getting cropped/truncated when a Label has both Multiline and LinebreakMode set * PM-3349 build.yml enabled android build workflow * PM-3349 build.yml configured FDROID job for MAUI * PM-3350 iOS extensions TapGestureRecognizer try Window workaround * PM-3350 iOS applied workaround on the iOS Autofill and Share extension to maui embed the navigation page with its content page in the Window * PM-3349 PM-3350 Added workaround for More Options to work on Search and Groupings Page Updated some code to MAUI Style also * PM-3349 PM-3350 Added the ability for users to press "Continue" button as a fallback when using the Yubikey if the "SubmitCommand" doesn't trigger automatically. * PM-3349 PM-3350 Fix for text getting cut/truncated in both account switcher and ciphers/search lists Issue is due to MAUI but can be avoided by using slightly different layout * PM-3350 iOS updated CFBundlerShortVersionString to latest one 2023.10.1 * PM-3350 fix build.yml Bitwarden.ipa AppStore exported file * PM-3350 build.yml added step to validate app for submitting into App Store and have better logs of it * PM-3350 build.yml Added several fixes like not using MtouchUseLLVM on the iOS builds to fix they taking forever to build and some changes on the automation CI to do a debug build for the moment * PM-3350 Improved MTouch linking and extra args on iOS related csprojs * PM-3349 PM-3350 Added MAUI label on self-host settings and on about settings to differentiate from XF app * PM-3349 PM-3350 build.yml uncommented jobs so we have a more complete workflow * PM-3349 PM-3350 Minor change: removed unneeded HorizontalTextAlignment from Label. * PM-3349 Replaced CrossCurrentActivity plugin with MAUI internal CurrentActivity * PM-3350 Fix iOS extensions navigation and Window/RootViewController handling for TapGestureRecognizer to work * PM-3350 Cleared left ClipLogger from the iOS extensions debug logging. * PM-3349 PM-3350 Refactored cipher bindings to have a simpler approach reusing a new CipherItemViewModel to avoid unwanted issues in the app * PM-3349 Added base structure for avoiding Android Autofill crash. This workaround works but it's not complete as it can't handle the entire workflow when showing CipherSelectionPAge (like checking if it should show LockPage) * PM-3350 Bumped iOS version * PM-3350 Changed linker to use default mode given that "Full" is presenting some problems as the linker is stripping things it shouldn't and we're trying to solve it. So for now we will use the mode "Link SDK assemblies only" so QA can test. * PM-3349 Fix for app crashing on Android when Dark mode is enabled Removed unused button style for android * Proof of concept for having multiple window in Android for autofill support and navigating with the help of an Extended splash page. * PM-3350 Fix crash on Release by adding Interpreter on iOS and also adding System.Security.Cryptography to be ignored by the linker * PM-3350 Apply Cryptography TrimmerRootAssembly only to iOS * PM-3350 Updated Plugin.Fingerprint so biometrics work * Update .github/workflows/build.yml setup-xcode commit hash Co-authored-by: Vince Grassia <593223+vgrassia@users.noreply.github.com> * PM-3349 PM-3350 Enabled argon2id and fixed one issue with the Uris when getting the icon image * PM-3349 Upgraded Android targetSdkVersion to 34 * minor change (public to private fields) * minor improvemments on autofill-redirect * PM-3349 Commented the Deploy step for Android job given that we're using the hotfix-rc branch for testing iOS on TestFlight * PM-3349 Uncommented the Deploy step for Android job * PM-3349 Ensure "_isResumed=true" is set on App.xaml.cs:Bootstrap * Reusing App.xaml.cs Navigation for the Android RedirectPage Some other cleanup and changes * Improved autofill workaround to better handle switching between windows. * PM-3349 minor fix to add space in HomePage between the region picker labels. * Added some comments and improvemments. * PM-3349 Added Window events unsubscription of events. Also changed code to avoid potentially having multiple autofillwindow * PM-3349 Minor ui fix (space between buttons in delete account page) * initial commit of android credential provider service (wip) * Revert "initial commit of android credential provider service (wip)" This reverts commit6011b63958. * PM-3350 Fix for Delete Account buttons on iOS * PM-3349 PM-3350 Changed search icon used in app to avoid issue with icon size on iOS * PM-3349 Added custom window so that we can always get the current Active Window. This is used to support the Android Autofil and multi-window scenarios. * PM-3349 Fix for icon and text spacing in some list items * PM-3349 Minor aligment improvemment for region selection in HomePage * PM-3349 Changed the "track color" for the Android switch so that the color is different from the "thumb" * PM-3350 Updated version to 2024.1.0 on iOS * PM-3349 Fix Picker selection style by doing a custom PickerHandler for Android which uses SetSingleChoiceItems(...) to provide with the appropriate UI * PM-3350 Updated MauiVersion to 8.0.4-nightly.* to have the TapGestureRecognizer fix applied. This is done on the Directory.Build.props so we don't have to change it on every csproj. Also removed the workaround of TapGestureHack and fix the Show environment picker to work on the extensions as well. * PM-3350 Added nuget.config so we add the nuget package source for MAUI Nightly builds * Bump main iOS version * PM-3350 Removed "iOS" old folder project that has been moved into the MAUI Single app project. * PM-3350 Improved code safety adding a lot of try...catch and logging throughout the app. Also made the invoking on main thread safer on several places of the app. Additionally, on the GroupingsPageViewModel changed the code removing the old Xamarin hack and just using Replace directly instead of Clearing first to see if that fixes the crash we're having sometimes on the app. * PM-3350 PM-3349 Updated Unit Test projects to NET 8.0 and fixed it to work with Core project reference. Also fixed a test that was breaking due to CIpherKey creation being wrong. Added "UT" as a constant to add when building/running Core.Test project so we have something on the context that tells us that is for a UT. With this I had to remove FFImageLoading on UT context because it doesn't support NET 8.0 * PM-3350 PM-3349 Updated Readme with MAUI and main branch * PM-3350 PM-3349 Enable running Core tests * PM-3350 Fix build.yml format * PM-3349 Fix navigation when coming from autofill with Accessibility Services enabled. The user was getting into Home page instead of where they were, with this workaround the app navigates as if the account has been switched, leaving the user as closely as possible to where they were, basically on the first screen for the current state of the user. * PM-3350 PM-3349 Added property to Directory.Build.props to enable Unit Testing globally so Test runners work * Improve TOTP scan performance on Android * Move Android camera/scan changes to xaml * PM-3350 Testing UseInterpreter false on CI build * PM-3350 Enabled back UseInterpreter on iOS Release given that it crashes on startup without it. * PM-3349 PM-3350 Improved code safety with try...catch, better invoke on main thread and better null handling. * PM-3349 PM-3350 Updated XCode version on build.yml to 15.1 * PM-3350 Removed TapGesture Window MAUI hack from iOS.Extension and iOS.ShareExtension * PM-3350 Fixed CancellationTokenSource proper disposal * PM-3350 Fix Avatar toolbar icon on extensions to load properly and to take advantage of using directly SkiaSharp to do the native conversion to UIImage. Also improved the toolbar item so that size is set appropriately. * PM-3349 PM-3350 Fix external link icon * PM-3350 Added new style to prevent spell check and text prediction * Fix merge from main * PM-3350 Commented event collection upload on the timer and when sending the app to background to see if that prevents the app from crashing on release mode. * PM-3350 Added check for state migration version before trying to migrate LiteDB values into Prefs when there's no need to and that may be inducing crashes on backgrounded iOS apps. * PM-3350 Try to disable Interpreter to have better crash knowledge. This time testing if avoiding loading the argon2id lib we're able to not use the interpreter. * PM-5928 Fix circle animation to be shown on verification codes list on each item * PM-3350 Go back to use Interpreter and added some Directory.Build.props to easily change Codesign properties and also include/exclude iOS extensions / WatchOS from the build. * PM-3350 Enabled iOS extensions and WatchOS app to be included based on the Directory.Build.props * PM-3350 Go back to include argon2id and interpreter * Removing error/loading placeholders of icons on the cells to see if that is causing the background crash on iOS; so we can test this in TestFlight * [PM-5910] Workaround for for sliding elements in Duo 2FA flow (#2967) * workaround for sliding elements in duo 2fa flow * restrict workaround to Android * restrict workaround to Android * Revert "restrict workaround to Android" This reverts commitc2753d5dc4. * Revert "restrict workaround to Android" This reverts commit69688cfb98. * PM-5902 fix for account switcher not dismissing when tapping outside (#2974) * PM-3350 Fix iossimulator-x64 argon2id load so we can test on simulators and also made easier to maintain loading the argon2id library on the iOS projects by setting a general Directory.Build.props that is shared. * PM-5903 Changed App.xaml.cs SetOption to only update the needed properties instead of replacing the existing Options object which would cause the AccountSwitcher button bug (#2973) * [PM-5896] Fix MAUI iOS Background crash due to lock files on suspension (#2969) * PM-5896 Fix background crash on iOS due to lock files when app gets suspended. Changed loading and error placeholders of the CachedImage to not be used and use default icon of IconLabel instead changing visibility. * PM-5896 Changed methods to be protected so that they don't get removed by the linker. * PM-5896 Added stub class and references to it so to have stronger references to Icon_Success and Icon_Error so the linker doesn't remove them. * PM-3349 Removed commented code from build.yml regarding FDroid that is not needed anymore. * PM-6077 Separated Android and iOS HybridWebViewHandler so that it can be used on iOS.Core (#2983) * [PM-5907] Fix for incorrect TOTP white text color on label when using light theme on iOS (#2982) * PM-5907 workaround for incorrect textcolor when programmatically changing text on Entry * Update src/Core/Pages/Vault/CipherAddEditPage.xaml.cs Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> --------- Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> * [PM-5906] Fix for incorrect Send MaxAccess white text color on label when using light theme on iOS (#2981) * PM-5906 workaround for incorrect textcolor when programmatically changing text on Entry * Update src/Core/Pages/Send/SendAddEditPage.xaml.cs Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> --------- Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> * PM-3349 PM-3350 Fixed Unit tests because of referencing FFImageLoading when it's not possible --------- Co-authored-by: Dinis Vieira <dinisvieira@outlook.com> Co-authored-by: Vince Grassia <593223+vgrassia@users.noreply.github.com> Co-authored-by: mpbw2 <59324545+mpbw2@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
f30158adf5
commit
39a34bd8c4
@@ -16,6 +16,7 @@ using Bit.Core.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using DeviceType = Bit.Core.Enums.DeviceType;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
|
||||
42
src/Core/Services/BaseBiometricService.cs
Normal file
42
src/Core/Services/BaseBiometricService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public abstract class BaseBiometricService : IBiometricService
|
||||
{
|
||||
protected readonly IStateService _stateService;
|
||||
protected readonly ICryptoService _cryptoService;
|
||||
|
||||
protected BaseBiometricService(IStateService stateService, ICryptoService cryptoService)
|
||||
{
|
||||
_stateService = stateService;
|
||||
_cryptoService = cryptoService;
|
||||
}
|
||||
|
||||
public async Task<bool> CanUseBiometricsUnlockAsync()
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
return await _cryptoService.GetBiometricUnlockKeyAsync() != null || await _stateService.GetKeyEncryptedAsync() != null;
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
}
|
||||
|
||||
public async Task SetCanUnlockWithBiometricsAsync(bool canUnlockWithBiometrics)
|
||||
{
|
||||
if (canUnlockWithBiometrics)
|
||||
{
|
||||
await SetupBiometricAsync();
|
||||
await _stateService.SetBiometricUnlockAsync(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _stateService.SetBiometricUnlockAsync(null);
|
||||
}
|
||||
await _stateService.SetBiometricLockedAsync(false);
|
||||
await _cryptoService.RefreshKeysAsync();
|
||||
}
|
||||
|
||||
public abstract Task<bool> IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null);
|
||||
public abstract Task<bool> SetupBiometricAsync(string bioIntegritySrcKey = null);
|
||||
}
|
||||
}
|
||||
142
src/Core/Services/BaseWatchDeviceService.cs
Normal file
142
src/Core/Services/BaseWatchDeviceService.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Core.Models.View;
|
||||
using MessagePack;
|
||||
using MessagePack.Resolvers;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public abstract class BaseWatchDeviceService : IWatchDeviceService
|
||||
{
|
||||
private readonly ICipherService _cipherService;
|
||||
private readonly IEnvironmentService _environmentService;
|
||||
private readonly IStateService _stateService;
|
||||
private readonly IVaultTimeoutService _vaultTimeoutService;
|
||||
|
||||
protected BaseWatchDeviceService(ICipherService cipherService,
|
||||
IEnvironmentService environmentService,
|
||||
IStateService stateService,
|
||||
IVaultTimeoutService vaultTimeoutService)
|
||||
{
|
||||
_cipherService = cipherService;
|
||||
_environmentService = environmentService;
|
||||
_stateService = stateService;
|
||||
_vaultTimeoutService = vaultTimeoutService;
|
||||
}
|
||||
|
||||
public abstract bool IsConnected { get; }
|
||||
|
||||
protected abstract bool CanSendData { get; }
|
||||
protected abstract bool IsSupported { get; }
|
||||
|
||||
public async Task SyncDataToWatchAsync()
|
||||
{
|
||||
if (!IsSupported)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var shouldConnect = await _stateService.GetShouldConnectToWatchAsync();
|
||||
if (shouldConnect && !IsConnected)
|
||||
{
|
||||
ConnectToWatch();
|
||||
}
|
||||
|
||||
if (!CanSendData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var userData = await _stateService.GetActiveUserCustomDataAsync(a => a?.Profile is null ? null : new WatchDTO.UserDataDto
|
||||
{
|
||||
Id = a.Profile.UserId,
|
||||
Name = a.Profile.Name,
|
||||
Email = a.Profile.Email
|
||||
});
|
||||
var state = await GetStateAsync(userData?.Id, shouldConnect);
|
||||
if (state != WatchState.Valid)
|
||||
{
|
||||
await SendDataToWatchAsync(new WatchDTO(state));
|
||||
return;
|
||||
}
|
||||
|
||||
var ciphersWithTotp = await _cipherService.GetAllDecryptedAsync(c => c.DeletedDate == null && c.Login?.Totp != null);
|
||||
|
||||
if (!ciphersWithTotp.Any())
|
||||
{
|
||||
await SendDataToWatchAsync(new WatchDTO(WatchState.Need2FAItem));
|
||||
return;
|
||||
}
|
||||
|
||||
var watchDto = new WatchDTO(state)
|
||||
{
|
||||
Ciphers = ciphersWithTotp.Select(c => new SimpleCipherView(c)).ToList(),
|
||||
UserData = userData,
|
||||
EnvironmentData = new WatchDTO.EnvironmentUrlDataDto
|
||||
{
|
||||
Base = _environmentService.BaseUrl,
|
||||
Icons = _environmentService.IconsUrl
|
||||
}
|
||||
//SettingsData = new WatchDTO.SettingsDataDto
|
||||
//{
|
||||
// VaultTimeoutInMinutes = await _vaultTimeoutService.GetVaultTimeout(userData?.Id),
|
||||
// VaultTimeoutAction = await _stateService.GetVaultTimeoutActionAsync(userData?.Id) ?? VaultTimeoutAction.Lock
|
||||
//}
|
||||
};
|
||||
await SendDataToWatchAsync(watchDto);
|
||||
}
|
||||
|
||||
private async Task<WatchState> GetStateAsync(string userId, bool shouldConnectToWatch)
|
||||
{
|
||||
if (await _stateService.GetLastUserShouldConnectToWatchAsync()
|
||||
&&
|
||||
(userId is null || !await _stateService.IsAuthenticatedAsync()))
|
||||
{
|
||||
// if the last user had "Connect to Watch" enabled and there's no user authenticated
|
||||
return WatchState.NeedLogin;
|
||||
}
|
||||
|
||||
if (!shouldConnectToWatch)
|
||||
{
|
||||
return WatchState.NeedSetup;
|
||||
}
|
||||
|
||||
//if (await _vaultTimeoutService.IsLockedAsync() ||
|
||||
// await _vaultTimeoutService.ShouldLockAsync())
|
||||
//{
|
||||
// return WatchState.NeedUnlock;
|
||||
//}
|
||||
|
||||
if (!await _stateService.CanAccessPremiumAsync(userId))
|
||||
{
|
||||
return WatchState.NeedPremium;
|
||||
}
|
||||
|
||||
return WatchState.Valid;
|
||||
}
|
||||
|
||||
public async Task SetShouldConnectToWatchAsync(bool shouldConnectToWatch)
|
||||
{
|
||||
await _stateService.SetShouldConnectToWatchAsync(shouldConnectToWatch);
|
||||
await SyncDataToWatchAsync();
|
||||
}
|
||||
|
||||
protected async Task SendDataToWatchAsync(WatchDTO watchDto)
|
||||
{
|
||||
var options = MessagePackSerializerOptions.Standard
|
||||
.WithResolver(CompositeResolver.Create(
|
||||
GeneratedResolver.Instance,
|
||||
StandardResolver.Instance
|
||||
));
|
||||
|
||||
await SendDataToWatchAsync(MessagePackSerializer.Serialize(watchDto, options));
|
||||
}
|
||||
|
||||
protected abstract Task SendDataToWatchAsync(byte[] rawData);
|
||||
|
||||
protected abstract void ConnectToWatch();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ using Bit.Core.Models.Request;
|
||||
using Bit.Core.Models.Response;
|
||||
using Bit.Core.Models.View;
|
||||
using Bit.Core.Utilities;
|
||||
using View = Bit.Core.Models.View.View;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
|
||||
@@ -8,6 +8,7 @@ using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Domain;
|
||||
using Bit.Core.Models.View;
|
||||
using Bit.Core.Utilities;
|
||||
using CollectionView = Bit.Core.Models.View.CollectionView;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
|
||||
30
src/Core/Services/DeepLinkContext.cs
Normal file
30
src/Core/Services/DeepLinkContext.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class DeepLinkContext : IDeepLinkContext
|
||||
{
|
||||
public const string NEW_OTP_MESSAGE = "handleOTPUriMessage";
|
||||
|
||||
private readonly IMessagingService _messagingService;
|
||||
|
||||
public DeepLinkContext(IMessagingService messagingService)
|
||||
{
|
||||
_messagingService = messagingService;
|
||||
}
|
||||
|
||||
public bool OnNewUri(Uri uri)
|
||||
{
|
||||
if (uri.Scheme == Constants.OtpAuthScheme)
|
||||
{
|
||||
_messagingService.Send(NEW_OTP_MESSAGE, uri.AbsoluteUri);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Domain;
|
||||
using Bit.Core.Utilities;
|
||||
using BwRegion = Bit.Core.Enums.Region;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@@ -34,7 +31,7 @@ namespace Bit.Core.Services
|
||||
public string IconsUrl { get; set; }
|
||||
public string NotificationsUrl { get; set; }
|
||||
public string EventsUrl { get; set; }
|
||||
public Region SelectedRegion { get; set; }
|
||||
public BwRegion SelectedRegion { get; set; }
|
||||
|
||||
public string GetWebVaultUrl(bool returnNullIfDefault = false)
|
||||
{
|
||||
@@ -77,7 +74,7 @@ namespace Bit.Core.Services
|
||||
|
||||
if (urls == null || urls.IsEmpty || region is null)
|
||||
{
|
||||
await SetRegionAsync(Region.US);
|
||||
await SetRegionAsync(BwRegion.US);
|
||||
_conditionedAwaiterManager.SetAsCompleted(AwaiterPrecondition.EnvironmentUrlsInited);
|
||||
return;
|
||||
}
|
||||
@@ -93,16 +90,16 @@ namespace Bit.Core.Services
|
||||
|
||||
}
|
||||
|
||||
public async Task<EnvironmentUrlData> SetRegionAsync(Region region, EnvironmentUrlData selfHostedUrls = null)
|
||||
public async Task<EnvironmentUrlData> SetRegionAsync(BwRegion region, EnvironmentUrlData selfHostedUrls = null)
|
||||
{
|
||||
EnvironmentUrlData urls;
|
||||
|
||||
if (region == Region.SelfHosted)
|
||||
if (region == BwRegion.SelfHosted)
|
||||
{
|
||||
// If user saves a self-hosted region with empty fields, default to US
|
||||
if (selfHostedUrls.IsEmpty)
|
||||
{
|
||||
return await SetRegionAsync(Region.US);
|
||||
return await SetRegionAsync(BwRegion.US);
|
||||
}
|
||||
urls = selfHostedUrls.FormatUrls();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
#if !FDROID
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AppCenter;
|
||||
@@ -16,8 +11,11 @@ namespace Bit.Core.Services
|
||||
{
|
||||
public class Logger : ILogger
|
||||
{
|
||||
private const string iOSAppSecret = "51f96ae5-68ba-45f6-99a1-8ad9f63046c3";
|
||||
private const string DroidAppSecret = "d3834185-b4a6-4347-9047-b86c65293d42";
|
||||
#if IOS
|
||||
private const string AppSecret = "51f96ae5-68ba-45f6-99a1-8ad9f63046c3";
|
||||
#else
|
||||
private const string AppSecret = "d3834185-b4a6-4347-9047-b86c65293d42";
|
||||
#endif
|
||||
|
||||
private string _userId;
|
||||
private string _appId;
|
||||
@@ -40,7 +38,6 @@ namespace Bit.Core.Services
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public string Description
|
||||
{
|
||||
get
|
||||
@@ -60,22 +57,10 @@ namespace Bit.Core.Services
|
||||
return;
|
||||
}
|
||||
|
||||
var device = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService").GetDevice();
|
||||
_userId = await ServiceContainer.Resolve<IStateService>("stateService").GetActiveUserIdAsync();
|
||||
_appId = await ServiceContainer.Resolve<IAppIdService>("appIdService").GetAppIdAsync();
|
||||
_userId = await ServiceContainer.Resolve<IStateService>().GetActiveUserIdAsync();
|
||||
_appId = await ServiceContainer.Resolve<IAppIdService>().GetAppIdAsync();
|
||||
|
||||
switch (device)
|
||||
{
|
||||
case Enums.DeviceType.Android:
|
||||
AppCenter.Start(DroidAppSecret, typeof(Crashes));
|
||||
break;
|
||||
case Enums.DeviceType.iOS:
|
||||
AppCenter.Start(iOSAppSecret, typeof(Crashes));
|
||||
break;
|
||||
default:
|
||||
throw new AppCenterException("Cannot start AppCenter. Device type is not configured.");
|
||||
|
||||
}
|
||||
AppCenter.Start(AppSecret, typeof(Crashes));
|
||||
|
||||
AppCenter.SetUserId(_userId);
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
|
||||
21
src/Core/Services/MobileBroadcasterMessagingService.cs
Normal file
21
src/Core/Services/MobileBroadcasterMessagingService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Models.Domain;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobileBroadcasterMessagingService : IMessagingService
|
||||
{
|
||||
private readonly IBroadcasterService _broadcasterService;
|
||||
|
||||
public MobileBroadcasterMessagingService(IBroadcasterService broadcasterService)
|
||||
{
|
||||
_broadcasterService = broadcasterService;
|
||||
}
|
||||
|
||||
public void Send(string subscriber, object arg = null)
|
||||
{
|
||||
var message = new Message { Command = subscriber, Data = arg };
|
||||
_broadcasterService.Send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
153
src/Core/Services/MobileI18nService.cs
Normal file
153
src/Core/Services/MobileI18nService.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using System.Threading;
|
||||
using Bit.Core.Resources.Localization;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobileI18nService : II18nService
|
||||
{
|
||||
private const string ResourceId = "Bit.Core.Resources.Localization.AppResources";
|
||||
|
||||
private static readonly Lazy<ResourceManager> _resourceManager = new Lazy<ResourceManager>(() =>
|
||||
new ResourceManager(ResourceId, IntrospectionExtensions.GetTypeInfo(typeof(MobileI18nService)).Assembly));
|
||||
|
||||
private readonly CultureInfo _defaultCulture = new CultureInfo("en-US");
|
||||
private bool _inited;
|
||||
private StringComparer _stringComparer;
|
||||
private Dictionary<string, string> _localeNames;
|
||||
|
||||
public MobileI18nService(CultureInfo systemCulture)
|
||||
{
|
||||
Culture = systemCulture;
|
||||
}
|
||||
|
||||
public CultureInfo Culture { get; set; }
|
||||
public StringComparer StringComparer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_stringComparer == null)
|
||||
{
|
||||
_stringComparer = StringComparer.Create(Culture, false);
|
||||
}
|
||||
return _stringComparer;
|
||||
}
|
||||
}
|
||||
public Dictionary<string, string> LocaleNames
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_localeNames == null)
|
||||
{
|
||||
_localeNames = new Dictionary<string, string>
|
||||
{
|
||||
["af"] = "Afrikaans",
|
||||
["be"] = "Беларуская",
|
||||
["bg"] = "български",
|
||||
["ca"] = "català",
|
||||
["cs"] = "čeština",
|
||||
["da"] = "Dansk",
|
||||
["de"] = "Deutsch",
|
||||
["el"] = "Ελληνικά",
|
||||
["en"] = "English",
|
||||
["en-GB"] = "English (British)",
|
||||
["eo"] = "Esperanto",
|
||||
["es"] = "Español",
|
||||
["et"] = "eesti",
|
||||
["fa"] = "فارسی",
|
||||
["fi"] = "suomi",
|
||||
["fr"] = "Français",
|
||||
["he"] = "עברית",
|
||||
["hi"] = "हिन्दी",
|
||||
["hr"] = "hrvatski",
|
||||
["hu"] = "magyar",
|
||||
["id"] = "Bahasa Indonesia",
|
||||
["it"] = "Italiano",
|
||||
["ja"] = "日本語",
|
||||
["ko"] = "한국어",
|
||||
["lv"] = "Latvietis",
|
||||
["ml"] = "മലയാളം",
|
||||
["nb"] = "norsk (bokmål)",
|
||||
["nl"] = "Nederlands",
|
||||
["pl"] = "Polski",
|
||||
["pt-BR"] = "Português do Brasil",
|
||||
["pt-PT"] = "Português",
|
||||
["ro"] = "română",
|
||||
["ru"] = "русский",
|
||||
["sk"] = "slovenčina",
|
||||
["sv"] = "svenska",
|
||||
["th"] = "ไทย",
|
||||
["tr"] = "Türkçe",
|
||||
["uk"] = "українська",
|
||||
["vi"] = "Tiếng Việt",
|
||||
["zh-CN"] = "中文(中国大陆)",
|
||||
["zh-TW"] = "中文(台灣)"
|
||||
};
|
||||
}
|
||||
return _localeNames;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(CultureInfo culture = null)
|
||||
{
|
||||
if (_inited)
|
||||
{
|
||||
throw new Exception("I18n already inited.");
|
||||
}
|
||||
_inited = true;
|
||||
SetCurrentCulture(culture);
|
||||
}
|
||||
|
||||
public void SetCurrentCulture(CultureInfo culture)
|
||||
{
|
||||
if (culture != null)
|
||||
{
|
||||
Culture = culture;
|
||||
}
|
||||
|
||||
AppResources.Culture = Culture;
|
||||
Thread.CurrentThread.CurrentCulture = Culture;
|
||||
Thread.CurrentThread.CurrentUICulture = Culture;
|
||||
}
|
||||
|
||||
public string T(string id, string p1 = null, string p2 = null, string p3 = null)
|
||||
{
|
||||
return Translate(id, p1, p2, p3);
|
||||
}
|
||||
|
||||
public string Translate(string id, string p1 = null, string p2 = null, string p3 = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var result = _resourceManager.Value.GetString(id, Culture);
|
||||
if (result == null)
|
||||
{
|
||||
result = _resourceManager.Value.GetString(id, _defaultCulture);
|
||||
if (result == null)
|
||||
{
|
||||
result = $"{{{id}}}";
|
||||
}
|
||||
}
|
||||
if (p1 == null && p2 == null && p3 == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else if (p2 == null && p3 == null)
|
||||
{
|
||||
return string.Format(result, p1);
|
||||
}
|
||||
else if (p3 == null)
|
||||
{
|
||||
return string.Format(result, p1, p2);
|
||||
}
|
||||
return string.Format(result, p1, p2, p3);
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/Core/Services/MobilePasswordRepromptService.cs
Normal file
65
src/Core/Services/MobilePasswordRepromptService.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.Core.Resources.Localization;
|
||||
using Bit.App.Utilities;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobilePasswordRepromptService : IPasswordRepromptService
|
||||
{
|
||||
private readonly IPlatformUtilsService _platformUtilsService;
|
||||
private readonly ICryptoService _cryptoService;
|
||||
private readonly IStateService _stateService;
|
||||
|
||||
public MobilePasswordRepromptService(IPlatformUtilsService platformUtilsService, ICryptoService cryptoService, IStateService stateService)
|
||||
{
|
||||
_platformUtilsService = platformUtilsService;
|
||||
_cryptoService = cryptoService;
|
||||
_stateService = stateService;
|
||||
}
|
||||
|
||||
public string[] ProtectedFields { get; } = { "LoginTotp", "LoginPassword", "H_FieldValue", "CardNumber", "CardCode" };
|
||||
|
||||
public async Task<bool> PromptAndCheckPasswordIfNeededAsync(CipherRepromptType repromptType = CipherRepromptType.Password)
|
||||
{
|
||||
if (repromptType == CipherRepromptType.None || await ShouldByPassMasterPasswordRepromptAsync())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return await _platformUtilsService.ShowPasswordDialogAsync(AppResources.PasswordConfirmation, AppResources.PasswordConfirmationDesc, ValidatePasswordAsync);
|
||||
}
|
||||
|
||||
public async Task<(string password, bool valid)> ShowPasswordPromptAndGetItAsync()
|
||||
{
|
||||
return await _platformUtilsService.ShowPasswordDialogAndGetItAsync(AppResources.PasswordConfirmation, AppResources.PasswordConfirmationDesc, ValidatePasswordAsync);
|
||||
}
|
||||
|
||||
private async Task<bool> ValidatePasswordAsync(string password)
|
||||
{
|
||||
// Assume user has canceled.
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
return false;
|
||||
};
|
||||
|
||||
var masterKey = await _cryptoService.GetOrDeriveMasterKeyAsync(password);
|
||||
var passwordValid = await _cryptoService.CompareAndUpdateKeyHashAsync(password, masterKey);
|
||||
if (passwordValid)
|
||||
{
|
||||
await AppHelpers.ResetInvalidUnlockAttemptsAsync();
|
||||
|
||||
await _cryptoService.UpdateMasterKeyAndUserKeyAsync(masterKey);
|
||||
}
|
||||
|
||||
return passwordValid;
|
||||
}
|
||||
|
||||
private async Task<bool> ShouldByPassMasterPasswordRepromptAsync()
|
||||
{
|
||||
return await _cryptoService.GetMasterKeyHashAsync() is null;
|
||||
}
|
||||
}
|
||||
}
|
||||
293
src/Core/Services/MobilePlatformUtilsService.cs
Normal file
293
src/Core/Services/MobilePlatformUtilsService.cs
Normal file
@@ -0,0 +1,293 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models;
|
||||
using Bit.Core.Resources.Localization;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Utilities;
|
||||
using Plugin.Fingerprint;
|
||||
using Plugin.Fingerprint.Abstractions;
|
||||
using Microsoft.Maui.ApplicationModel.DataTransfer;
|
||||
using Microsoft.Maui.ApplicationModel;
|
||||
using Microsoft.Maui.Devices;
|
||||
using Microsoft.Maui.Controls;
|
||||
using Microsoft.Maui;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobilePlatformUtilsService : IPlatformUtilsService
|
||||
{
|
||||
private static readonly Random _random = new Random();
|
||||
|
||||
private const int DialogPromiseExpiration = 600000; // 10 minutes
|
||||
|
||||
private readonly IDeviceActionService _deviceActionService;
|
||||
private readonly IClipboardService _clipboardService;
|
||||
private readonly IMessagingService _messagingService;
|
||||
private readonly IBroadcasterService _broadcasterService;
|
||||
|
||||
private readonly Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>> _showDialogResolves =
|
||||
new Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>>();
|
||||
|
||||
public MobilePlatformUtilsService(
|
||||
IDeviceActionService deviceActionService,
|
||||
IClipboardService clipboardService,
|
||||
IMessagingService messagingService,
|
||||
IBroadcasterService broadcasterService
|
||||
)
|
||||
{
|
||||
_deviceActionService = deviceActionService;
|
||||
_clipboardService = clipboardService;
|
||||
_messagingService = messagingService;
|
||||
_broadcasterService = broadcasterService;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
_broadcasterService.Subscribe(nameof(MobilePlatformUtilsService), (message) =>
|
||||
{
|
||||
if (message.Command == "showDialogResolve")
|
||||
{
|
||||
var details = message.Data as Tuple<int, bool>;
|
||||
var dialogId = details.Item1;
|
||||
var confirmed = details.Item2;
|
||||
if (_showDialogResolves.ContainsKey(dialogId))
|
||||
{
|
||||
var resolveObj = _showDialogResolves[dialogId].Item1;
|
||||
resolveObj.TrySetResult(confirmed);
|
||||
}
|
||||
|
||||
// Clean up old tasks
|
||||
var deleteIds = new HashSet<int>();
|
||||
foreach (var item in _showDialogResolves)
|
||||
{
|
||||
var age = DateTime.UtcNow - item.Value.Item2;
|
||||
if (age.TotalMilliseconds > DialogPromiseExpiration)
|
||||
{
|
||||
deleteIds.Add(item.Key);
|
||||
}
|
||||
}
|
||||
foreach (var id in deleteIds)
|
||||
{
|
||||
_showDialogResolves.Remove(id);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the device type on the server enum
|
||||
/// </summary>
|
||||
public Core.Enums.DeviceType GetDevice()
|
||||
{
|
||||
// Can't use Device.RuntimePlatform here because it gets called before Forms.Init() and throws.
|
||||
// so we need to get the DeviceType ourselves
|
||||
return _deviceActionService.DeviceType;
|
||||
}
|
||||
|
||||
public string GetDeviceString()
|
||||
{
|
||||
return DeviceInfo.Model;
|
||||
}
|
||||
|
||||
public ClientType GetClientType()
|
||||
{
|
||||
return ClientType.Mobile;
|
||||
}
|
||||
|
||||
public bool IsViewOpen()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void LaunchUri(string uri, Dictionary<string, object> options = null)
|
||||
{
|
||||
if ((uri.StartsWith("http://") || uri.StartsWith("https://")) &&
|
||||
Uri.TryCreate(uri, UriKind.Absolute, out var parsedUri))
|
||||
{
|
||||
try
|
||||
{
|
||||
Browser.OpenAsync(uri, BrowserLaunchMode.External);
|
||||
}
|
||||
catch (FeatureNotSupportedException) { }
|
||||
}
|
||||
else
|
||||
{
|
||||
var launched = false;
|
||||
if (GetDevice() == Core.Enums.DeviceType.Android && uri.StartsWith("androidapp://"))
|
||||
{
|
||||
launched = _deviceActionService.LaunchApp(uri);
|
||||
}
|
||||
if (!launched && (options?.ContainsKey("page") ?? false))
|
||||
{
|
||||
(options["page"] as Page).DisplayAlert(null, "", ""); // TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetApplicationVersion()
|
||||
{
|
||||
return AppInfo.VersionString;
|
||||
}
|
||||
|
||||
public bool SupportsDuo()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ShowToastForCopiedValue(string valueNameCopied)
|
||||
{
|
||||
ShowToast("info", null, string.Format(AppResources.ValueHasBeenCopied, valueNameCopied));
|
||||
}
|
||||
|
||||
public bool SupportsFido2()
|
||||
{
|
||||
return _deviceActionService.SupportsFido2();
|
||||
}
|
||||
|
||||
public void ShowToast(string type, string title, string text, Dictionary<string, object> options = null)
|
||||
{
|
||||
ShowToast(type, title, new string[] { text }, options);
|
||||
}
|
||||
|
||||
public void ShowToast(string type, string title, string[] text, Dictionary<string, object> options = null)
|
||||
{
|
||||
if (text.Length > 0)
|
||||
{
|
||||
var longDuration = options != null && options.ContainsKey("longDuration") ?
|
||||
(bool)options["longDuration"] : false;
|
||||
_deviceActionService.Toast(text[0], longDuration);
|
||||
}
|
||||
}
|
||||
|
||||
public Task<bool> ShowDialogAsync(string text, string title = null, string confirmText = null,
|
||||
string cancelText = null, string type = null)
|
||||
{
|
||||
var dialogId = 0;
|
||||
lock (_random)
|
||||
{
|
||||
dialogId = _random.Next(0, int.MaxValue);
|
||||
}
|
||||
_messagingService.Send("showDialog", new DialogDetails
|
||||
{
|
||||
Text = text,
|
||||
Title = title,
|
||||
ConfirmText = confirmText,
|
||||
CancelText = cancelText,
|
||||
Type = type,
|
||||
DialogId = dialogId
|
||||
});
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
_showDialogResolves.Add(dialogId, new Tuple<TaskCompletionSource<bool>, DateTime>(tcs, DateTime.UtcNow));
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public async Task<bool> ShowPasswordDialogAsync(string title, string body, Func<string, Task<bool>> validator)
|
||||
{
|
||||
return (await ShowPasswordDialogAndGetItAsync(title, body, validator)).valid;
|
||||
}
|
||||
|
||||
public async Task<(string password, bool valid)> ShowPasswordDialogAndGetItAsync(string title, string body, Func<string, Task<bool>> validator)
|
||||
{
|
||||
var password = await _deviceActionService.DisplayPromptAync(AppResources.PasswordConfirmation,
|
||||
AppResources.PasswordConfirmationDesc, null, AppResources.Submit, AppResources.Cancel, password: true);
|
||||
|
||||
if (password == null)
|
||||
{
|
||||
return (password, false);
|
||||
}
|
||||
|
||||
var valid = await validator(password);
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
await ShowDialogAsync(AppResources.InvalidMasterPassword, null, AppResources.Ok);
|
||||
}
|
||||
|
||||
return (password, valid);
|
||||
}
|
||||
|
||||
public bool IsSelfHost()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<string> ReadFromClipboardAsync(Dictionary<string, object> options = null)
|
||||
{
|
||||
return await Clipboard.GetTextAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> SupportsBiometricAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await CrossFingerprint.Current.IsAvailableAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsBiometricIntegrityValidAsync(string bioIntegritySrcKey = null)
|
||||
{
|
||||
bioIntegritySrcKey ??= Core.Constants.BiometricIntegritySourceKey;
|
||||
|
||||
var biometricService = ServiceContainer.Resolve<IBiometricService>();
|
||||
if (!await biometricService.IsSystemBiometricIntegrityValidAsync(bioIntegritySrcKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var stateService = ServiceContainer.Resolve<IStateService>();
|
||||
return await stateService.IsAccountBiometricIntegrityValidAsync(bioIntegritySrcKey);
|
||||
}
|
||||
|
||||
public async Task<bool> AuthenticateBiometricAsync(string text = null, string fallbackText = null,
|
||||
Action fallback = null, bool logOutOnTooManyAttempts = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
text = AppResources.BiometricsDirection;
|
||||
// TODO Xamarin.Forms.Device.RuntimePlatform is no longer supported. Use Microsoft.Maui.Devices.DeviceInfo.Platform instead. For more details see https://learn.microsoft.com/en-us/dotnet/maui/migration/forms-projects#device-changes
|
||||
if (Device.RuntimePlatform == Device.iOS)
|
||||
{
|
||||
var supportsFace = await _deviceActionService.SupportsFaceBiometricAsync();
|
||||
text = supportsFace ? AppResources.FaceIDDirection : AppResources.FingerprintDirection;
|
||||
}
|
||||
}
|
||||
var biometricRequest = new AuthenticationRequestConfiguration(AppResources.Bitwarden, text)
|
||||
{
|
||||
CancelTitle = AppResources.Cancel,
|
||||
FallbackTitle = fallbackText
|
||||
};
|
||||
var result = await CrossFingerprint.Current.AuthenticateAsync(biometricRequest);
|
||||
if (result.Authenticated)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (result.Status == FingerprintAuthenticationResultStatus.FallbackRequested)
|
||||
{
|
||||
fallback?.Invoke();
|
||||
}
|
||||
if (result.Status == FingerprintAuthenticationResultStatus.TooManyAttempts
|
||||
&& logOutOnTooManyAttempts)
|
||||
{
|
||||
await ShowDialogAsync(AppResources.AccountLoggedOutBiometricExceeded, AppResources.TooManyAttempts, AppResources.Ok);
|
||||
_messagingService.Send(AccountsManagerMessageCommands.LOGOUT);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return false;
|
||||
}
|
||||
|
||||
public long GetActiveTime()
|
||||
{
|
||||
return _deviceActionService.GetActiveTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
103
src/Core/Services/MobileStorageService.cs
Normal file
103
src/Core/Services/MobileStorageService.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobileStorageService : IStorageService, IDisposable
|
||||
{
|
||||
private readonly IStorageService _preferencesStorageService;
|
||||
private readonly IStorageService _liteDbStorageService;
|
||||
|
||||
private readonly HashSet<string> _liteDbStorageKeys = new HashSet<string>
|
||||
{
|
||||
Constants.EventCollectionKey,
|
||||
Constants.CiphersKey(""),
|
||||
Constants.FoldersKey(""),
|
||||
Constants.CollectionsKey(""),
|
||||
Constants.CiphersLocalDataKey(""),
|
||||
Constants.SendsKey(""),
|
||||
Constants.PassGenHistoryKey(""),
|
||||
Constants.SettingsKey(""),
|
||||
};
|
||||
|
||||
public MobileStorageService(
|
||||
IStorageService preferenceStorageService,
|
||||
IStorageService liteDbStorageService)
|
||||
{
|
||||
_preferencesStorageService = preferenceStorageService;
|
||||
_liteDbStorageService = liteDbStorageService;
|
||||
}
|
||||
|
||||
public async Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
if (IsLiteDbKey(key))
|
||||
{
|
||||
return await _liteDbStorageService.GetAsync<T>(key);
|
||||
}
|
||||
|
||||
return await _preferencesStorageService.GetAsync<T>(key) ?? await TryMigrateLiteDbToPrefsAsync<T>(key);
|
||||
}
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if (IsLiteDbKey(key))
|
||||
{
|
||||
return _liteDbStorageService.SaveAsync(key, obj);
|
||||
}
|
||||
return _preferencesStorageService.SaveAsync(key, obj);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
if (IsLiteDbKey(key))
|
||||
{
|
||||
return _liteDbStorageService.RemoveAsync(key);
|
||||
}
|
||||
return _preferencesStorageService.RemoveAsync(key);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_liteDbStorageService is IDisposable disposableLiteDbService)
|
||||
{
|
||||
disposableLiteDbService.Dispose();
|
||||
}
|
||||
if (_preferencesStorageService is IDisposable disposablePrefService)
|
||||
{
|
||||
disposablePrefService.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
private bool IsLiteDbKey(string key)
|
||||
{
|
||||
return _liteDbStorageKeys.Any(key.StartsWith) ||
|
||||
_liteDbStorageKeys.Contains(key);
|
||||
}
|
||||
|
||||
private async Task<T> TryMigrateLiteDbToPrefsAsync<T>(string key)
|
||||
{
|
||||
// Note: this is added to prevent searching a value in LiteDB when the migration has already run and it's in its latest version.
|
||||
// If not, we could get several concurrent calls to the DB asking for values we already know they are not there,
|
||||
// possible causing crashes on backgrounded apps.
|
||||
if (await _preferencesStorageService.GetAsync<int?>(Constants.StateVersionKey) == Constants.LatestStateVersion)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var currentValue = await _liteDbStorageService.GetAsync<T>(key);
|
||||
if (currentValue != null)
|
||||
{
|
||||
await _preferencesStorageService.SaveAsync(key, currentValue);
|
||||
await _liteDbStorageService.RemoveAsync(key);
|
||||
}
|
||||
|
||||
return currentValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/Core/Services/NoopPushNotificationListenerService.cs
Normal file
43
src/Core/Services/NoopPushNotificationListenerService.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class NoopPushNotificationListenerService : IPushNotificationListenerService
|
||||
{
|
||||
public Task OnMessageAsync(JObject value, string deviceType)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task OnRegisteredAsync(string token, string deviceType)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public void OnUnregistered(string deviceType)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnError(string message, string deviceType)
|
||||
{
|
||||
}
|
||||
|
||||
public bool ShouldShowNotification()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task OnNotificationTapped(BaseNotificationData data)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task OnNotificationDismissed(BaseNotificationData data)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/Core/Services/NoopPushNotificationService.cs
Normal file
36
src/Core/Services/NoopPushNotificationService.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class NoopPushNotificationService : IPushNotificationService
|
||||
{
|
||||
public bool IsRegisteredForPush => false;
|
||||
|
||||
public Task<bool> AreNotificationsSettingsEnabledAsync()
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
public Task<string> GetTokenAsync()
|
||||
{
|
||||
return Task.FromResult(null as string);
|
||||
}
|
||||
|
||||
public Task RegisterAsync()
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task UnregisterAsync()
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public void DismissLocalNotification(string notificationId) { }
|
||||
|
||||
public void SendLocalNotification(string title, string message, BaseNotificationData data) { }
|
||||
}
|
||||
}
|
||||
149
src/Core/Services/PreferencesStorageService.cs
Normal file
149
src/Core/Services/PreferencesStorageService.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class PreferencesStorageService : IStorageService, ISynchronousStorageService
|
||||
{
|
||||
public static string KeyFormat = "bwPreferencesStorage:{0}";
|
||||
|
||||
private readonly string _sharedName;
|
||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
|
||||
public PreferencesStorageService(string sharedName)
|
||||
{
|
||||
_sharedName = sharedName;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key) => Task.FromResult(Get<T>(key));
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
Save(key, obj);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
if (!Microsoft.Maui.Storage.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
var objType = typeof(T);
|
||||
if (objType == typeof(string))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return (T)(object)val;
|
||||
}
|
||||
else if (objType == typeof(bool) || objType == typeof(bool?))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(bool), _sharedName);
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(int) || objType == typeof(int?))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(int), _sharedName);
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(long) || objType == typeof(long?))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(long), _sharedName);
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(double) || objType == typeof(double?))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(double), _sharedName);
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(DateTime) || objType == typeof(DateTime?))
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(DateTime), _sharedName);
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
var val = Microsoft.Maui.Storage.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return JsonConvert.DeserializeObject<T>(val, _jsonSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public void Save<T>(string key, T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
Remove(key);
|
||||
return;
|
||||
}
|
||||
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
var objType = typeof(T);
|
||||
if (objType == typeof(string))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, obj as string, _sharedName);
|
||||
}
|
||||
else if (objType == typeof(bool) || objType == typeof(bool?))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, (obj as bool?).Value, _sharedName);
|
||||
}
|
||||
else if (objType == typeof(int) || objType == typeof(int?))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, (obj as int?).Value, _sharedName);
|
||||
}
|
||||
else if (objType == typeof(long) || objType == typeof(long?))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, (obj as long?).Value, _sharedName);
|
||||
}
|
||||
else if (objType == typeof(double) || objType == typeof(double?))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, (obj as double?).Value, _sharedName);
|
||||
}
|
||||
else if (objType == typeof(DateTime) || objType == typeof(DateTime?))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, (obj as DateTime?).Value, _sharedName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings),
|
||||
_sharedName);
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
if (Microsoft.Maui.Storage.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
Microsoft.Maui.Storage.Preferences.Remove(formattedKey, _sharedName);
|
||||
}
|
||||
}
|
||||
|
||||
private static T ChangeType<T>(object value)
|
||||
{
|
||||
var t = typeof(T);
|
||||
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
t = Nullable.GetUnderlyingType(t);
|
||||
}
|
||||
return (T)Convert.ChangeType(value, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
291
src/Core/Services/PushNotificationListenerService.cs
Normal file
291
src/Core/Services/PushNotificationListenerService.cs
Normal file
@@ -0,0 +1,291 @@
|
||||
#if !FDROID
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models;
|
||||
using Bit.App.Pages;
|
||||
using Bit.Core.Resources.Localization;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Response;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Maui.Controls;
|
||||
using Microsoft.Maui;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class PushNotificationListenerService : IPushNotificationListenerService
|
||||
{
|
||||
const string TAG = "##PUSH NOTIFICATIONS";
|
||||
|
||||
private bool _showNotification;
|
||||
private LazyResolve<ISyncService> _syncService = new LazyResolve<ISyncService>();
|
||||
private LazyResolve<IStateService> _stateService = new LazyResolve<IStateService>();
|
||||
private LazyResolve<IAppIdService> _appIdService = new LazyResolve<IAppIdService>();
|
||||
private LazyResolve<IApiService> _apiService = new LazyResolve<IApiService>();
|
||||
private LazyResolve<IMessagingService> _messagingService = new LazyResolve<IMessagingService>();
|
||||
private LazyResolve<IPushNotificationService> _pushNotificationService = new LazyResolve<IPushNotificationService>();
|
||||
private LazyResolve<ILogger> _logger = new LazyResolve<ILogger>();
|
||||
|
||||
public async Task OnMessageAsync(JObject value, string deviceType)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} OnMessageAsync called");
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_showNotification = false;
|
||||
Debug.WriteLine($"{TAG} Message Arrived: {JsonConvert.SerializeObject(value)}");
|
||||
|
||||
NotificationResponse notification = null;
|
||||
if (deviceType == Device.Android)
|
||||
{
|
||||
notification = value.ToObject<NotificationResponse>();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!value.TryGetValue("data", StringComparison.OrdinalIgnoreCase, out JToken dataToken) ||
|
||||
dataToken == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
notification = dataToken.ToObject<NotificationResponse>();
|
||||
}
|
||||
|
||||
Debug.WriteLine($"{TAG} - Notification object created: t:{notification?.Type} - p:{notification?.Payload}");
|
||||
|
||||
var appId = await _appIdService.Value.GetAppIdAsync();
|
||||
if (notification?.Payload == null || notification.ContextId == appId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var myUserId = await _stateService.Value.GetActiveUserIdAsync();
|
||||
var isAuthenticated = await _stateService.Value.IsAuthenticatedAsync();
|
||||
switch (notification.Type)
|
||||
{
|
||||
case NotificationType.SyncCipherUpdate:
|
||||
case NotificationType.SyncCipherCreate:
|
||||
var cipherCreateUpdateMessage = JsonConvert.DeserializeObject<SyncCipherNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && cipherCreateUpdateMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncUpsertCipherAsync(cipherCreateUpdateMessage,
|
||||
notification.Type == NotificationType.SyncCipherUpdate);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncFolderUpdate:
|
||||
case NotificationType.SyncFolderCreate:
|
||||
var folderCreateUpdateMessage = JsonConvert.DeserializeObject<SyncFolderNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && folderCreateUpdateMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncUpsertFolderAsync(folderCreateUpdateMessage,
|
||||
notification.Type == NotificationType.SyncFolderUpdate);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncLoginDelete:
|
||||
case NotificationType.SyncCipherDelete:
|
||||
var loginDeleteMessage = JsonConvert.DeserializeObject<SyncCipherNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && loginDeleteMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncDeleteCipherAsync(loginDeleteMessage);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncFolderDelete:
|
||||
var folderDeleteMessage = JsonConvert.DeserializeObject<SyncFolderNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && folderDeleteMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncDeleteFolderAsync(folderDeleteMessage);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncCiphers:
|
||||
case NotificationType.SyncVault:
|
||||
case NotificationType.SyncSettings:
|
||||
if (isAuthenticated)
|
||||
{
|
||||
await _syncService.Value.FullSyncAsync(false);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncOrgKeys:
|
||||
if (isAuthenticated)
|
||||
{
|
||||
await _apiService.Value.RefreshIdentityTokenAsync();
|
||||
await _syncService.Value.FullSyncAsync(true);
|
||||
}
|
||||
break;
|
||||
case NotificationType.LogOut:
|
||||
if (isAuthenticated)
|
||||
{
|
||||
_messagingService.Value.Send("logout");
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncSendCreate:
|
||||
case NotificationType.SyncSendUpdate:
|
||||
var sendCreateUpdateMessage = JsonConvert.DeserializeObject<SyncSendNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && sendCreateUpdateMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncUpsertSendAsync(sendCreateUpdateMessage,
|
||||
notification.Type == NotificationType.SyncSendUpdate);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncSendDelete:
|
||||
var sendDeleteMessage = JsonConvert.DeserializeObject<SyncSendNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && sendDeleteMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncDeleteSendAsync(sendDeleteMessage);
|
||||
}
|
||||
break;
|
||||
case NotificationType.AuthRequest:
|
||||
var passwordlessLoginMessage = JsonConvert.DeserializeObject<PasswordlessRequestNotification>(notification.Payload);
|
||||
|
||||
// if the user has not enabled passwordless logins ignore requests
|
||||
if (!await _stateService.Value.GetApprovePasswordlessLoginsAsync(passwordlessLoginMessage?.UserId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// if there is a request modal opened ignore all incoming requests
|
||||
// App.Current can be null if the app is killed
|
||||
if (App.Current != null && App.Current.MainPage.Navigation.ModalStack.Any(p => p is NavigationPage navPage && navPage.CurrentPage is LoginPasswordlessPage))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _stateService.Value.SetPasswordlessLoginNotificationAsync(passwordlessLoginMessage);
|
||||
var userEmail = await _stateService.Value.GetEmailAsync(passwordlessLoginMessage?.UserId);
|
||||
|
||||
var notificationData = new PasswordlessNotificationData()
|
||||
{
|
||||
Id = Constants.PasswordlessNotificationId,
|
||||
TimeoutInMinutes = Constants.PasswordlessNotificationTimeoutInMinutes,
|
||||
UserEmail = userEmail,
|
||||
};
|
||||
|
||||
_pushNotificationService.Value.SendLocalNotification(AppResources.LogInRequested, String.Format(AppResources.ConfimLogInAttempForX, userEmail), notificationData);
|
||||
_messagingService.Value.Send(Constants.PasswordlessLoginRequestKey, passwordlessLoginMessage);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnRegisteredAsync(string token, string deviceType)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} - Device Registered - Token : {token}");
|
||||
var isAuthenticated = await _stateService.Value.IsAuthenticatedAsync();
|
||||
if (!isAuthenticated)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} - not auth");
|
||||
return;
|
||||
}
|
||||
|
||||
var appId = await _appIdService.Value.GetAppIdAsync();
|
||||
try
|
||||
{
|
||||
#if DEBUG
|
||||
await _stateService.Value.SetPushInstallationRegistrationErrorAsync(null);
|
||||
#endif
|
||||
|
||||
await _apiService.Value.PutDeviceTokenAsync(appId,
|
||||
new Core.Models.Request.DeviceTokenRequest { PushToken = token });
|
||||
|
||||
Debug.WriteLine($"{TAG} Registered device with server.");
|
||||
|
||||
await _stateService.Value.SetPushLastRegistrationDateAsync(DateTime.UtcNow);
|
||||
if (deviceType == Device.Android)
|
||||
{
|
||||
await _stateService.Value.SetPushCurrentTokenAsync(token);
|
||||
}
|
||||
}
|
||||
#if DEBUG
|
||||
catch (ApiException apiEx)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} Failed to register device.");
|
||||
|
||||
await _stateService.Value.SetPushInstallationRegistrationErrorAsync(apiEx.Error?.Message);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _stateService.Value.SetPushInstallationRegistrationErrorAsync(e.Message);
|
||||
throw;
|
||||
}
|
||||
#else
|
||||
catch (ApiException)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnUnregistered(string deviceType)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} - Device Unnregistered");
|
||||
}
|
||||
|
||||
public void OnError(string message, string deviceType)
|
||||
{
|
||||
Debug.WriteLine($"{TAG} error - {message}");
|
||||
}
|
||||
|
||||
public async Task OnNotificationTapped(BaseNotificationData data)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data is PasswordlessNotificationData passwordlessNotificationData)
|
||||
{
|
||||
var notificationUserId = await _stateService.Value.GetUserIdAsync(passwordlessNotificationData.UserEmail);
|
||||
var activeUserEmail = await _stateService.Value.GetActiveUserEmailAsync();
|
||||
var notificationSaved = await _stateService.Value.GetPasswordlessLoginNotificationAsync();
|
||||
if (activeUserEmail != passwordlessNotificationData.UserEmail && notificationUserId != null && notificationSaved != null)
|
||||
{
|
||||
await _stateService.Value.SetActiveUserAsync(notificationUserId);
|
||||
_messagingService.Value.Send(AccountsManagerMessageCommands.SWITCHED_ACCOUNT);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Value.Exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnNotificationDismissed(BaseNotificationData data)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data is PasswordlessNotificationData passwordlessNotificationData)
|
||||
{
|
||||
var savedNotification = await _stateService.Value.GetPasswordlessLoginNotificationAsync();
|
||||
if (savedNotification != null)
|
||||
{
|
||||
await _stateService.Value.SetPasswordlessLoginNotificationAsync(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Value.Exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ShouldShowNotification()
|
||||
{
|
||||
return _showNotification;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
56
src/Core/Services/SecureStorageService.cs
Normal file
56
src/Core/Services/SecureStorageService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class SecureStorageService : IStorageService
|
||||
{
|
||||
private readonly string _keyFormat = "bwSecureStorage:{0}";
|
||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public async Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
var val = await Microsoft.Maui.Storage.SecureStorage.GetAsync(formattedKey);
|
||||
if (typeof(T) == typeof(string))
|
||||
{
|
||||
return (T)(object)val;
|
||||
}
|
||||
else
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(val, _jsonSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
await RemoveAsync(key);
|
||||
return;
|
||||
}
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
if (typeof(T) == typeof(string))
|
||||
{
|
||||
await Microsoft.Maui.Storage.SecureStorage.SetAsync(formattedKey, obj as string);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Microsoft.Maui.Storage.SecureStorage.SetAsync(formattedKey,
|
||||
JsonConvert.SerializeObject(obj, _jsonSettings));
|
||||
}
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
Microsoft.Maui.Storage.SecureStorage.Remove(formattedKey);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Domain;
|
||||
using Bit.Core.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
using DeviceType = Bit.Core.Enums.DeviceType;
|
||||
using Region = Bit.Core.Enums.Region;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class StateMigrationService : IStateMigrationService
|
||||
{
|
||||
private const int StateVersion = 7;
|
||||
|
||||
private readonly DeviceType _deviceType;
|
||||
private readonly IStorageService _preferencesStorageService;
|
||||
private readonly IStorageService _liteDbStorageService;
|
||||
@@ -62,10 +56,10 @@ namespace Bit.Core.Services
|
||||
if (lastVersion == 0)
|
||||
{
|
||||
// fresh install, set current/latest version for availability going forward
|
||||
lastVersion = StateVersion;
|
||||
lastVersion = Constants.LatestStateVersion;
|
||||
await SetLastStateVersionAsync(lastVersion);
|
||||
}
|
||||
return lastVersion < StateVersion;
|
||||
return lastVersion < Constants.LatestStateVersion;
|
||||
}
|
||||
|
||||
private async Task PerformMigrationAsync()
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Domain;
|
||||
using Bit.Core.Models.Response;
|
||||
using Bit.Core.Models.View;
|
||||
using Bit.Core.Utilities;
|
||||
using BwRegion = Bit.Core.Enums.Region;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@@ -1372,17 +1369,17 @@ namespace Bit.Core.Services
|
||||
await SaveAccountAsync(account, reconciledOptions);
|
||||
}
|
||||
|
||||
public async Task<Region?> GetActiveUserRegionAsync()
|
||||
public async Task<BwRegion?> GetActiveUserRegionAsync()
|
||||
{
|
||||
return await GetActiveUserCustomDataAsync(a => a?.Settings?.Region);
|
||||
}
|
||||
|
||||
public async Task<Region?> GetPreAuthRegionAsync()
|
||||
public async Task<BwRegion?> GetPreAuthRegionAsync()
|
||||
{
|
||||
return await _storageMediatorService.GetAsync<Region?>(Constants.RegionEnvironment);
|
||||
return await _storageMediatorService.GetAsync<BwRegion?>(Constants.RegionEnvironment);
|
||||
}
|
||||
|
||||
public async Task SetPreAuthRegionAsync(Region value)
|
||||
public async Task SetPreAuthRegionAsync(BwRegion value)
|
||||
{
|
||||
await _storageMediatorService.SaveAsync(Constants.RegionEnvironment, value);
|
||||
}
|
||||
|
||||
38
src/Core/Services/UserPinService.cs
Normal file
38
src/Core/Services/UserPinService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class UserPinService : IUserPinService
|
||||
{
|
||||
private readonly IStateService _stateService;
|
||||
private readonly ICryptoService _cryptoService;
|
||||
|
||||
public UserPinService(IStateService stateService, ICryptoService cryptoService)
|
||||
{
|
||||
_stateService = stateService;
|
||||
_cryptoService = cryptoService;
|
||||
}
|
||||
|
||||
public async Task SetupPinAsync(string pin, bool requireMasterPasswordOnRestart)
|
||||
{
|
||||
var kdfConfig = await _stateService.GetActiveUserCustomDataAsync(a => new KdfConfig(a?.Profile));
|
||||
var email = await _stateService.GetEmailAsync();
|
||||
var pinKey = await _cryptoService.MakePinKeyAsync(pin, email, kdfConfig);
|
||||
var userKey = await _cryptoService.GetUserKeyAsync();
|
||||
var protectedPinKey = await _cryptoService.EncryptAsync(userKey.Key, pinKey);
|
||||
|
||||
var encPin = await _cryptoService.EncryptAsync(pin);
|
||||
await _stateService.SetProtectedPinAsync(encPin.EncryptedString);
|
||||
|
||||
if (requireMasterPasswordOnRestart)
|
||||
{
|
||||
await _stateService.SetPinKeyEncryptedUserKeyEphemeralAsync(protectedPinKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _stateService.SetPinKeyEncryptedUserKeyAsync(protectedPinKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user