Skip to content

Commit

Permalink
Added Encryption support
Browse files Browse the repository at this point in the history
This commit adds support for encrypt and decrypt `Secure` properties.

Fixed #21
  • Loading branch information
AndreasReitberger committed Mar 19, 2024
1 parent 80775f1 commit 65966f1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 31 deletions.
9 changes: 9 additions & 0 deletions src/MauiSettings.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/MauiSettings/Attributes/MauiSettingAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 3 additions & 0 deletions src/MauiSettings/Helper/MauiSettingsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 2 additions & 4 deletions src/MauiSettings/MauiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
// All the code in this file is included in all platforms.
public class MauiSettings<T> : MauiSettingsGeneric<T> where T : new()
{
public MauiSettings()
{

}
public MauiSettings() { }
public MauiSettings(T settingsObject, string key) : base(settingsObject, key) { }
}
}
9 changes: 7 additions & 2 deletions src/MauiSettings/MauiSettings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.10" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharedNetCoreLibrary" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\SharedMauiCoreLibrary\src\SharedNetCoreLibrary\SharedNetCoreLibrary.csproj" />
</ItemGroup>

</Project>
88 changes: 63 additions & 25 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -39,6 +41,7 @@ public static SO SettingsObject
#endregion

#region Variables
static string _passPhrase = string.Empty;
static readonly object lockObject = new();
#endregion

Expand All @@ -48,6 +51,11 @@ public MauiSettingsGeneric(SO settingsObject)
{
_settingsObject = settingsObject;
}
public MauiSettingsGeneric(SO settingsObject, string key)
{
_settingsObject = settingsObject;
_passPhrase = key;
}
#endregion

#region Methods
Expand Down Expand Up @@ -253,17 +261,14 @@ public static void LoadDefaultSettings(object settings)

#region Conversion

public static async Task<Dictionary<string, Tuple<object, Type>>> ToDictionaryAsync()
{
return await ToDictionaryAsync(settings: SettingsObject);
}
public static Task<Dictionary<string, Tuple<object, Type>>> ToDictionaryAsync()
=> ToDictionaryAsync(settings: SettingsObject);

public static async Task<Dictionary<string, Tuple<object, Type>>> ToDictionaryAsync(object settings)
{
if (true)
{
Dictionary<string, Tuple<object, Type>> setting = new();
//List<MemberInfo> members = GetClassMetaAsList(settings);

Dictionary<string, Tuple<object, Type>> setting = [];
IEnumerable<MemberInfo> declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers;

MauiSettingsMemberInfo settingsObjectInfo = new();
Expand All @@ -275,7 +280,7 @@ public static async Task<Dictionary<string, Tuple<object, Type>>> 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<object, Type>(settingsPair.Value ?? settingsPair.Default, settingsPair.SettingsType));
}
Expand All @@ -296,10 +301,9 @@ public static async Task<Dictionary<string, Tuple<object, Type>>> ToDictionaryAs
}
}

public static async Task<ConcurrentDictionary<string, Tuple<object, Type>>> ToConcurrentDictionaryAsync()
{
return await ToConcurrentDictionaryAsync(settings: SettingsObject);
}
public static Task<ConcurrentDictionary<string, Tuple<object, Type>>> ToConcurrentDictionaryAsync()
=> ToConcurrentDictionaryAsync(settings: SettingsObject);

public static async Task<ConcurrentDictionary<string, Tuple<object, Type>>> ToConcurrentDictionaryAsync(object settings)
{
ConcurrentDictionary<string, Tuple<object, Type>> setting = new();
Expand All @@ -314,7 +318,7 @@ public static async Task<ConcurrentDictionary<string, Tuple<object, Type>>> 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<object, Type>(settingsPair.Value ?? settingsPair.Default, settingsPair.SettingsType));
}
Expand All @@ -334,11 +338,8 @@ public static async Task<ConcurrentDictionary<string, Tuple<object, Type>>> ToCo
return setting;
}

public static async Task<Tuple<string, Tuple<object, Type>>> ToSettingsTupleAsync<T>(Expression<Func<SO, T>> value)
{
return await ToSettingsTupleAsync(settings: SettingsObject, value: value);
}

public static Task<Tuple<string, Tuple<object, Type>>> ToSettingsTupleAsync<T>(Expression<Func<SO, T>> value) => ToSettingsTupleAsync(settings: SettingsObject, value: value);

public static async Task<Tuple<string, Tuple<object, Type>>> ToSettingsTupleAsync<T>(object settings, Expression<Func<SO, T>> value)
{
MauiSettingsInfo info = await GetExpressionMetaAsKeyValuePairAsync(settings: settings, value: value);
Expand Down Expand Up @@ -522,11 +523,15 @@ List<MauiSettingAttribute> 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)
Expand All @@ -552,7 +557,6 @@ List<MauiSettingAttribute> 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);
Expand Down Expand Up @@ -656,6 +660,10 @@ List<MauiSettingAttribute> 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.
Expand Down Expand Up @@ -773,7 +781,14 @@ List<MauiSettingAttribute> 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
{
Expand Down Expand Up @@ -817,7 +832,14 @@ List<MauiSettingAttribute> 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
{
Expand Down Expand Up @@ -852,7 +874,14 @@ List<MauiSettingAttribute> 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
{
Expand Down Expand Up @@ -894,14 +923,17 @@ List<MauiSettingAttribute> 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);
}
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.
Expand All @@ -912,6 +944,12 @@ List<MauiSettingAttribute> 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
{
Expand Down

0 comments on commit 65966f1

Please sign in to comment.