Devin Martin avatar Devin Martin committed f180ad7

Initial version of Add/Edit

Comments (0)

Files changed (6)

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="OtpInformation.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="OtpInformation.Designer.cs">
+      <DependentUpon>OtpInformation.cs</DependentUpon>
+    </Compile>
     <Compile Include="OtpType.cs" />
     <Compile Include="KeeOtpExt.cs" />
     <Compile Include="OtpAuthData.cs" />
     </BootstrapperPackage>
   </ItemGroup>
   <ItemGroup>
+    <EmbeddedResource Include="OtpInformation.resx">
+      <DependentUpon>OtpInformation.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="ShowOneTimePasswords.resx">
       <DependentUpon>ShowOneTimePasswords.cs</DependentUpon>
     </EmbeddedResource>
             // grab the entry that we care about
             var entry = entries[0];
 
-            ShowOneTimePasswords form = new ShowOneTimePasswords(entry);
+            ShowOneTimePasswords form = new ShowOneTimePasswords(entry, host);
             form.ShowDialog();
         }
     }
     /// <summary>
     /// Class that serializes and deserializes data into the Strings for the entry
     /// </summary>
-    class OtpAuthData
+    public class OtpAuthData
     {
         public const string StringDictionaryKey = "otp";
 
 
 namespace KeeOtp
 {
-    enum OtpType
+    public enum OtpType
     {
         Totp,
         Hotp

ShowOneTimePasswords.Designer.cs

             this.labelRemaining = new System.Windows.Forms.Label();
             this.labelRemainingLabel = new System.Windows.Forms.Label();
             this.labelInstructions = new System.Windows.Forms.Label();
+            this.buttonClose = new System.Windows.Forms.Button();
+            this.buttonEdit = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // labelOtp
     " check that the clock on your computer is set precisely.  Be sure to double chec" +
     "k the time zone as well.";
             // 
+            // buttonClose
+            // 
+            this.buttonClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
+            this.buttonClose.DialogResult = System.Windows.Forms.DialogResult.OK;
+            this.buttonClose.Location = new System.Drawing.Point(197, 231);
+            this.buttonClose.Name = "buttonClose";
+            this.buttonClose.Size = new System.Drawing.Size(75, 23);
+            this.buttonClose.TabIndex = 4;
+            this.buttonClose.Text = "Close";
+            this.buttonClose.UseVisualStyleBackColor = true;
+            // 
+            // buttonEdit
+            // 
+            this.buttonEdit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
+            this.buttonEdit.Location = new System.Drawing.Point(116, 231);
+            this.buttonEdit.Name = "buttonEdit";
+            this.buttonEdit.Size = new System.Drawing.Size(75, 23);
+            this.buttonEdit.TabIndex = 5;
+            this.buttonEdit.Text = "Edit";
+            this.buttonEdit.UseVisualStyleBackColor = true;
+            this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
+            // 
             // ShowOneTimePasswords
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(284, 192);
+            this.ClientSize = new System.Drawing.Size(284, 266);
+            this.Controls.Add(this.buttonEdit);
+            this.Controls.Add(this.buttonClose);
             this.Controls.Add(this.labelInstructions);
             this.Controls.Add(this.labelRemainingLabel);
             this.Controls.Add(this.labelRemaining);
         private System.Windows.Forms.Label labelRemaining;
         private System.Windows.Forms.Label labelRemainingLabel;
         private System.Windows.Forms.Label labelInstructions;
+        private System.Windows.Forms.Button buttonClose;
+        private System.Windows.Forms.Button buttonEdit;
     }
 }

ShowOneTimePasswords.cs

 using System;
 using System.Windows.Forms;
+using KeePass.Plugins;
+using KeePassLib.Security;
 using OtpSharp;
 
 namespace KeeOtp
 {
     public partial class ShowOneTimePasswords : Form
     {
-        private KeePassLib.PwEntry entry;
+        private readonly KeePassLib.PwEntry entry;
+        private readonly IPluginHost host ;
         private Totp totp;
         private int lastCode;
         private int lastRemainingTime;
 
-        public ShowOneTimePasswords(KeePassLib.PwEntry entry)
+        OtpAuthData data;
+
+        public ShowOneTimePasswords(KeePassLib.PwEntry entry, IPluginHost host)
         {
+            this.host = host;
             this.entry = entry;
             InitializeComponent();
             this.Shown += (sender, e) => FormWasShown();
                 if (code != lastCode)
                 {
                     lastCode = code;
-                    this.labelOtp.Text = code.ToString().PadLeft(6, '0');
+                    this.labelOtp.Text = code.ToString().PadLeft(this.data.Size, '0');
                 }
                 if (remaining != lastRemainingTime)
                 {
         {
             if (!entry.Strings.Exists(OtpAuthData.StringDictionaryKey))
             {
-                this.Close();
-                MessageBox.Show("Please add a one time password field");
+                this.AddEdit();
             }
             else
             {
                 try
                 {
-                    var otpAuthData = OtpAuthData.FromString(entry.Strings.Get(OtpAuthData.StringDictionaryKey).ReadString());
+                    this.data = OtpAuthData.FromString(entry.Strings.Get(OtpAuthData.StringDictionaryKey).ReadString());
 
-                    this.totp = new Totp(otpAuthData.Key, step:otpAuthData.Step);
-                    this.timerUpdateTotp.Enabled = true;
+                    ShowCode();
                 }
-                catch (Exception e)
+                catch
                 {
-                    MessageBox.Show(e.Message);
-                    this.Close();
+                    this.AddEdit();
                 }
             }
+        }
 
-            //entry.Strings.Set("TOTP", new ProtectedString(true, "test"));
+        private void buttonEdit_Click(object sender, EventArgs e)
+        {
+            this.AddEdit();
+        }
 
-            //host.MainWindow.UpdateUI(false, null, true, host.Database.RootGroup,
-            //true, null, true);
+        private void ShowCode()
+        {
+            this.totp = new Totp(this.data.Key, step: this.data.Step, totpSize: this.data.Size);
+            this.timerUpdateTotp.Enabled = true;
+        }
+
+        private void AddEdit()
+        {
+            this.timerUpdateTotp.Enabled = false;
+            this.labelRemaining.Text = "x";
+            this.labelOtp.Text = "xxxxxx";
+            this.totp = null;
+
+            var addEditForm = new OtpInformation();
+            addEditForm.Data = this.data;
+
+            var result = addEditForm.ShowDialog();
+            if (result == System.Windows.Forms.DialogResult.OK)
+            {
+                this.data = addEditForm.Data;
+                // set the data
+                entry.Strings.Set(OtpAuthData.StringDictionaryKey, new ProtectedString(true, this.data.EncodedString));
+
+                // indicate that a change was made, must save
+                host.MainWindow.UpdateUI(false, null, true, host.Database.RootGroup, true, null, true);
+
+                this.Show();
+            }
+            else
+                this.Close();
         }
     }
 }
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.