diff --git a/src/MauiSettings.sln b/src/MauiSettings.sln index 115d1bf..d7cb1a4 100644 --- a/src/MauiSettings.sln +++ b/src/MauiSettings.sln @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Projektmappenelemente", "Pr nuget.config = nuget.config EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedNetCoreLibrary", "..\..\SharedMauiCoreLibrary\src\SharedNetCoreLibrary\SharedNetCoreLibrary.csproj", "{CA7C2653-5783-4A4C-960A-97CA4038748F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -20,10 +22,17 @@ Global {9A79D717-2C07-48A4-A78D-BD252E9BCA25}.Debug|Any CPU.Build.0 = Debug|Any CPU {9A79D717-2C07-48A4-A78D-BD252E9BCA25}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A79D717-2C07-48A4-A78D-BD252E9BCA25}.Release|Any CPU.Build.0 = Release|Any CPU + {CA7C2653-5783-4A4C-960A-97CA4038748F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CA7C2653-5783-4A4C-960A-97CA4038748F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CA7C2653-5783-4A4C-960A-97CA4038748F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CA7C2653-5783-4A4C-960A-97CA4038748F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {CA7C2653-5783-4A4C-960A-97CA4038748F} = {26232A22-4BBC-4509-99A5-987CE67F1882} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2733F837-2A02-4993-BB5C-771792807C3C} EndGlobalSection diff --git a/src/MauiSettings/Attributes/MauiSettingAttribute.cs b/src/MauiSettings/Attributes/MauiSettingAttribute.cs index 3cd18ba..848b401 100644 --- a/src/MauiSettings/Attributes/MauiSettingAttribute.cs +++ b/src/MauiSettings/Attributes/MauiSettingAttribute.cs @@ -13,6 +13,8 @@ public class MauiSettingAttribute : MauiSettingBaseAttribute { #region Properties public bool Secure { get; set; } = false; + public bool Encrypt { get; set; } = false; + public bool SkipForExport { get; set; } = false; #endregion } } diff --git a/src/MauiSettings/Helper/MauiSettingsInfo.cs b/src/MauiSettings/Helper/MauiSettingsInfo.cs index 933feaa..4edf04a 100644 --- a/src/MauiSettings/Helper/MauiSettingsInfo.cs +++ b/src/MauiSettings/Helper/MauiSettingsInfo.cs @@ -14,6 +14,9 @@ internal class MauiSettingsInfo public object Value { get; set; } public Type SettingsType { get; set; } public object Default { get; set; } + public bool IsSecure { get; set; } = false; + public bool Encrypt { get; set; } = false; + public bool SkipForExport { get; set; } = false; #endregion } } diff --git a/src/MauiSettings/MauiSettings.cs b/src/MauiSettings/MauiSettings.cs index a0415d9..9a138fa 100644 --- a/src/MauiSettings/MauiSettings.cs +++ b/src/MauiSettings/MauiSettings.cs @@ -11,9 +11,7 @@ // All the code in this file is included in all platforms. public class MauiSettings : MauiSettingsGeneric where T : new() { - public MauiSettings() - { - - } + public MauiSettings() { } + public MauiSettings(T settingsObject, string key) : base(settingsObject, key) { } } } \ No newline at end of file diff --git a/src/MauiSettings/MauiSettings.csproj b/src/MauiSettings/MauiSettings.csproj index 039a980..922e221 100644 --- a/src/MauiSettings/MauiSettings.csproj +++ b/src/MauiSettings/MauiSettings.csproj @@ -38,9 +38,14 @@ - - + + + + + + + diff --git a/src/MauiSettings/MauiSettingsGeneric.cs b/src/MauiSettings/MauiSettingsGeneric.cs index 99e0867..d5205eb 100644 --- a/src/MauiSettings/MauiSettingsGeneric.cs +++ b/src/MauiSettings/MauiSettingsGeneric.cs @@ -5,10 +5,12 @@ using AndreasReitberger.Maui.Enums; using AndreasReitberger.Maui.Helper; using AndreasReitberger.Maui.Utilities; +using AndreasReitberger.Shared.Core.Utilities; using System.Collections.Concurrent; using System.Linq.Expressions; using System.Reflection; using System.Runtime.CompilerServices; +using System.Security; namespace AndreasReitberger.Maui { @@ -39,6 +41,7 @@ public static SO SettingsObject #endregion #region Variables + static string _passPhrase = string.Empty; static readonly object lockObject = new(); #endregion @@ -48,6 +51,11 @@ public MauiSettingsGeneric(SO settingsObject) { _settingsObject = settingsObject; } + public MauiSettingsGeneric(SO settingsObject, string key) + { + _settingsObject = settingsObject; + _passPhrase = key; + } #endregion #region Methods @@ -253,17 +261,14 @@ public static void LoadDefaultSettings(object settings) #region Conversion - public static async Task>> ToDictionaryAsync() - { - return await ToDictionaryAsync(settings: SettingsObject); - } + public static Task>> ToDictionaryAsync() + => ToDictionaryAsync(settings: SettingsObject); + public static async Task>> ToDictionaryAsync(object settings) { if (true) { - Dictionary> setting = new(); - //List members = GetClassMetaAsList(settings); - + Dictionary> setting = []; IEnumerable declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; MauiSettingsMemberInfo settingsObjectInfo = new(); @@ -275,7 +280,7 @@ public static async Task>> ToDictionaryAs settingsObjectInfo.Info = mInfo; // Handles saving the settings to the Maui.Storage.Preferences MauiSettingsInfo settingsPair = await ProcessSettingsInfoAsKeyValuePairAsync(settingsObjectInfo, settingsInfo); - if (settingsPair != null) + if (settingsPair != null && !settingsPair.SkipForExport) { setting.TryAdd(settingsPair.Name, new Tuple(settingsPair.Value ?? settingsPair.Default, settingsPair.SettingsType)); } @@ -296,10 +301,9 @@ public static async Task>> ToDictionaryAs } } - public static async Task>> ToConcurrentDictionaryAsync() - { - return await ToConcurrentDictionaryAsync(settings: SettingsObject); - } + public static Task>> ToConcurrentDictionaryAsync() + => ToConcurrentDictionaryAsync(settings: SettingsObject); + public static async Task>> ToConcurrentDictionaryAsync(object settings) { ConcurrentDictionary> setting = new(); @@ -314,7 +318,7 @@ public static async Task>> ToCo settingsObjectInfo.Info = mInfo; // Handles saving the settings to the Maui.Storage.Preferences MauiSettingsInfo settingsPair = await ProcessSettingsInfoAsKeyValuePairAsync(settingsObjectInfo, settingsInfo); - if (settingsPair != null) + if (settingsPair != null && !settingsPair.SkipForExport) { setting.TryAdd(settingsPair.Name, new Tuple(settingsPair.Value ?? settingsPair.Default, settingsPair.SettingsType)); } @@ -334,11 +338,8 @@ public static async Task>> ToCo return setting; } - public static async Task>> ToSettingsTupleAsync(Expression> value) - { - return await ToSettingsTupleAsync(settings: SettingsObject, value: value); - } - + public static Task>> ToSettingsTupleAsync(Expression> value) => ToSettingsTupleAsync(settings: SettingsObject, value: value); + public static async Task>> ToSettingsTupleAsync(object settings, Expression> value) { MauiSettingsInfo info = await GetExpressionMetaAsKeyValuePairAsync(settings: settings, value: value); @@ -522,11 +523,15 @@ List settingBaseAttributes } //settingsInfo.Value = MauiSettingsHelper.GetSettingsValue(settingsInfo.Name, settingsInfo.Default); } - bool? secure = false; + bool secure = false; if (settingBaseAttribute is MauiSettingAttribute settingAttribute) { secure = settingAttribute.Secure; - if (secure ?? false) + // Save the states + settingsInfo.IsSecure = secure; + settingsInfo.Encrypt = settingAttribute.Encrypt; + settingsInfo.SkipForExport = settingAttribute.SkipForExport; + if (secure) { #if IOS switch (target) @@ -552,7 +557,6 @@ List settingBaseAttributes if (settingBaseAttribute?.DefaultValueInUse ?? false) { object defaultValue = MauiSettingsObjectHelper.GetDefaultValue(settingBaseAttribute, settingsInfo.SettingsType); - } // Sets the loaded value back to the settingsObject MauiSettingsObjectHelper.SetSettingValue(settingsObjectInfo.Info, settingsObjectInfo.OrignalSettingsObject, settingsInfo.Value, settingsInfo.SettingsType); @@ -656,6 +660,10 @@ List settingBaseAttributes if (settingBaseAttribute is MauiSettingAttribute settingAttribute) { secure = settingAttribute.Secure; + // Save the states + settingsInfo.IsSecure = secure; + settingsInfo.Encrypt = settingAttribute.Encrypt; + settingsInfo.SkipForExport = settingAttribute.SkipForExport; if (!secure) { // If only secure storage should be loaded, stop here. @@ -773,7 +781,14 @@ List settingBaseAttributes { if (settingsInfo.Value is string secureString) { - await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); + if (settingsInfo.Encrypt && !string.IsNullOrEmpty(_passPhrase)) + { + // Decrypt string + string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, _passPhrase); + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString); + } + else + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); } else { @@ -817,7 +832,14 @@ List settingBaseAttributes { if (settingsInfo.Value is string secureString) { - await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); + if (settingsInfo.Encrypt && !string.IsNullOrEmpty(_passPhrase)) + { + // Decrypt string + string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, _passPhrase); + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString); + } + else + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); } else { @@ -852,7 +874,14 @@ List settingBaseAttributes { if (settingsInfo.Value is string secureString) { - await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); + if (settingsInfo.Encrypt && !string.IsNullOrEmpty(_passPhrase)) + { + // Decrypt string + string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, _passPhrase); + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString); + } + else + await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString); } else { @@ -894,7 +923,6 @@ List settingBaseAttributes settingsInfo.SettingsType = (settingsInfo.SettingsType = MauiSettingsObjectHelper.GetSettingType(settingsObjectInfo.Info)); settingsInfo.Default = MauiSettingsObjectHelper.GetDefaultValue(settingBaseAttribute, settingsInfo.SettingsType); - //Type type = (settingsInfo.SettingsType = MauiSettingsObjectHelper.GetSettingType(settingsObjectInfo.Info)); //settingsInfo.Value = MauiSettingsObjectHelper.GetSettingValue(settingsObjectInfo.Info, settingsObjectInfo.OrignalSettingsObject); //settingsInfo.Value = MauiSettingsHelper.GetSettingsValue(settingsInfo.Name, settingsInfo.Default); @@ -902,6 +930,10 @@ List settingBaseAttributes if (settingBaseAttribute is MauiSettingAttribute settingAttribute) { bool secure = settingAttribute.Secure; + // Save the states + settingsInfo.IsSecure = secure; + settingsInfo.Encrypt = settingAttribute.Encrypt; + settingsInfo.SkipForExport = settingAttribute.SkipForExport; if (!secure) { // If only secure storage should be loaded, stop here. @@ -912,6 +944,12 @@ List settingBaseAttributes else if (settingsInfo.SettingsType == typeof(string)) { settingsInfo.Value = await MauiSettingsHelper.GetSecureSettingsValueAsync(settingsInfo.Name, settingsInfo.Default as string); + if (settingsInfo.Encrypt && !string.IsNullOrEmpty(_passPhrase)) + { + // Decrypt string + string decryptedString = EncryptionManager.DecryptStringFromBase64String(settingsInfo.Value as string, _passPhrase); + settingsInfo.Value = decryptedString; + } } else {