Skip to content

Commit

Permalink
Fixes #16 and #29
Browse files Browse the repository at this point in the history
  • Loading branch information
tiuub committed Sep 24, 2021
1 parent 4457069 commit 65ed0c2
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
6 changes: 1 addition & 5 deletions KeeOtp2/Forms/OtpInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ private void SetCustomSettingsState()
private void scanQRCode()
{
Uri uri = null;
IBarcodeReader reader = new BarcodeReader();
Bitmap bmpScreenshot;
Graphics gfxScreenshot;

Expand All @@ -560,10 +559,7 @@ private void scanQRCode()
bmpScreenshot = new Bitmap(sc.Bounds.Width, sc.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(sc.Bounds.X, sc.Bounds.Y, 0, 0, sc.Bounds.Size, CopyPixelOperation.SourceCopy);
var result = reader.Decode(bmpScreenshot);
if (result != null)
if (result.ToString().StartsWith("otpauth"))
uri = new Uri(result.ToString());
uri = OtpAuthUtils.bitmapToUri(bmpScreenshot);
}

p = this;
Expand Down
17 changes: 16 additions & 1 deletion KeeOtp2/Forms/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using KeePass.Plugins;
using KeePassLib;
using KeePassLib.Collections;
using KeePassLib.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void InitEx()
labelUseHotKey.Text = KeeOtp2Statics.SettingsTOTPGlobalAutoType + KeeOtp2Statics.SelectorChar;
checkBoxUseHotkey.Text = KeeOtp2Statics.SettingsUseGlobalHotkey;
labelHotKeySequence.Text = KeeOtp2Statics.SettingsHotKeySequence + KeeOtp2Statics.InformationChar + KeeOtp2Statics.SelectorChar;
labelGlobalHotkey.Text = KeeOtp2Statics.SettingsGlobalHotKey;
labelGlobalHotkey.Text = KeeOtp2Statics.SettingsGlobalHotKey + KeeOtp2Statics.SelectorChar;
groupBoxTime.Text = KeeOtp2Statics.GlobalTime + KeeOtp2Statics.InformationChar + KeeOtp2Statics.SelectorChar;
radioButtonSystemTime.Text = KeeOtp2Statics.SettingsUseSystemTime;
labelTime.Text = String.Format(KeeOtp2Statics.SettingsPreviewUtc, "00:00:00");
Expand Down Expand Up @@ -123,6 +124,20 @@ public void InitEx()

loadConfig();

if (NativeLib.IsUnix())
{
checkBoxUseHotkey.Enabled = false;
textBoxHotKeySequence.Enabled = false;
hotKeyControlExGlobalHotkey.Enabled = false;
string toolTipHotKeyNotAvailable = KeeOtp2Statics.ToolTipHotKeyNotAvailable;
toolTip.SetToolTip(labelUseHotKey, toolTipHotKeyNotAvailable);
toolTip.SetToolTip(checkBoxUseHotkey, toolTipHotKeyNotAvailable);
toolTip.SetToolTip(labelHotKeySequence, toolTipHotKeyNotAvailable);
toolTip.SetToolTip(textBoxHotKeySequence, toolTipHotKeyNotAvailable);
toolTip.SetToolTip(labelGlobalHotkey, toolTipHotKeyNotAvailable);
toolTip.SetToolTip(hotKeyControlExGlobalHotkey, toolTipHotKeyNotAvailable);
}

timerClock.Start();
}

Expand Down
12 changes: 6 additions & 6 deletions KeeOtp2/KeeOtp2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,37 @@
<Compile Include="Forms\About.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\About.designer.cs">
<Compile Include="Forms\About.Designer.cs">
<DependentUpon>About.cs</DependentUpon>
</Compile>
<Compile Include="Forms\OtpInformation.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\OtpInformation.designer.cs">
<Compile Include="Forms\OtpInformation.Designer.cs">
<DependentUpon>OtpInformation.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Settings.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\Settings.designer.cs">
<Compile Include="Forms\Settings.Designer.cs">
<DependentUpon>Settings.cs</DependentUpon>
</Compile>
<Compile Include="Forms\ShowOneTimePasswords.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\ShowOneTimePasswords.designer.cs">
<Compile Include="Forms\ShowOneTimePasswords.Designer.cs">
<DependentUpon>ShowOneTimePasswords.cs</DependentUpon>
</Compile>
<Compile Include="Forms\ShowQrCode.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\ShowQrCode.designer.cs">
<Compile Include="Forms\ShowQrCode.Designer.cs">
<DependentUpon>ShowQrCode.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Troubleshooting.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\Troubleshooting.designer.cs">
<Compile Include="Forms\Troubleshooting.Designer.cs">
<DependentUpon>Troubleshooting.cs</DependentUpon>
</Compile>
<Compile Include="KeeOtp2Config.cs" />
Expand Down
11 changes: 8 additions & 3 deletions KeeOtp2/KeeOtp2Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Windows.Forms;
using NHotkey.WindowsForms;
using KeePassLib.Native;

