Skip to content

Commit

Permalink
Improvements and hot key support for Linux/Unix
Browse files Browse the repository at this point in the history
Small improvements, especially regarding display of the KeePassOTP tab in the entry form.
Allow hot key usage on Linux / Unix to auto-type OTP
  • Loading branch information
Rookiestyle committed Sep 23, 2024
1 parent e7cdd93 commit ebc031f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 35 deletions.
58 changes: 35 additions & 23 deletions src/DAO/TFASites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace KeePassOTP
public static class TFASites
{
//const string TFA_JSON_FILE = "https://twofactorauth.org/api/v2/tfa.json";
const string TFA_JSON_FILE_DEFAULT = "https://2fa.directory/api/v3/tfa.json";
const string TFA_JSON_FILE_DEFAULT = "https://api.2fa.directory/v3/tfa.json";

public static string TFA_JSON_FILE
{
Expand Down Expand Up @@ -254,27 +254,34 @@ private static void ReadOTPSites(object s)
Dictionary<string, TFAData> dTFAEntries = new Dictionary<string, TFAData>();
foreach (string tfaentry in lTFAEntries)
{
TFAData tfa = new TFAData();
tfa.domain = GetJSonString(tfaentry, "domain");
string sDomain = tfa.domain.ToLowerInvariant();
if (!sDomain.StartsWith("http://") && !sDomain.StartsWith("https://")) tfa.domain = "https://" + tfa.domain;
try
{
TFAData tfa = new TFAData();
tfa.domain = GetJSonString(tfaentry, "domain");
string sDomain = tfa.domain.ToLowerInvariant();
if (!sDomain.StartsWith("http://") && !sDomain.StartsWith("https://")) tfa.domain = "https://" + tfa.domain;

tfa.img = GetJSonString(tfaentry, "img");
tfa.url = GetJSonString(tfaentry, "url");
if (string.IsNullOrEmpty(tfa.url)) tfa.url = tfa.domain;
tfa.tfa = GetJSonList(tfaentry, "tfa");
tfa.documentation = GetJSonString(tfaentry, "documentation");
tfa.recovery = GetJSonString(tfaentry, "recovery");
tfa.notes = GetJSonString(tfaentry, "notes");
tfa.contact = GetJSonString(tfaentry, "contact");
tfa.regions = GetJSonList(tfaentry, "regions");
tfa.additional_domains = GetJSonList(tfaentry, "additional_domains");
tfa.custom_software = GetJSonList(tfaentry, "custom_software");
tfa.custom_hardware = GetJSonList(tfaentry, "custom_hardware");
tfa.keywords = GetJSonList(tfaentry, "keywords");
string sRegexPattern = CreatePattern(tfa.domain);
tfa.RegexUrl = new Regex(sRegexPattern);
m_dTFA[sRegexPattern] = tfa;
tfa.img = GetJSonString(tfaentry, "img");
tfa.url = GetJSonString(tfaentry, "url");
if (string.IsNullOrEmpty(tfa.url)) tfa.url = tfa.domain;
tfa.tfa = GetJSonList(tfaentry, "tfa");
tfa.documentation = GetJSonString(tfaentry, "documentation");
tfa.recovery = GetJSonString(tfaentry, "recovery");
tfa.notes = GetJSonString(tfaentry, "notes");
tfa.contact = GetJSonString(tfaentry, "contact");
tfa.regions = GetJSonList(tfaentry, "regions");
tfa.additional_domains = GetJSonList(tfaentry, "additional_domains");
tfa.custom_software = GetJSonList(tfaentry, "custom_software");
tfa.custom_hardware = GetJSonList(tfaentry, "custom_hardware");
tfa.keywords = GetJSonList(tfaentry, "keywords");
string sRegexPattern = CreatePattern(tfa.domain);
tfa.RegexUrl = new Regex(sRegexPattern);
m_dTFA[sRegexPattern] = tfa;
}
catch (Exception exAll)
{
PluginDebug.AddError("Error reading OTP sites", 0, exAll.Message, tfaentry);
}
}
DateTime dtEnd = DateTime.Now;
lock (m_TFAReadLock)
Expand All @@ -288,6 +295,7 @@ private static List<string> ParseJSON(string content)
{
bool bRepeat = true;
MatchCollection aMatches = null;

while (bRepeat)
{
try
Expand All @@ -296,10 +304,14 @@ private static List<string> ParseJSON(string content)
aMatches = r.Matches(content);
bRepeat = false;
}
catch (Exception ex) { }
catch (Exception ex) {
PluginDebug.AddError("Error in ParseJSON", 0, ex.Message);
}
}
List<string> lResult = new List<string>();
foreach (Match m in aMatches) lResult.Add(m.Value);

lResult = aMatches.Cast<Match>().Select(m => m.Value).ToList();

return lResult;
}

Expand Down
19 changes: 12 additions & 7 deletions src/KPOTP_Details.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ internal void InitEx(PwEntryForm f)
if (_tc == null) return;

InitTab();
_tc.Selected += OnTabSelected;

DoRefresh();
}

private void OnTabSelected(object sender, TabControlEventArgs e)
{
DoRefresh();
}

