public void EnablePassphrase()
        {
            int offset = RegionsEnabled ? 1 : 0;

            passphraseTextBox          = new TextBox();
            passphraseTextBox.Location = new Point(84, 70);
            passphraseTextBox.Name     = "passphraseTextBox";
            passphraseTextBox.Size     = new Size(121, 23);
            passphraseTextBox.UseSystemPasswordChar = true;
            passphraseTextBox.TabIndex     = 4;
            passphraseTextBox.Anchor       = (((AnchorStyles.Left | AnchorStyles.Right)));
            passphraseTextBox.TextChanged += delegate(object sender, EventArgs args)
            {
                PasswordStrengthValidator.Strength strength = strengthValidator.getScore(passphraseTextBox.Text);
                strengthIndicator.Minimum = PasswordStrengthValidator.Strength.veryweak.getScore();
                strengthIndicator.Maximum = PasswordStrengthValidator.Strength.verystrong.getScore() + 1;
                strengthIndicator.Value   = strength.getScore() + 1;
            };
            tableLayoutPanel.RowCount++;
            tableLayoutPanel.RowStyles.Insert(2 + offset, new RowStyle(SizeType.AutoSize));
            tableLayoutPanel.Controls.Add(passphraseTextBox, 1, 2 + offset);
            tableLayoutPanel.SetColumnSpan(passphraseTextBox, 3);
            NativeMethods.SendMessage(passphraseTextBox.Handle, NativeConstants.EM_SETCUEBANNER, 0,
                                      LocaleFactory.localizedString("Passphrase", "Cryptomator"));

            strengthIndicator        = new PasswordStrengthIndicator();
            strengthIndicator.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
            tableLayoutPanel.RowCount++;
            tableLayoutPanel.RowStyles.Insert(3 + offset, new RowStyle(SizeType.AutoSize));
            tableLayoutPanel.Controls.Add(strengthIndicator, 1, 3 + offset);
            tableLayoutPanel.SetColumnSpan(strengthIndicator, 3);
            strengthIndicator.Value = 0;

            passphraseConfirmTextBox          = new TextBox();
            passphraseConfirmTextBox.Location = new Point(84, 70);
            passphraseConfirmTextBox.Name     = "passphraseConfirmTextBox";
            passphraseConfirmTextBox.Size     = new Size(121, 23);
            passphraseConfirmTextBox.UseSystemPasswordChar = true;
            passphraseConfirmTextBox.TabIndex = 4;
            passphraseConfirmTextBox.Anchor   = (((AnchorStyles.Left | AnchorStyles.Right)));
            tableLayoutPanel.RowCount++;
            tableLayoutPanel.RowStyles.Insert(4 + offset, new RowStyle(SizeType.AutoSize));
            tableLayoutPanel.Controls.Add(passphraseConfirmTextBox, 1, 4 + offset);
            tableLayoutPanel.SetColumnSpan(passphraseConfirmTextBox, 3);
            tableLayoutPanel.SetRow(okButton, 5 + offset);
            tableLayoutPanel.SetRow(cancelButton, 5 + offset);
            NativeMethods.SendMessage(passphraseConfirmTextBox.Handle, NativeConstants.EM_SETCUEBANNER, 0,
                                      LocaleFactory.localizedString("Confirm Passphrase", "Cryptomator"));
        }
