Skip to content

Commit 31255f0

Browse files
committed
move TOTP button on keyboard to third position (after User/Pwd) and disable TOTP "internal fields"
closes #1341 closes #2394 closes #1844
1 parent 059280e commit 31255f0

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

src/keepass2android/Totp/KeeOtpPluginAdapter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ public TotpData GetData()
3939
{
4040
TotpData res = new TotpData();
4141
string data;
42-
if (!_entryFields.TryGetValue("otp", out data))
42+
var otpKey = "otp";
43+
if (!_entryFields.TryGetValue(otpKey, out data))
4344
{
4445
return res;
4546
}
4647
NameValueCollection parameters = ParseQueryString(data);
48+
res.InternalFields.Add(otpKey);
4749

4850
if (parameters[KeyParameter] == null)
4951
{

src/keepass2android/Totp/KeeWebOtpPluginAdapter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public TotpData GetTotpData(IDictionary<string, string> entryFields, Context ctx
1616
{
1717
return res;
1818
}
19+
res.InternalFields.Add("otp");
1920

2021
string otpUriStart = "otpauth://totp/";
2122

src/keepass2android/Totp/Keepass2TotpPluginAdapter.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ class Keepass2TotpPluginAdapter : ITotpPluginAdapter
1313
public TotpData GetTotpData(IDictionary<string, string> entryFields, Context ctx, bool muteWarnings)
1414
{
1515
TotpData res = new TotpData();
16-
byte[] pbSecret = (GetOtpSecret(entryFields, "TimeOtp-") ?? MemUtil.EmptyByteArray);
16+
byte[] pbSecret = (GetOtpSecret(entryFields, "TimeOtp-", out string secretFieldKey) ?? MemUtil.EmptyByteArray);
17+
1718
if (pbSecret.Length == 0)
1819
return res;
1920

21+
res.InternalFields.Add(secretFieldKey);
22+
2023
string strPeriod;
2124
uint uPeriod = 0;
2225
if (entryFields.TryGetValue("TimeOtp-Period", out strPeriod))
2326
{
27+
res.InternalFields.Add("TimeOtp-Period");
2428
uint.TryParse(strPeriod, out uPeriod);
2529
}
2630

@@ -33,6 +37,7 @@ public TotpData GetTotpData(IDictionary<string, string> entryFields, Context ctx
3337
uint uLength = 0;
3438
if (entryFields.TryGetValue("TimeOtp-Length", out strLength))
3539
{
40+
res.InternalFields.Add("TimeOtp-Length");
3641
uint.TryParse(strLength, out uLength);
3742
}
3843

@@ -41,6 +46,8 @@ public TotpData GetTotpData(IDictionary<string, string> entryFields, Context ctx
4146

4247
string strAlg;
4348
entryFields.TryGetValue("TimeOtp-Algorithm", out strAlg);
49+
if (!string.IsNullOrEmpty(strAlg))
50+
res.InternalFields.Add("TimeOtp-Algorithm");
4451

4552
res.HashAlgorithm = strAlg;
4653
res.TotpSecret = pbSecret;
@@ -51,32 +58,37 @@ public TotpData GetTotpData(IDictionary<string, string> entryFields, Context ctx
5158
}
5259

5360

54-
private static byte[] GetOtpSecret(IDictionary<string, string> entryFields, string strPrefix)
61+
private static byte[] GetOtpSecret(IDictionary<string, string> entryFields, string strPrefix, out string secretFieldKey)
5562
{
5663
try
5764
{
5865
string str;
59-
entryFields.TryGetValue(strPrefix + "Secret", out str);
60-
if (!string.IsNullOrEmpty(str))
66+
secretFieldKey = strPrefix + "Secret";
67+
entryFields.TryGetValue(secretFieldKey, out str);
68+
if (!string.IsNullOrEmpty(str))
6169
return StrUtil.Utf8.GetBytes(str);
62-
63-
entryFields.TryGetValue(strPrefix + "Secret-Hex", out str);
70+
71+
secretFieldKey = strPrefix + "Secret-Hex";
72+
entryFields.TryGetValue(secretFieldKey, out str);
6473
if (!string.IsNullOrEmpty(str))
6574
return MemUtil.HexStringToByteArray(str);
66-
67-
entryFields.TryGetValue(strPrefix + "Secret-Base32", out str);
75+
76+
secretFieldKey = strPrefix + "Secret-Base32";
77+
entryFields.TryGetValue(secretFieldKey, out str);
6878
if (!string.IsNullOrEmpty(str))
6979
return MemUtil.ParseBase32(str);
7080

71-
entryFields.TryGetValue(strPrefix + "Secret-Base64", out str);
81+
secretFieldKey = strPrefix + "Secret-Base64";
82+
entryFields.TryGetValue(secretFieldKey, out str);
7283
if (!string.IsNullOrEmpty(str))
7384
return Convert.FromBase64String(str);
85+
7486
}
7587
catch (Exception e)
7688
{
7789
Kp2aLog.LogUnexpectedError(e);
7890
}
79-
91+
secretFieldKey = null;
8092
return null;
8193
}
8294
}

src/keepass2android/Totp/TotpData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ public string TotpSeed
3131
public string TimeCorrectionUrl { get; set; }
3232

3333
public string HashAlgorithm { get; set; }
34-
34+
3535
public bool IsDefaultRfc6238
3636
{
3737
get { return Length == "6" && Duration == "30" && (HashAlgorithm == null || HashAlgorithm == HashSha1); }
3838
}
3939

40+
public List<string> InternalFields { get; set; } = new List<string>();
41+
4042
public static TotpData MakeDefaultRfc6238()
4143
{
4244
return new TotpData()

src/keepass2android/Totp/TrayTotpPluginAdapter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public TotpData GetTotpData(IDictionary<string, string> entryFields)
115115
{
116116
bool NoTimeCorrection = false;
117117
string[] Settings = SettingsGet(entryFields);
118+
res.InternalFields.Add(SettingsFieldName);
119+
res.InternalFields.Add(SeedFieldName);
118120
res.Duration = Settings[0];
119121
res.Length = Settings[1];
120122
if (res.Length == "S")

src/keepass2android/services/CopyToClipboardService.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ private bool ClearNotifications()
546546
return hadKeyboardData;
547547
}
548548

549+
549550
bool MakeAccessibleForKeyboard(PwEntryOutput entry, string searchUrl)
550551
{
551552
#if EXCLUDE_KEYBOARD
@@ -554,38 +555,41 @@ bool MakeAccessibleForKeyboard(PwEntryOutput entry, string searchUrl)
554555
bool hasData = false;
555556
Keepass2android.Kbbridge.KeyboardDataBuilder kbdataBuilder = new Keepass2android.Kbbridge.KeyboardDataBuilder();
556557

557-
String[] keys = {PwDefs.UserNameField,
558+
String[] standardKeys = {PwDefs.UserNameField,
558559
PwDefs.PasswordField,
560+
Kp2aTotp.TotpKey,
559561
PwDefs.UrlField,
560562
PwDefs.NotesField,
561563
PwDefs.TitleField
562564
};
563565
int[] resIds = {Resource.String.entry_user_name,
564566
Resource.String.entry_password,
567+
0,
565568
Resource.String.entry_url,
566569
Resource.String.entry_comment,
567570
Resource.String.entry_title };
568571

569572
//add standard fields:
570573
int i = 0;
571-
foreach (string key in keys)
574+
foreach (string key in standardKeys)
572575
{
573576
String value = entry.OutputStrings.ReadSafe(key);
574577

575578
if (value.Length > 0)
576579
{
577-
kbdataBuilder.AddString(key, GetString(resIds[i]), value);
580+
kbdataBuilder.AddString(key, resIds[i] > 0 ? GetString(resIds[i]) : key, value);
578581
hasData = true;
579582
}
580583
i++;
581584
}
582585
//add additional fields:
586+
var totpData = new Kp2aTotp().TryGetTotpData(entry);
583587
foreach (var pair in entry.OutputStrings)
584588
{
585589
var key = pair.Key;
586590
var value = pair.Value.ReadString();
587591

588-
if (!PwDefs.IsStandardField(key))
592+
if (!standardKeys.Contains(key) && totpData?.InternalFields.Contains(key) != true)
589593
{
590594
kbdataBuilder.AddString(pair.Key, pair.Key, value);
591595
hasData = true;

0 commit comments

Comments
 (0)