Exemplo n.º 1
0
        /// <summary>
        /// Button that moves forward in the setup steps.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonFinish_Click(object sender, EventArgs e)
        {
            string invalidBase32Chars;

            // TOTP Seed field
            if (TextBoxSeedSetup.Text == string.Empty) //If no TOTP Seed
            {
                ErrorProviderSetup.SetError(TextBoxSeedSetup, Localization.Strings.SetupSeedCantBeEmpty);
            }
            else if (Base32.HasInvalidPadding(TextBoxSeedSetup.Text.ExtWithoutSpaces()))
            {
                ErrorProviderSetup.SetError(TextBoxSeedSetup, Localization.Strings.SetupInvalidPadding);
            }
            else if (!TextBoxSeedSetup.Text.ExtWithoutSpaces().IsBase32(out invalidBase32Chars))
            {
                ErrorProviderSetup.SetError(TextBoxSeedSetup, Localization.Strings.SetupInvalidCharacter + "(" + invalidBase32Chars + ")");
            }
            else
            {
                ErrorProviderSetup.SetError(TextBoxSeedSetup, string.Empty);
            }

            // Interval field
            if ((NumericIntervalSetup.Value < 1) || (NumericIntervalSetup.Value > 180))
            {
                ErrorProviderSetup.SetError(NumericIntervalSetup, string.Format(Localization.Strings.SetupInterval, Environment.NewLine));
            }
            else
            {
                ErrorProviderSetup.SetError(NumericIntervalSetup, string.Empty);
            }

            // Format/Interval radios
            if (!RadioButtonLength6Setup.Checked && !RadioButtonLength7Setup.Checked && !RadioButtonLength8Setup.Checked && !RadioButtonSteamFormatSetup.Checked)
            {
                ErrorProviderSetup.SetError(RadioButtonLength8Setup, Localization.Strings.SetupLengthMandatory);
            }
            else
            {
                ErrorProviderSetup.SetError(RadioButtonLength8Setup, string.Empty);
            }

            // Time Correction Field
            if (ComboBoxTimeCorrectionSetup.Text != string.Empty)
            {
                string uriName = ComboBoxTimeCorrectionSetup.Text;

                Uri  uriResult;
                bool validUrl = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
                if (!validUrl)
                {
                    ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, Localization.Strings.SetupInvalidUrl);
                }
                else
                {
                    ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, string.Empty);
                }
            }

            if (ErrorProviderSetup.GetError(TextBoxSeedSetup) != string.Empty ||
                ErrorProviderSetup.GetError(NumericIntervalSetup) != string.Empty ||
                ErrorProviderSetup.GetError(ComboBoxTimeCorrectionSetup) != string.Empty ||
                ErrorProviderSetup.GetError(ComboBoxTimeCorrectionSetup) != string.Empty)
            {
                return;
            }

            try
            {
                _entry.CreateBackup(_plugin.PluginHost.MainWindow.ActiveDatabase);

                _entry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(true, TextBoxSeedSetup.Text));

                string format = "";

                if (RadioButtonLength6Setup.Checked)
                {
                    format = "6";
                }
                else if (RadioButtonLength7Setup.Checked)
                {
                    format = "7";
                }
                else if (RadioButtonLength8Setup.Checked)
                {
                    format = "8";
                }
                else if (RadioButtonSteamFormatSetup.Checked)
                {
                    format = "S";
                }

                var settings = new ProtectedString(false, NumericIntervalSetup.Value.ToString() + ";" + format + (ComboBoxTimeCorrectionSetup.Text != string.Empty ? ";" : string.Empty) + ComboBoxTimeCorrectionSetup.Text);
                _entry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, settings);

                _entry.Touch(true);

                _plugin.PluginHost.MainWindow.ActiveDatabase.Modified = true;
                _plugin.ResetLastSelectedGroup();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }