forked from KeeTrayTOTP/KeeTrayTOTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupTOTP.cs
203 lines (172 loc) · 9.65 KB
/
SetupTOTP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KeePass.Plugins;
using KeePassLib;
using KeePassLib.Security;
namespace KeeTrayTOTP
{
public partial class SetupTOTP : Form
{
/// <summary>
/// Plugin Host.
/// </summary>
private readonly KeeTrayTOTPExt _plugin;
/// <summary>
/// Current entry's reference.
/// </summary>
private readonly PwEntry entry;
/// <summary>
/// Windows Form Constructor.
/// </summary>
/// <param name="Plugin">Plugin Host.</param>
internal SetupTOTP(KeeTrayTOTPExt Plugin, PwEntry Entry)
{
_plugin = Plugin; //Defines variable from argument.
entry = Entry; //Defines variable from argument.
InitializeComponent(); //Form Initialization.
}
private void SetupTOTP_Load(object sender, EventArgs e)
{
Text = TrayTOTP_Plugin_Localization.strSetup + TrayTOTP_Plugin_Localization.strSpaceDashSpace + TrayTOTP_Plugin_Localization.strTrayTOTPPlugin; //Set form's name using constants.
if ( _plugin.SettingsCheck(entry) || _plugin.SeedCheck(entry) ) //Checks the the totp settings exists.
{
string[] Settings = _plugin.SettingsGet(entry); //Gets the the existing totp settings.
bool ValidInterval = false; bool ValidLength = false; bool ValidUrl = false;
_plugin.SettingsValidate(entry, out ValidInterval, out ValidLength, out ValidUrl); //Validates the settings value.
if (ValidInterval) NumericIntervalSetup.Value = Convert.ToDecimal(Settings[0]); //Checks if interval is valid and sets interval numeric to the setting value.
if (ValidLength) //Checks if length is valid.
{
RadioButtonLength6Setup.Checked = Settings[1] == "6"; //Sets length radio 6 to checked if the setting value is 6.
RadioButtonLength8Setup.Checked = Settings[1] == "8"; //Sets length radio 8 to checked if the setting value is 8.
RadioButtonSteamFormatSetup.Checked = Settings[1] == "S"; //Sets format Steam to checked if the setting value is S.
}
if (ValidUrl) ComboBoxTimeCorrectionSetup.Text = Settings[2]; //Checks if url is valid and sets time correction textbox to the setting value.
DeleteSetupButton.Visible = true; //Shows the back button.
HelpProviderSetup.SetHelpString(DeleteSetupButton, SetupTOTP_Localization.SetupDelete);
}
else
{
DeleteSetupButton.Visible = false; //Hides the back button.
}
if (_plugin.SeedCheck(entry)) TextBoxSeedSetup.Text = _plugin.SeedGet(entry).ReadString(); //Checks if the seed exists and sets seed textbox to the seed value.
ComboBoxTimeCorrectionSetup.Items.AddRange(_plugin.TimeCorrections.ToComboBox()); //Gets existings time corrections and adds them in the combobox.
HelpProviderSetup.SetHelpString(FinishSetupButton, SetupTOTP_Localization.SetupFinnish);
ErrorProviderSetup.SetError(TextBoxSeedSetup, string.Empty);
ErrorProviderSetup.SetError(NumericIntervalSetup, string.Empty);
ErrorProviderSetup.SetError(RadioButtonLength8Setup, string.Empty);
ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, string.Empty);
}
private void CheckBoxSeedVisibility_CheckedChanged(object sender, EventArgs e)
{
TextBoxSeedSetup.UseSystemPasswordChar = !CheckBoxSeedVisibility.Checked; //Displays the seed's textbox data to the user in plain text or hides it.
}
/// <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, SetupTOTP_Localization.SetupSeedCantBeEmpty);
else if (!TextBoxSeedSetup.Text.ExtWithoutSpaces().ExtIsBase32(out InvalidBase32Chars)) // TODO: Add support to other known formats
ErrorProviderSetup.SetError(TextBoxSeedSetup, SetupTOTP_Localization.SetupInvalidCharacter + "(" + InvalidBase32Chars + ")!");
else
ErrorProviderSetup.SetError(TextBoxSeedSetup, string.Empty);
// Interval field
if ((NumericIntervalSetup.Value < 1) || (NumericIntervalSetup.Value > 60))
ErrorProviderSetup.SetError(NumericIntervalSetup, string.Format(SetupTOTP_Localization.SetupInterval1to60, Environment.NewLine));
else
ErrorProviderSetup.SetError(NumericIntervalSetup, string.Empty);
// Format/Interval radios
if (!RadioButtonLength6Setup.Checked && !RadioButtonLength8Setup.Checked && !RadioButtonSteamFormatSetup.Checked)
ErrorProviderSetup.SetError(RadioButtonLength8Setup, SetupTOTP_Localization.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, SetupTOTP_Localization.SetupInvalidUrl);
else
ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, string.Empty);
}
/* ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, SetupTOTP_Localization.SetupUrlMustContainHttp);
if (ComboBoxTimeCorrectionSetup.Text.Contains(";"))
ErrorProviderSetup.SetError(ComboBoxTimeCorrectionSetup, SetupTOTP_Localization.SetupInvalidCharacter + " (;)");
*/
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.m_host.MainWindow.ActiveDatabase);
entry.Strings.Set(_plugin.m_host.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName,
TrayTOTP_Plugin_Localization.setdef_string_TOTPSeed_StringName),
new ProtectedString(true, TextBoxSeedSetup.Text)
);
string format = "";
if (RadioButtonLength6Setup.Checked)
format = "6";
else if (RadioButtonLength8Setup.Checked)
format = "8";
else if (RadioButtonSteamFormatSetup.Checked)
format = "S";
entry.Strings.Set(_plugin.m_host.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName,
TrayTOTP_Plugin_Localization.setdef_string_TOTPSettings_StringName),
new ProtectedString(false,
NumericIntervalSetup.Value.ToString() + ";" + format +
(ComboBoxTimeCorrectionSetup.Text != string.Empty ? ";" : string.Empty) + ComboBoxTimeCorrectionSetup.Text)
);
entry.Touch(true);
_plugin.m_host.MainWindow.ActiveDatabase.Modified = true;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
return;
}
DialogResult = DialogResult.OK;
Close();
}
/// <summary>
/// Button that moves back in the setup steps.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelete_Click(object sender, EventArgs e)
{
//if (_plugin.SettingsCheck(entry) || _plugin.SeedCheck(entry))
if (MessageBox.Show(SetupTOTP_Localization.SetupMessageAskDeleteCurrentEntry, TrayTOTP_Plugin_Localization.strTrayTOTPPlugin, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
try
{
entry.CreateBackup(_plugin.m_host.MainWindow.ActiveDatabase);
entry.Strings.Remove(_plugin.m_host.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSeed_StringName, TrayTOTP_Plugin_Localization.setdef_string_TOTPSeed_StringName));
entry.Strings.Remove(_plugin.m_host.CustomConfig.GetString(KeeTrayTOTPExt.setname_string_TOTPSettings_StringName, TrayTOTP_Plugin_Localization.setdef_string_TOTPSettings_StringName));
entry.Touch(true);
_plugin.m_host.MainWindow.ActiveDatabase.Modified = true;
}
catch (Exception)
{
}
DialogResult = DialogResult.OK;
Close();
}
}
}
}