From 002c67e48c2864674131830ec61088d537c85552 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Tue, 23 Jan 2018 19:53:06 +0100 Subject: [PATCH] support otpauth:// URIs in otp field as used by KeeWeb (closes #118) --- .../Totp/KeeWebOtpPluginAdapter.cs | 43 +++++++++++++++++++ src/keepass2android/Totp/Kp2aTotp.cs | 3 +- src/keepass2android/keepass2android.csproj | 2 + 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/keepass2android/Totp/KeeWebOtpPluginAdapter.cs diff --git a/src/keepass2android/Totp/KeeWebOtpPluginAdapter.cs b/src/keepass2android/Totp/KeeWebOtpPluginAdapter.cs new file mode 100644 index 00000000..ab53bcb3 --- /dev/null +++ b/src/keepass2android/Totp/KeeWebOtpPluginAdapter.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Web; +using Android.Content; +using PluginTOTP; + +namespace keepass2android +{ + internal class KeeWebOtpPluginAdapter : ITotpPluginAdapter + { + public TotpData GetTotpData(IDictionary entryFields, Context ctx, bool muteWarnings) + { + TotpData res = new TotpData(); + string data; + if (!entryFields.TryGetValue("otp", out data)) + { + return res; + } + + string otpUriStart = "otpauth://totp/"; + + if (!data.StartsWith(otpUriStart)) + return res; + + + try + { + Uri myUri = new Uri(data); + var parsedQuery = HttpUtility.ParseQueryString(myUri.Query); + res.TotpSeed = parsedQuery.Get("secret"); + res.Length = parsedQuery.Get("digits"); + res.Duration = parsedQuery.Get("period"); + } + catch (Exception) + { + return res; + } + + res.IsTotpEnry = true; + return res; + } + } +} \ No newline at end of file diff --git a/src/keepass2android/Totp/Kp2aTotp.cs b/src/keepass2android/Totp/Kp2aTotp.cs index 04f4af99..9a67b71c 100644 --- a/src/keepass2android/Totp/Kp2aTotp.cs +++ b/src/keepass2android/Totp/Kp2aTotp.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; @@ -11,7 +10,7 @@ namespace keepass2android class Kp2aTotp { - readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[] { new TrayTotpPluginAdapter(), new KeeOtpPluginAdapter() }; + readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[] { new TrayTotpPluginAdapter(), new KeeOtpPluginAdapter(), new KeeWebOtpPluginAdapter() }; public void OnOpenEntry() { diff --git a/src/keepass2android/keepass2android.csproj b/src/keepass2android/keepass2android.csproj index d2307ba4..d7765465 100644 --- a/src/keepass2android/keepass2android.csproj +++ b/src/keepass2android/keepass2android.csproj @@ -102,6 +102,7 @@ + @@ -255,6 +256,7 @@ +