namespace KeeOtp2
{
Expand All @@ -13,14 +14,16 @@ internal static class KeeOtp2Config

public static void loadConfig()
{
registerHotKey();
if (OtpTime.getTimeType() == OtpTimeType.CustomNtpServer)
OtpTime.pollCustomNtpServer();

if (!NativeLib.IsUnix())
registerHotKey();
}

public static void registerHotKey()
{
if (handler != null && !KeePassLib.Native.NativeLib.IsUnix())
if (handler != null)
HotkeyManager.Current.AddOrReplace(HOTKEY_NAME, KeeOtp2Config.HotKeyKeys, handler);
}

Expand All @@ -46,7 +49,9 @@ internal static bool UseHotKey
{
get
{
return Program.Config.CustomConfig.GetBool(PATH_USE_HOTKEY, true);
if (!NativeLib.IsUnix())
return Program.Config.CustomConfig.GetBool(PATH_USE_HOTKEY, true);
return false;
}
set
{
Expand Down
4 changes: 3 additions & 1 deletion KeeOtp2/KeeOtp2Ext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.InteropServices;
using NHotkey;
using KeePassLib.Security;
using KeePassLib.Native;

namespace KeeOtp2
{
Expand Down Expand Up @@ -91,7 +92,8 @@ public override bool Initialize(IPluginHost host)

public override void Terminate()
{
KeeOtp2Config.unregisterHotKey();
if (!NativeLib.IsUnix())
KeeOtp2Config.unregisterHotKey();

// Remove all of our menu items
ToolStripItemCollection menu = host.MainWindow.EntryContextMenu.Items;
Expand Down
1 change: 1 addition & 0 deletions KeeOtp2/KeeOtp2Statics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class KeeOtp2Statics
public static readonly string ToolTipMigrate = "Since KeePass 2.47, TOTPs can generated by a built-in function.\nYou can use this button to easily migrate to the built-in function.\n\n(It is also recommended!)";
public static readonly string ToolTipHotKeySequence = "Click inside the box to change the sequence!";
public static readonly string ToolTipHotKeyCombination = "Click inside the box to change tho combination!";
public static readonly string ToolTipHotKeyNotAvailable = "HotKeys are currently not supported on your system! They are only supported on Windows right now!";
public static readonly string ToolTipOverrideBuiltInTime = "If set true, this plugin will generate the TOTP even if\n{TIMEOTP} placeholder is set. This will be only done\nwhen using a time offset or a custom NTP server!";
public static readonly string ToolTipOtpInformationUseCustomSettings = "You should only modify these settings, if you know what you are doing.\nIn the most cases, these settings are not needed to change.";
public static readonly string ToolTipOtpInformationUseOldKeeOtpSaveMode = "This setting is here to guarantee the compatability to older versions of KeeOtp.\nIt indicates whether you are using the old KeeOtp save mode or the new one.";
Expand Down
34 changes: 30 additions & 4 deletions KeeOtp2/OtpAuthUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Text.RegularExpressions;
using OtpNet;
using KeePass.Plugins;
using System.Drawing;
using ZXing;

namespace KeeOtp2
{
Expand Down Expand Up @@ -507,14 +509,22 @@ public static bool checkUriString(String uriString)
{
try
{
if (uriToOtpAuthData(new Uri(uriString)) != null)
if (checkUri(new Uri(uriString)))
return true;
return false;
}
catch
catch { }
return false;
}

public static bool checkUri(Uri uri)
{
try
{
return false;
if (uriToOtpAuthData(uri) != null)
return true;
}
catch { }
return false;
}

public static OtpAuthData uriToOtpAuthData(Uri uri)
Expand Down Expand Up @@ -562,6 +572,22 @@ public static OtpAuthData uriToOtpAuthData(Uri uri)
throw new InvalidUriFormat("Given Uri does not start with 'otpauth://'!");
}

public static Uri bitmapToUri(Bitmap bitmap)
{
IBarcodeReader reader = new BarcodeReader();
var result = reader.Decode(bitmap);
if (result != null)
{
if (result.ToString().StartsWith("otpauth"))
{
Uri uri = new Uri(result.ToString());
if (checkUri(uri))
return uri;
}
}
return null;
}

public static OtpBase getOtp(OtpAuthData data)
{
if (data.Type == OtpType.Totp)
Expand Down

0 comments on commit 65ed0c2

Please sign in to comment.