Expand All @@ -59,7 +65,6 @@ private void InitTab()
_tpKeePassOTP.Controls.Add(this);
if (_pef != null)
{
_tc.TabPages[0].Leave += CheckActiveTabStop;
_tc.TabPages.Insert(5, _tpKeePassOTP);
}
else
Expand All @@ -78,6 +83,7 @@ private void DoRefresh()
var tfa = TFASites.GetTFAData(_pef.EntryStrings.ReadSafe(PwDefs.UrlField));
DoRefresh(tfa);
}

private void DoRefresh(TFASites.TFAData tfa)
{
if (tfa == null)
Expand All @@ -94,15 +100,14 @@ private void DoRefresh(TFASites.TFAData tfa)

this.Enabled = tfa.tfa_possible;

if (!tfa.tfa_possible) _tc.TabPages.Remove(_tpKeePassOTP);
if (!tfa.tfa_possible)
{
if (_tc.SelectedTab == _tpKeePassOTP) _tc.SelectTab(0);
if (_tc.TabPages.Contains(_tpKeePassOTP)) _tc.TabPages.Remove(_tpKeePassOTP);
}
else if (!_tc.TabPages.Contains(_tpKeePassOTP)) _tc.TabPages.Insert(5, _tpKeePassOTP);
}

private void CheckActiveTabStop(object sender, EventArgs e)
{
DoRefresh();
}

private void SetupUrl(Label l, LinkLabel ll, string url)
{
ll.Text = string.Empty;
Expand Down
15 changes: 14 additions & 1 deletion src/KeePassOTPExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class KeePassOTPExt : Plugin
{
#region members
private IPluginHost m_host = null;

private const string m_sIpcEventName_KPOTP = "kpotp";
//menu stuff
private ToolStripMenuItem m_ContextMenu;
private ToolStripMenuItem m_ContextMenuCopy;
Expand Down Expand Up @@ -98,9 +98,19 @@ public override bool Initialize(IPluginHost host)

GlobalWindowManager.WindowAdded += GlobalWindowManager_WindowAdded;

IpcUtilEx.IpcEvent += OnIpcEvent;

return true;
}

private void OnIpcEvent(object sender, IpcEventArgs ipcEventArgs)
{
if (ipcEventArgs.Name.Equals(m_sIpcEventName_KPOTP, StringComparison.InvariantCultureIgnoreCase))
{
m_host.MainWindow.BeginInvoke(new Action(() => { PTHotKeyManager_HotKeyPressed(null, null); }));
}
}

private void CleanupColumns()
{
//Column KPOTP_Reduced has been removed (use KeePassOTP options instead)
Expand Down Expand Up @@ -553,6 +563,7 @@ private void OptionsFormShown(object sender, Tools.OptionsFormsEventArgs e)
PluginDebug.AddInfo("Options page prepared, " + dDB.Count.ToString() + " open databases found", 0);
options.InitEx(dDB, m_host.Database);
PluginDebug.AddInfo(dDB.Count.ToString() + " open databases added to options page", 0);
e.form.Shown += options.OptionsFormShown;
Tools.AddPluginToOptionsForm(this, options);
}

Expand Down Expand Up @@ -1176,6 +1187,8 @@ public override void Terminate()
RemoveTray();
RemoveMenu();

IpcUtilEx.IpcEvent -= OnIpcEvent;

PluginDebug.SaveOrShow();

m_host = null;
Expand Down
16 changes: 15 additions & 1 deletion src/Options.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,20 @@ private void tbPlaceholder_TextChanged(object sender, EventArgs e)
cbAutoSubmit.Enabled = tbPlaceholder.Text.StartsWith("{") && tbPlaceholder.Text.EndsWith("}");
cbAutoSubmit.Text = string.Format(PluginTranslate.PlaceholderAutoSubmit, tbPlaceholder.Text);
}

internal void OptionsFormShown(object sender, EventArgs e)
{
llHotKeyUnix.Visible = KeePassLib.Native.NativeLib.IsUnix();
llHotKeyUnix.Links.Clear();
Control m_linkHotKeyHelp = Tools.GetControl("m_linkHotKeyHelp", sender as Form);
string sText = m_linkHotKeyHelp != null ? m_linkHotKeyHelp.Text : "Create system-wide hot keys";
llHotKeyUnix.Links.Add(0, sText.Length);
llHotKeyUnix.Text = sText;
}

private void llHotKeyUnix_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Tools.OpenUrl("https://github.com/Rookiestyle/KeePassOTP/wiki/Unix-Linux-%E2%80%90-Create-system%E2%80%90wide-hot-keys");
}
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.2")]
[assembly: AssemblyFileVersion("1.7.2")]
[assembly: AssemblyVersion("1.8")]
[assembly: AssemblyFileVersion("1.8")]
2 changes: 1 addition & 1 deletion version.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:
KeePassOTP:1.7.2
KeePassOTP:1.8
KeePassOTP!de:25
KeePassOTP!fr:7
KeePassOTP!nl:3
Expand Down

0 comments on commit ebc031f

Please sign in to comment.