async Task HandleInputActionAsync(string deviceId, IInputAlgorithm inputAlgorithm)
        {
            try
            {
                await inputAlgorithm.InputAsync(new[] { deviceId });
            }
            catch (OtpNotFoundException ex)
            {
                await _metaMessenger.Publish(new ShowWarningNotificationMessage(ex.Message, notificationId : _activeDevice.Device?.Mac));

                WriteLine(ex.Message, LogErrorSeverity.Warning);
            }
            catch (AccountException ex) when(ex is LoginNotFoundException || ex is PasswordNotFoundException)
            {
                if (_appSettingsManager.Settings.UseSimplifiedUI)
                {
                    await _metaMessenger.Publish(new ShowWarningNotificationMessage(ex.Message, notificationId : _activeDevice.Device?.Mac));

                    WriteLine(ex.Message, LogErrorSeverity.Warning);
                }
                else if (_appSettingsManager.Settings.AutoCreateAccountIfNotFound)
                {
                    await OnAddNewAccount(deviceId);
                }
                else
                {
                    var appInfo = AppInfoFactory.GetCurrentAppInfo();
                    WriteLine(ex.Message, LogErrorSeverity.Warning);
                    var createNewAccount = await _windowsManager.ShowAccountNotFoundAsync(ex.Message);

                    if (createNewAccount)
                    {
                        await OnAddNewAccount(deviceId, appInfo);
                    }
                }
            }
            catch (FieldNotSecureException) // Assume that precondition failed because field is not secure
            {
                string message = TranslationSource.Instance["Exception.FieldNotSecure"];
                await _metaMessenger.Publish(new ShowWarningNotificationMessage(message, notificationId : _activeDevice.Device?.Mac));

                WriteLine(message, LogErrorSeverity.Warning);
            }
            catch (AuthEndedUnexpectedlyException)
            {
                var message = TranslationSource.Instance["Exception.AuthEndedUnexpectedly"];
                await _metaMessenger.Publish(new ShowWarningNotificationMessage(message, notificationId : _activeDevice.Device?.Mac));

                WriteLine(message, LogErrorSeverity.Warning);
            }
        }
        async Task OnAddNewAccount(string deviceId, AppInfo appInfo = null)
        {
            if (_appSettingsManager.Settings.UseSimplifiedUI)
            {
                throw new ActionNotSupportedException();
            }

            if (appInfo == null)
            {
                appInfo = AppInfoFactory.GetCurrentAppInfo();
            }

            if (appInfo.ProcessName == "HideezClient")
            {
                throw new HideezWindowSelectedException();
            }

            if (_activeDevice.Device.IsLoadingStorage)
            {
                return;
            }

            if (!_activeDevice.Device.IsAuthorized || !_activeDevice.Device.IsStorageLoaded)
            {
                await _activeDevice.Device.InitRemoteAndLoadStorageAsync();
            }

            if (_activeDevice.Device.IsAuthorized && _activeDevice.Device.IsStorageLoaded)
            {
                await _metaMessenger.Publish(new ShowInfoNotificationMessage(string.Format(TranslationSource.Instance["UserAction.Notification.CreatingNewAccount"], appInfo.Title), notificationId : _activeDevice.Device?.Mac));

                await _metaMessenger.Publish(new ShowActivateMainWindowMessage());

                await _metaMessenger.Publish(new OpenPasswordManagerMessage(deviceId));

                await _metaMessenger.Publish(new AddAccountForAppMessage(deviceId, appInfo));
            }
        }
Exemple #3
0
        /// <summary>
        /// Cache current input field, AppInfo
        /// Get accounts for AppInfo
        /// Filter accounts by divicessId
        /// Filter accounts by previous cached inputs
        /// Call method NotFoundAccounts if not found any
        /// Input data if found one account
        /// Show window to select one account if found more then one
        /// Save Exception in property Exception if was exception during work the method
        /// </summary>
        /// <param name="devicesId">Devices for find account</param>
        public async Task InputAsync(string[] devicesId)
        {
            if (devicesId == null)
            {
                string message = $"ArgumentNull: {nameof(devicesId)}";
                log.WriteLine(message, LogErrorSeverity.Error);
                Debug.Assert(false, message);
                return;
            }

            if (!devicesId.Any())
            {
                string message = "Vaults id cannot be empty.";
                log.WriteLine(message, LogErrorSeverity.Error);
                Debug.Assert(false, message);
                return;
            }

            if (inputCache.HasCache())
            {
                return;
            }

            try
            {
                await Task.Run(() =>
                {
                    currentAppInfo = AppInfoFactory.GetCurrentAppInfo();
                });

                // Our application window was active during action invocation
                if (currentAppInfo.ProcessName == "HideezClient")
                {
                    throw new HideezWindowSelectedException();
                }

                inputCache.CacheInputField();

                if (await BeforeCondition(devicesId))
                {
                    Account[] accounts = GetAccountsByAppInfoAsync(currentAppInfo, devicesId);
                    accounts = FilterAccounts(accounts, devicesId);

                    if (!accounts.Any()) // No accounts for current application
                    {
                        if (inputCache.HasCache())
                        {
                            await inputCache.SetFocusAsync();
                        }

                        OnAccountNotFoundError(currentAppInfo, devicesId);
                    }
                    else if (accounts.Length == 1) // Single account for current application
                    {
                        try
                        {
                            await InputAsync(accounts.First());
                        }
                        catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.ChannelNotAuthorized)
                        {
                            log.WriteLine(ex, LogErrorSeverity.Warning);
                            throw new AuthEndedUnexpectedlyException();
                        }
                    }
                    else // Multiple accounts for current application
                    {
                        Account selectedAccount = null;
                        try
                        {
                            selectedAccount = await windowsManager.SelectAccountAsync(accounts, inputCache.WindowHandle);

                            if (selectedAccount != null)
                            {
                                await InputAsync(selectedAccount);
                            }
                            else
                            {
                                log.WriteLine("Account was not selected.");
                            }
                        }
                        catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.ChannelNotAuthorized)
                        {
                            log.WriteLine(ex, LogErrorSeverity.Warning);
                            if (inputCache.HasCache())
                            {
                                await inputCache.SetFocusAsync();
                            }

                            throw new AuthEndedUnexpectedlyException();
                        }
                        catch (SystemException ex) when(ex is OperationCanceledException || ex is TimeoutException)
                        {
                            if (inputCache.HasCache())
                            {
                                await inputCache.SetFocusAsync();
                            }
                            log.WriteLine(ex, LogErrorSeverity.Information);
                        }
                        catch (Exception ex)
                        {
                            Debug.Assert(false, ex.ToString());
                            log.WriteLine(ex);
                        }
                    }
                }
            }
            finally
            {
                inputCache.ClearInputFieldCache();
                currentAppInfo = null;
            }
        }