Skip to content

Commit

Permalink
code simplification and fix for Autofill not being able to save crede…
Browse files Browse the repository at this point in the history
…ntials, closes #2269
  • Loading branch information
PhilippC committed Mar 16, 2023
1 parent 6f10a04 commit 6110166
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
21 changes: 12 additions & 9 deletions src/Kp2aAutofillParser/AutofillParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static bool isW3cTypeHint(string hint)
/// </summary>
public class FilledAutofillFieldCollection<FieldT> where FieldT:InputField
{
public Dictionary<string, FilledAutofillField<FieldT>> HintMap { get; }
public Dictionary<string, FilledAutofillField> HintMap { get; }
public string DatasetName { get; set; }

public FilledAutofillFieldCollection(Dictionary<string, FilledAutofillField<FieldT>> hintMap, string datasetName = "")
public FilledAutofillFieldCollection(Dictionary<string, FilledAutofillField> hintMap, string datasetName = "")
{
//recreate hint map making sure we compare case insensitive
HintMap = BuildHintMap();
Expand All @@ -149,17 +149,17 @@ public FilledAutofillFieldCollection(Dictionary<string, FilledAutofillField<Fiel
public FilledAutofillFieldCollection() : this(BuildHintMap())
{ }

private static Dictionary<string, FilledAutofillField<FieldT>> BuildHintMap()
private static Dictionary<string, FilledAutofillField> BuildHintMap()
{
return new Dictionary<string, FilledAutofillField<FieldT>>(StringComparer.OrdinalIgnoreCase);
return new Dictionary<string, FilledAutofillField>(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Adds a filledAutofillField to the collection, indexed by all of its hints.
/// </summary>
/// <returns>The add.</returns>
/// <param name="filledAutofillField">Filled autofill field.</param>
public void Add(FilledAutofillField<FieldT> filledAutofillField)
public void Add(FilledAutofillField filledAutofillField)
{
foreach (string hint in filledAutofillField.AutofillHints)
{
Expand Down Expand Up @@ -572,7 +572,7 @@ public static DateTime ConvertUnixTime(double dtUnix)
}
}

public class FilledAutofillField<FieldT> where FieldT : InputField
public class FilledAutofillField
{
private string[] _autofillHints;
public string TextValue { get; set; }
Expand Down Expand Up @@ -611,13 +611,13 @@ public string[] AutofillHints
public FilledAutofillField()
{ }

public FilledAutofillField(FieldT inputField)
public FilledAutofillField(InputField inputField)
: this(inputField, inputField.AutofillHints)
{

}

public FilledAutofillField(FieldT inputField, string[] hints)
public FilledAutofillField(InputField inputField, string[] hints)
{

string[] rawHints = AutofillHintsHelper.FilterForSupportedHints(hints);
Expand Down Expand Up @@ -665,6 +665,7 @@ public FilledAutofillField(FieldT inputField, string[] hints)
}
}
AutofillHints = AutofillHintsHelper.ConvertToCanonicalLowerCaseHints(hintList.ToArray()).ToArray();
inputField.FillFilledAutofillValue(this);


}
Expand All @@ -679,7 +680,7 @@ public override bool Equals(object obj)
if (this == obj) return true;
if (obj == null || GetType() != obj.GetType()) return false;

FilledAutofillField<FieldT> that = (FilledAutofillField<FieldT>)obj;
FilledAutofillField that = (FilledAutofillField)obj;

if (!TextValue?.Equals(that.TextValue) ?? that.TextValue != null)
return false;
Expand Down Expand Up @@ -717,6 +718,8 @@ public abstract class InputField
public string HtmlInfoTag { get; set; }
public string HtmlInfoTypeAttribute { get; set; }

public abstract void FillFilledAutofillValue(FilledAutofillField filledField);

}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Kp2aAutofillParserTest/AutofillTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public AutofillTest(ITestOutputHelper testOutputHelper)
class TestInputField: InputField
{
public string[] ExpectedAssignedHints { get; set; }
public override void FillFilledAutofillValue(FilledAutofillField filledField)
{
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static bool ApplyToFields(FilledAutofillFieldCollection<ViewNodeInputFiel
{
foreach (AutofillFieldMetadata autofillFieldMetadata in autofillFieldMetadataCollection.GetFieldsForHint(hint))
{
FilledAutofillField<ViewNodeInputField> filledAutofillField;
FilledAutofillField filledAutofillField;
if (!filledAutofillFieldCollection.HintMap.TryGetValue(hint, out filledAutofillField) || (filledAutofillField == null))
{
continue;
Expand Down
9 changes: 7 additions & 2 deletions src/keepass2android/services/AutofillBase/StructureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ViewNodeInputField(AssistStructure.ViewNode viewNode)
[JsonIgnore]
public AssistStructure.ViewNode ViewNode { get; set; }

public void FillFilledAutofillValue(FilledAutofillField<ViewNodeInputField> filledField)
public override void FillFilledAutofillValue(FilledAutofillField filledField)
{
AutofillValue autofillValue = ViewNode.AutofillValue;
if (autofillValue != null)
Expand Down Expand Up @@ -159,17 +159,22 @@ public StructureParser(Context context, AssistStructure structure)

protected override AutofillTargetId Parse(bool forFill, bool isManualRequest, AutofillView<ViewNodeInputField> autofillView)
{
if (autofillView == null)
Kp2aLog.Log("Received null autofill view!");
var result = base.Parse(forFill, isManualRequest, autofillView);

Kp2aLog.Log("Parsing done");

if (forFill)
{
foreach (var p in FieldsMappedToHints)
AutofillFields.Add(new AutofillFieldMetadata(p.Key.ViewNode, p.Value));
}
else
{
ClientFormData = new FilledAutofillFieldCollection<ViewNodeInputField>();
foreach (var p in FieldsMappedToHints)
ClientFormData.Add(new FilledAutofillField<ViewNodeInputField>(p.Key, p.Value));
ClientFormData.Add(new FilledAutofillField(p.Key, p.Value));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public static FilledAutofillFieldCollection<ViewNodeInputField> GetFilledAutofil
foreach (string key in pwEntryOutput.OutputStrings.GetKeys())
{

FilledAutofillField<ViewNodeInputField> field =
new FilledAutofillField<ViewNodeInputField>
FilledAutofillField field =
new FilledAutofillField
{
AutofillHints = GetCanonicalHintsFromKp2aField(key).ToArray(),
TextValue = pwEntryOutput.OutputStrings.ReadSafe(key)
Expand All @@ -73,32 +73,32 @@ public static FilledAutofillFieldCollection<ViewNodeInputField> GetFilledAutofil
if (IsCreditCard(pwEntry, context) && pwEntry.Expires)
{
DateTime expTime = pwEntry.ExpiryTime;
FilledAutofillField<ViewNodeInputField> field =
new FilledAutofillField<ViewNodeInputField>
FilledAutofillField field =
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationDate},
DateValue = (long) (1000 * TimeUtil.SerializeUnix(expTime))
};
fieldCollection.Add(field);

field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationDay},
TextValue = expTime.Day.ToString()
};
fieldCollection.Add(field);

field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationMonth},
TextValue = expTime.Month.ToString()
};
fieldCollection.Add(field);

field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationYear},
TextValue = expTime.Year.ToString()
Expand Down

0 comments on commit 6110166

Please sign in to comment.