Пример #2
0
        public ChangePasswordDialog() :
            base(UiResources.UI_ChangePassword,
                 new TwoButtonsDialogSettings() { ApplicationBarButtons = true, OverrideOkButtonText = UiResources.Save })
        {
            var contentStackPanel = new StackPanel()
            {
                Orientation         = Orientation.Vertical,
                Width               = Double.NaN,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment   = VerticalAlignment.Top,
                Margin              = new Thickness(12),
                Background          = new SolidColorBrush((Color)Application.Current.Resources["PhoneChromeColor"])
            };

            var description = new TextBlock()
            {
                Margin = new Thickness(12, 0, 12, 20),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Opacity             = 0.8,
                Text         = UiResources.UI_ChangePasswordDescription,
                TextWrapping = TextWrapping.Wrap
            };

            contentStackPanel.Children.Add(description);

            var passwordStrengthIndicator = new PasswordStrengthIndicator()
            {
                Height = 4,
                Margin = new Thickness(12, 0, 12, 0),
                IndicatorBackground = (Brush)Application.Current.Resources["PhoneInactiveBrush"],
                IndicatorForeground = (Brush)Application.Current.Resources["MegaRedColorBrush"]
            };

            passwordStrengthIndicator.IndicatorsOpacity.Add(0.4);
            passwordStrengthIndicator.IndicatorsOpacity.Add(0.6);
            passwordStrengthIndicator.IndicatorsOpacity.Add(0.8);
            passwordStrengthIndicator.IndicatorsOpacity.Add(1.0);

            var warningMessage = new TextBlock()
            {
                Margin       = new Thickness(12, 0, 12, 0),
                Foreground   = (Brush)Application.Current.Resources["MegaRedColorBrush"],
                Text         = string.Empty,
                TextWrapping = TextWrapping.Wrap
            };

            var newPassword = new RadPasswordBox()
            {
                Watermark             = UiResources.UI_NewPassword.ToLower(),
                ClearButtonVisibility = Visibility.Visible
            };

            newPassword.PasswordChanged += (sender, args) =>
            {
                warningMessage.Text             = string.Empty;
                passwordStrengthIndicator.Value =
                    ValidationService.CalculatePasswordStrength(newPassword.Password);
            };
            contentStackPanel.Children.Add(newPassword);

            contentStackPanel.Children.Add(passwordStrengthIndicator);

            var confirmPassword = new RadPasswordBox()
            {
                Watermark             = UiResources.UI_ConfirmPassword.ToLower(),
                ClearButtonVisibility = Visibility.Visible
            };

            confirmPassword.PasswordChanged += (sender, args) =>
            {
                warningMessage.Text = string.Empty;
            };
            contentStackPanel.Children.Add(confirmPassword);

            contentStackPanel.Children.Add(warningMessage);

            this.MainGrid.Children.Add(contentStackPanel);
            Grid.SetRow(contentStackPanel, 2);

            this.OkButtonTapped += async(sender, args) =>
            {
                warningMessage.Text = string.Empty;

                if (string.IsNullOrWhiteSpace(newPassword.Password) || string.IsNullOrWhiteSpace(confirmPassword.Password))
                {
                    if (string.IsNullOrWhiteSpace(newPassword.Password))
                    {
                        newPassword.Focus();
                    }
                    else if (string.IsNullOrWhiteSpace(confirmPassword.Password))
                    {
                        confirmPassword.Focus();
                    }

                    warningMessage.Text = AppMessages.AM_EmptyRequiredFields;
                    return;
                }

                // If the new password and the confirmation password don't match
                if (!newPassword.Password.Equals(confirmPassword.Password))
                {
                    confirmPassword.Focus();
                    warningMessage.Text = AppMessages.PasswordsDoNotMatch;
                    return;
                }

                // If the password strength is very weak
                if (passwordStrengthIndicator.Value == MPasswordStrength.PASSWORD_STRENGTH_VERYWEAK)
                {
                    newPassword.Focus();
                    warningMessage.Text = AppMessages.AM_VeryWeakPassword;
                    return;
                }

                ChangePasswordResult result = ChangePasswordResult.Unknown;
                var changePassword          = new ChangePasswordRequestListenerAsync();

                var mfaStatus = await AccountService.CheckMultiFactorAuthStatusAsync();

                if (mfaStatus == MultiFactorAuthStatus.Enabled)
                {
                    this.CloseDialog();
                    await DialogService.ShowAsyncMultiFactorAuthCodeInputDialogAsync(async (string code) =>
                    {
                        result = await changePassword.ExecuteAsync(() =>
                        {
                            SdkService.MegaSdk.multiFactorAuthChangePasswordWithoutOld(
                                newPassword.Password, code, changePassword);
                        });

                        if (result == ChangePasswordResult.MultiFactorAuthInvalidCode)
                        {
                            DialogService.SetMultiFactorAuthCodeInputDialogWarningMessage();
                            return(false);
                        }

                        return(true);
                    });
                }
                else
                {
                    result = await changePassword.ExecuteAsync(() =>
                                                               SdkService.MegaSdk.changePasswordWithoutOld(newPassword.Password, changePassword));
                }

                if (result != ChangePasswordResult.Success)
                {
                    warningMessage.Text = AppMessages.AM_PasswordChangeFailed;
                    Deployment.Current.Dispatcher.BeginInvoke(() => this.ShowDialog());
                    return;
                }

                this.CloseDialog();

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    new CustomMessageDialog(
                        AppMessages.AM_PasswordChanged_Title,
                        AppMessages.AM_PasswordChanged,
                        App.AppInformation,
                        MessageDialogButtons.Ok).ShowDialog();
                });
            };

            this.CancelButtonTapped += (sender, args) =>
            {
                this.CloseDialog();
            };
        }