-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeePassFlatexPluginExt.cs
241 lines (226 loc) · 5.77 KB
/
KeePassFlatexPluginExt.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using KeePass.Plugins;
using KeePass.Util;
using KeePassFlatexPlugin.Properties;
using KeePassLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace KeePassFlatexPlugin
{
public sealed class KeePassFlatexPluginExt : Plugin
{
private const String TITLE = "KeePassFlatexPlugin";
private const String ITANTITLE = "<ITAN>";
private const String ROWS = "ABCDEFGHKLM";
//9x11
private const int FLATEX_COLUMNS = 9;
private const int FLATEX_ROWS = 11;
public static Regex rToken = new Regex(@"([A-HK-M])([1-9])");
private IPluginHost phHost = null;
private ToolStripMenuItem tsmStart;
public IPluginHost PluginHost
{
get
{
return phHost;
}
}
public override System.Drawing.Image SmallIcon
{
get
{
return Resources.Flatex;
}
}
public override bool Initialize(IPluginHost host)
{
phHost = host;
//add a menu item
MenuStrip msMain = phHost.MainWindow.MainMenu;
tsmStart = new ToolStripMenuItem("Flatex");
tsmStart.Click += tsmStart_Click;
tsmStart.Visible = false;
msMain.Items.Add(tsmStart);
//wait for file opening
phHost.MainWindow.FileClosed += MainWindow_FileClosed;
phHost.MainWindow.FileOpened += MainWindow_FileOpened;
phHost.MainWindow.DocumentManager.ActiveDocumentSelected += DocumentManager_ActiveDocumentSelected;
return true;
}
/// <summary>
/// Collects all tokens in the given group.
/// </summary>
/// <param name="group">group</param>
/// <param name="d">map of tokens</param>
private static void CollectTokens(PwGroup group, Dictionary<String, String> d)
{
if (group != null)
{
//check entries
foreach (PwEntry e in group.Entries)
{
String title = e.Strings.Get(PwDefs.TitleField).ReadString();
if (ITANTITLE.Equals(title))
{
//get token index
String username = e.Strings.Get(PwDefs.UserNameField).ReadString();
if (!String.IsNullOrEmpty(username))
{
//try to parse it
int i;
if (int.TryParse(username, out i))
{
int row = (i - 1) / FLATEX_COLUMNS;
int col = i - 1 - (row * FLATEX_COLUMNS);
if ((row >= 0) && (row < FLATEX_ROWS) && (col >= 0) && (col < FLATEX_COLUMNS))
{
String token = ROWS[row] + (col + 1).ToString();
String value = e.Strings.Get(PwDefs.PasswordField).ReadString();
d[token] = value;
}
}
}
}
}
//walk down
foreach (PwGroup g in group.Groups)
{
CollectTokens(g, d);
}
}
}
/// <summary>
/// Collects all tokens in the current database.
/// </summary>
/// <returns>map of tokens</returns>
private Dictionary<String, String> CollectTokens()
{
Dictionary<String, String> d = new Dictionary<String, String>();
PwDatabase db = phHost.MainWindow.ActiveDatabase;
if (db != null)
{
CollectTokens(db.RootGroup, d);
}
return d;
}
/// <summary>
/// Checks if the active database has iTanEntries.
/// </summary>
/// <param name="group">group (can be null)</param>
/// <returns>true on success, else false</returns>
private static bool DatabaseHasITanEntries(PwGroup group)
{
if (group != null)
{
//check entries
foreach (PwEntry e in group.Entries)
{
String title = e.Strings.Get(PwDefs.TitleField).ReadString();
if (ITANTITLE.Equals(title))
{
return true;
}
}
//walk down
foreach (PwGroup g in group.Groups)
{
if (DatabaseHasITanEntries(g))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Checks if the active database has iTanEntries.
/// </summary>
/// <returns>true on success, else false</returns>
public bool DatabaseHasITanEntries()
{
PwDatabase db = phHost.MainWindow.ActiveDatabase;
if (db != null)
{
return DatabaseHasITanEntries(db.RootGroup);
}
return false;
}
public void RefreshState()
{
//search for iTan entries
tsmStart.Visible = DatabaseHasITanEntries();
}
private void DocumentManager_ActiveDocumentSelected(object sender, EventArgs e)
{
try
{
RefreshState();
}
catch (Exception ex)
{
ShowError(null, ex);
}
}
private void MainWindow_FileClosed(object sender, KeePass.Forms.FileClosedEventArgs e)
{
try
{
RefreshState();
}
catch (Exception ex)
{
ShowError(null, ex);
}
}
private void MainWindow_FileOpened(object sender, KeePass.Forms.FileOpenedEventArgs e)
{
try
{
RefreshState();
}
catch (Exception ex)
{
ShowError(null, ex);
}
}
private void tsmStart_Click(object sender, EventArgs e)
{
try
{
Dictionary<String, String> tokens = CollectTokens();
GenerateTokenForm frm = new GenerateTokenForm(tokens);
if (frm.ShowDialog() == DialogResult.OK)
{
//get token and construct temporary entry
String token = frm.Token;
//PwEntry entry = new PwEntry(false, false);
////create an empty title field to prevent bug in iTanMaster plugin
//entry.Strings.Set(PwDefs.TitleField, new KeePassLib.Security.ProtectedString(false, ""));
//entry.Strings.Set(PwDefs.PasswordField, new KeePassLib.Security.ProtectedString(true, token));
//AutoType.PerformIntoPreviousWindow(phHost.MainWindow, entry, phHost.Database, "{PASSWORD}{ENTER}");
//just copy the entry to the clipboard
Clipboard.SetText(token);
}
}
catch (Exception ex)
{
ShowError(null, ex);
}
}
public static void ShowError(String msg, Exception ex)
{
if (msg != null)
{
msg = msg + "\n" + ex.Message + "@" + ex.StackTrace;
}
else if (ex != null)
{
msg = ex.Message + "@" + ex.StackTrace;
}
MessageBox.Show(msg, TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}