From 28c4b5caed58ce0163f5411e7e3f7f6ce227339f Mon Sep 17 00:00:00 2001 From: luckyrat Date: Tue, 20 Feb 2024 13:14:52 +0000 Subject: [PATCH] wip --- .../browser_entry_settings.dart | 7 ++++--- .../browser_entry_settings_v1.dart | 12 +++++++---- .../kee_vault_model/entry_matcher_config.dart | 11 +++++----- lib/src/kee_vault_model/field.dart | 12 ++++++----- lib/src/kee_vault_model/field_matcher.dart | 20 ++++++++++--------- .../kee_vault_model/field_matcher_config.dart | 9 +++++---- test/browser_entry_settings_test.dart | 13 +++++++++--- 7 files changed, 51 insertions(+), 33 deletions(-) diff --git a/lib/src/kee_vault_model/browser_entry_settings.dart b/lib/src/kee_vault_model/browser_entry_settings.dart index 6c1ac82..f8e2f0f 100644 --- a/lib/src/kee_vault_model/browser_entry_settings.dart +++ b/lib/src/kee_vault_model/browser_entry_settings.dart @@ -173,10 +173,11 @@ class BrowserEntrySettings { return { 'version': version, 'authenticationMethods': authenticationMethods, - 'hTTPRealm': realm, - if (fields != null) 'fields': fields?.map((x) => x.toMap()).toList(), - 'behaviour': behaviour?.name, + if (realm?.isNotEmpty ?? false) 'hTTPRealm': realm, 'matcherConfigs': matcherConfigs.map((x) => x.toMap()).toList(), + if (fields != null) 'fields': fields?.map((x) => x.toMap()).toList(), + if (behaviour != null && behaviour != BrowserAutoFillBehaviour.Default) + 'behaviour': behaviour?.name, ...parseUrls(includeUrls, excludeUrls), }; } diff --git a/lib/src/kee_vault_model/browser_entry_settings_v1.dart b/lib/src/kee_vault_model/browser_entry_settings_v1.dart index 7610644..052f415 100644 --- a/lib/src/kee_vault_model/browser_entry_settings_v1.dart +++ b/lib/src/kee_vault_model/browser_entry_settings_v1.dart @@ -52,10 +52,14 @@ class BrowserEntrySettingsV1 { } factory BrowserEntrySettingsV1.fromJson(String source, - {required MatchAccuracy minimumMatchAccuracy}) => - BrowserEntrySettingsV1.fromMap( - json.decode(source) as Map?, - minimumMatchAccuracy: minimumMatchAccuracy); + {required MatchAccuracy minimumMatchAccuracy}) { + if (source.isEmpty) { + return BrowserEntrySettingsV1(minimumMatchAccuracy: minimumMatchAccuracy); + } + return BrowserEntrySettingsV1.fromMap( + json.decode(source) as Map?, + minimumMatchAccuracy: minimumMatchAccuracy); + } int version; // enum diff --git a/lib/src/kee_vault_model/entry_matcher_config.dart b/lib/src/kee_vault_model/entry_matcher_config.dart index 557e500..a0b9220 100644 --- a/lib/src/kee_vault_model/entry_matcher_config.dart +++ b/lib/src/kee_vault_model/entry_matcher_config.dart @@ -41,11 +41,12 @@ class EntryMatcherConfig { Map toMap() { return { 'matcherType': matcherType?.name, - 'customMatcher': customMatcher?.toMap(), - 'urlMatchMethod': urlMatchMethod?.name, - 'weight': weight, - 'actionOnMatch': actionOnMatch?.name, - 'actionOnNoMatch': actionOnNoMatch?.name, + if (customMatcher != null) 'customMatcher': customMatcher?.toMap(), + if (urlMatchMethod != null && urlMatchMethod != MatchAccuracy.Domain) + 'urlMatchMethod': urlMatchMethod?.name, + if (weight != null) 'weight': weight, + if (actionOnMatch != null) 'actionOnMatch': actionOnMatch?.name, + if (actionOnNoMatch != null) 'actionOnNoMatch': actionOnNoMatch?.name, }; } diff --git a/lib/src/kee_vault_model/field.dart b/lib/src/kee_vault_model/field.dart index 8767af5..848f02a 100644 --- a/lib/src/kee_vault_model/field.dart +++ b/lib/src/kee_vault_model/field.dart @@ -42,14 +42,16 @@ class Field { Map toMap() { return { - 'uuid': uuid, - 'name': name, - 'valuePath': valuePath, - 'value': value, 'page': page, + 'valuePath': valuePath, + 'uuid': uuid, 'type': type?.name, - 'placeholderHandling': placeholderHandling?.name, 'matcherConfigs': matcherConfigs?.map((x) => x.toMap()).toList(), + if (name?.isNotEmpty ?? false) 'name': name, + if (value?.isNotEmpty ?? false) 'value': value, + if (placeholderHandling != null && + placeholderHandling != PlaceholderHandling.Default) + 'placeholderHandling': placeholderHandling?.name, }; } diff --git a/lib/src/kee_vault_model/field_matcher.dart b/lib/src/kee_vault_model/field_matcher.dart index db1f545..8186688 100644 --- a/lib/src/kee_vault_model/field_matcher.dart +++ b/lib/src/kee_vault_model/field_matcher.dart @@ -41,15 +41,17 @@ class FieldMatcher { Map toMap() { return { - 'matchLogic': matchLogic?.name, - 'ids': ids, - 'names': names, - 'types': types, - 'queries': queries, - 'labels': labels, - 'autocompleteValues': autocompleteValues, - 'maxLength': maxLength, - 'minLength': minLength, + if (matchLogic != null && matchLogic != MatcherLogic.Client) + 'matchLogic': matchLogic?.name, + if (ids.isNotEmpty) 'ids': ids, + if (names.isNotEmpty) 'names': names, + if (types.isNotEmpty) 'types': types, + if (queries.isNotEmpty) 'queries': queries, + if (labels.isNotEmpty) 'labels': labels, + if (autocompleteValues.isNotEmpty) + 'autocompleteValues': autocompleteValues, + if (maxLength != null) 'maxLength': maxLength, + if (minLength != null) 'minLength': minLength, }; } diff --git a/lib/src/kee_vault_model/field_matcher_config.dart b/lib/src/kee_vault_model/field_matcher_config.dart index f2bf121..3076fd4 100644 --- a/lib/src/kee_vault_model/field_matcher_config.dart +++ b/lib/src/kee_vault_model/field_matcher_config.dart @@ -50,10 +50,11 @@ class FieldMatcherConfig { Map toMap() { return { - 'matcherType': matcherType?.name, - 'customMatcher': customMatcher?.toMap(), - 'weight': weight, - 'actionOnMatch': actionOnMatch?.name, + if (matcherType != null && matcherType != FieldMatcherType.Custom) + 'matcherType': matcherType?.name, + if (customMatcher != null) 'customMatcher': customMatcher?.toMap(), + if (weight != null) 'weight': weight, + if (actionOnMatch != null) 'actionOnMatch': actionOnMatch?.name, }; } diff --git a/test/browser_entry_settings_test.dart b/test/browser_entry_settings_test.dart index dc1f7db..a5ba1e8 100644 --- a/test/browser_entry_settings_test.dart +++ b/test/browser_entry_settings_test.dart @@ -76,11 +76,18 @@ void main() { test('config v1->v2', () async { testCaseToV2( '{"version":1,"hTTPRealm":"","formFieldList":[{"name":"password","displayName":"KeePass password","value":"{PASSWORD}","type":"FFTpassword","id":"password","page":-1,"placeholderHandling":"Default"},{"name":"username","displayName":"KeePass username","value":"{USERNAME}","type":"FFTradio","id":"username","page":-1,"placeholderHandling":"Default"}],"alwaysAutoFill":false,"neverAutoFill":false,"alwaysAutoSubmit":false,"neverAutoSubmit":false,"priority":0,"altURLs":[],"hide":true,"blockHostnameOnlyMatch":false,"blockDomainOnlyMatch":false}', - '{"version":2,"authenticationMethods":["password"],"matcherConfigs":[{"matcherType":"Url"},{"matcherType":"Hide"}],"fields":[{"page":1,"valuePath":"Password","uuid":"00000000-0000-0000-0000-000000000000","type":"Password","matcherConfigs":[{"customMatcher":{"ids":["password"],"names":["password"],"types":["password"],"queries":[]}}]},{"page":1,"valuePath":"UserName","uuid":"00000000-0000-0000-0000-000000000000","type":"Text","matcherConfigs":[{"customMatcher":{"ids":["username"],"names":["username"],"types":["text"],"queries":[]}}]}],"altUrls":[]}'); + '{"version":2,"authenticationMethods":["password"],"matcherConfigs":[{"matcherType":"Url"},{"matcherType":"Hide"}],"fields":[{"page":1,"valuePath":"Password","uuid":"00000000-0000-0000-0000-000000000000","type":"Password","matcherConfigs":[{"customMatcher":{"ids":["password"],"names":["password"],"types":["password"]}}]},{"page":1,"valuePath":"UserName","uuid":"00000000-0000-0000-0000-000000000000","type":"Text","matcherConfigs":[{"customMatcher":{"ids":["username"],"names":["username"],"types":["text"]}}]}]}'); - // testCase("{\"version\":1,\"hTTPRealm\":\"\",\"formFieldList\":[{\"name\":\"password\",\"displayName\":\"KeePass password\",\"value\":\"{PASSWORD}\",\"type\":\"FFTpassword\",\"id\":\"password\",\"page\":-1,\"placeholderHandling\":\"Default\"},{\"name\":\"username\",\"displayName\":\"KeePass username\",\"value\":\"{USERNAME}\",\"type\":\"FFTradio\",\"id\":\"username\",\"page\":-1,\"placeholderHandling\":\"Default\"}],\"alwaysAutoFill\":false,\"neverAutoFill\":false,\"alwaysAutoSubmit\":false,\"neverAutoSubmit\":false,\"priority\":0,\"altURLs\":[],\"hide\":true,\"blockHostnameOnlyMatch\":false,\"blockDomainOnlyMatch\":false}", + testCaseToV2( + '{"version":1,"hTTPRealm":"","formFieldList":[{"name":"password","displayName":"KeePass password","value":"{PASSWORD}","type":"FFTpassword","id":"password","page":-1,"placeholderHandling":"Default"},{"name":"username","displayName":"KeePass username","value":"{USERNAME}","type":"FFTradio","id":"username","page":-1,"placeholderHandling":"Default"}],"alwaysAutoFill":false,"neverAutoFill":false,"alwaysAutoSubmit":false,"neverAutoSubmit":false,"priority":0,"altURLs":[],"hide":false,"blockHostnameOnlyMatch":false,"blockDomainOnlyMatch":false}', + '{"version":2,"authenticationMethods":["password"],"matcherConfigs":[{"matcherType":"Url"}],"fields":[{"page":1,"valuePath":"Password","uuid":"00000000-0000-0000-0000-000000000000","type":"Password","matcherConfigs":[{"customMatcher":{"ids":["password"],"names":["password"],"types":["password"]}}]},{"page":1,"valuePath":"UserName","uuid":"00000000-0000-0000-0000-000000000000","type":"Text","matcherConfigs":[{"customMatcher":{"ids":["username"],"names":["username"],"types":["text"]}}]}]}'); + + testCaseToV2( + '{"version":1,"hTTPRealm":"","formFieldList":[{"name":"password","displayName":"KeePass password","value":"{PASSWORD}","type":"FFTpassword","id":"password","page":-1,"placeholderHandling":"Default"},{"name":"username","displayName":"KeePass username","value":"{USERNAME}","type":"FFTusername","id":"username","page":-1,"placeholderHandling":"Default"}],"alwaysAutoFill":false,"neverAutoFill":false,"alwaysAutoSubmit":false,"neverAutoSubmit":false,"priority":0,"altURLs":[],"hide":false,"blockHostnameOnlyMatch":false,"blockDomainOnlyMatch":false}', + '{"version":2,"authenticationMethods":["password"],"matcherConfigs":[{"matcherType":"Url"}],"fields":[{"page":1,"valuePath":"Password","uuid":"00000000-0000-0000-0000-000000000000","type":"Password","matcherConfigs":[{"customMatcher":{"ids":["password"],"names":["password"],"types":["password"]}}]},{"page":1,"valuePath":"UserName","uuid":"00000000-0000-0000-0000-000000000000","type":"Text","matcherConfigs":[{"customMatcher":{"ids":["username"],"names":["username"],"types":["text"]}}]}]}'); - // "{\"version\":2,\"altUrls\":[],\"authenticationMethods\":[\"password\"],\"matcherConfigs\":[{\"matcherType\":\"Url\"},{\"matcherType\":\"Hide\"}],\"fields\":[{\"page\":1,\"valuePath\":\"Password\",\"uuid\":\"00000000-0000-0000-0000-000000000000\",\"type\":\"Password\",\"matcherConfigs\":[{\"customMatcher\":{\"ids\":[\"password\"],\"names\":[\"password\"],\"types\":[\"password\"],\"queries\":[]}}]},{\"page\":1,\"valuePath\":\"UserName\",\"uuid\":\"00000000-0000-0000-0000-000000000000\",\"type\":\"Text\",\"matcherConfigs\":[{\"customMatcher\":{\"ids\":[\"username\"],\"names\":[\"username\"],\"types\":[\"text\"],\"queries\":[]}}]}]}"); + testCaseToV2('', + '{"version":2,"authenticationMethods":["password"],"matcherConfigs":[{"matcherType":"Url"}],"fields":[{"page":1,"valuePath":"UserName","uuid":"00000000-0000-0000-0000-000000000000","type":"Text","matcherConfigs":[{"matcherType":"UsernameDefaultHeuristic"}]},{"page":1,"valuePath":"Password","uuid":"00000000-0000-0000-0000-000000000000","type":"Password","matcherConfigs":[{"matcherType":"PasswordDefaultHeuristic"}]}]}'); }); }); }