-
Notifications
You must be signed in to change notification settings - Fork 0
/
CredentialAutomation.cs
executable file
·59 lines (51 loc) · 1.87 KB
/
CredentialAutomation.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
using System;
using System.Text;
using KeePass.Plugins;
namespace CredentialAutomation
{
public sealed class CredentialAutomationExt : Plugin
{
KeePass.Forms.MainForm m_formMain = null;
KeePass.Util.CommandLineArgs m_argsCommand = null;
public override bool Initialize(IPluginHost host)
{
m_formMain = host.MainWindow;
m_argsCommand = host.CommandLineArgs;
// I couldn't find any better event to capture
host.MainWindow.UserActivityPost += DetectLoadingHasCompleted;
return true;
}
void DetectLoadingHasCompleted(object sender, EventArgs e)
{
KeePassLib.Interfaces.IStatusLogger loggerSW = null;
KeePassLib.Keys.CompositeKey key = null;
foreach (var doc in m_formMain.DocumentManager.Documents)
{
if (doc.Database.IsOpen || string.IsNullOrEmpty(doc.LockedIoc.Path))
// If the DB is Open, no need to re-open it
// If the path is blank, then we've captured an event too early in the process
continue;
if (loggerSW == null)
{ // Initialize the data a single time
key = KeePass.Util.KeyUtil.KeyFromCommandLine(m_argsCommand);
m_formMain.UserActivityPost -= DetectLoadingHasCompleted; // Unsubscribe from any more events
if (key == null)
return; // Do this after removing the event handler, since we don't have any Key to pass
loggerSW = m_formMain.CreateShowWarningsLogger();
loggerSW.StartLogging(KeePass.Resources.KPRes.OpenDatabase, true);
}
doc.Database.Open(doc.LockedIoc, key, loggerSW); // Attempt to open with command-line credentials
if (doc.Database.IsOpen)
doc.LockedIoc = new KeePassLib.Serialization.IOConnectionInfo(); // Clear lock
}
if (loggerSW != null)
{
loggerSW.EndLogging();
}
}
public override void Terminate()
{
m_formMain.UserActivityPost -= DetectLoadingHasCompleted;
}
}
}