KeeOtp / KeeOtp / OtpInformation.cs

Devin Martin 48a6f35 




























































































































using System;
using System.Windows.Forms;
using OtpSharp;

namespace KeeOtp
{
    public partial class OtpInformation : Form
    {
        const string validKeyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
        public OtpAuthData Data { get; set; }

        public OtpInformation()
        {
            InitializeComponent();
            this.Shown += (sender, e) => FormWasShown();
        }

        private void FormWasShown()
        {
            if (this.Data != null)
            {
                this.Data.Key.UsePlainKey(key =>this.textBoxKey.Text = Base32.Encode(key));
                this.textBoxStep.Text = this.Data.Step.ToString();

                if (this.Data.Size == 8)
                {
                    this.radioButtonSix.Checked = false;
                    this.radioButtonEight.Checked = true;
                }
                else
                {
                    this.radioButtonSix.Checked = true;
                    this.radioButtonEight.Checked = false;
                }
            }
            else
                this.textBoxStep.Text = "30";
        }

        private void OtpInformation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                return;
            try
            {
                if (string.IsNullOrEmpty(this.textBoxKey.Text))
                {
                    MessageBox.Show("A key must be set");
                    e.Cancel = true;
                    return;
                }

                if (this.textBoxKey.Text.Length < 8)
                {
                    MessageBox.Show("Key must be at least 8 characters.  If you are provided with less data then pad it up to 8 characters with '='s");
                    e.Cancel = true;
                    return;
                }

                var size = (this.radioButtonEight.Checked) ? 8 : 6;
                int step;
                
                if (int.TryParse(this.textBoxStep.Text, out step))
                {
                    if (step != 30)
                    {
                        if (step <= 0)
                        {
                            this.textBoxStep.Text = "30";
                            MessageBox.Show("The time step must be a non-zero positive integer.  The standard value is 30.  If you weren't specifically given an alternate value just use 30.");
                            e.Cancel = true;
                            return;
                        }
                        else if (MessageBox.Show("You have selected a non-standard time step.  30 is the standard recommended value.  You should only proceed if you were specifically told to use this time step size.  Do you wish to proceed?", "Non-standard time step size", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                }
                else
                {
                    this.textBoxStep.Text = "30";
                    MessageBox.Show("The time step must be a non-zero positive integer.  The standard value is 30.  If you weren't specifically given an alternate value just use 30.");
                    e.Cancel = true;
                    return;
                }

                var key = ProtectedKey.CreateProtectedKeyAndDestroyPlaintextKey(Base32.Decode(this.textBoxKey.Text));
                this.Data = new OtpAuthData()
                {
                    Key = key,
                    Size = size,
                    Step = step
                };
            }
            catch
            {
                e.Cancel = true;
            }
        }

        private void textBoxKey_TextChanged(object sender, EventArgs e)
        {
            var unpaddedBase32 = this.textBoxKey.Text.ToUpperInvariant().TrimEnd('=');

            bool invalid = false;
            foreach (var c in unpaddedBase32)
            {
                if (validKeyChars.IndexOf(c) < 0)
                {
                    invalid = true;
                    break;
                }
            }

            if (invalid)
            {
                if (this.textBoxKey.Text.Length > 1)
                    this.textBoxKey.Text = this.textBoxKey.Text.Substring(0, this.textBoxKey.Text.Length - 1);
                else
                    this.textBoxKey.Text = string.Empty;
            }
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.