Skip to content

Commit

Permalink
Add Event for EncryptionErrors
Browse files Browse the repository at this point in the history
This commit adds `trycatch` to the encryption stuff and invokes an event if such a error happens.
  • Loading branch information
AndreasReitberger committed Apr 3, 2024
1 parent 2e35cd1 commit 662d0e5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/MauiSettings/Enums/MauiSettingsResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace AndreasReitberger.Maui.Enums
{
public enum MauiSettingsResults
{
Success,
Skipped,
EncryptionError,
Failed,
}
}
8 changes: 8 additions & 0 deletions src/MauiSettings/Events/EncryptionErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AndreasReitberger.Maui.Events
{
public partial class EncryptionErrorEventArgs : EventArgs
{
public Exception? Exception { get; set; }
public string Key { get; set; } = string.Empty;
}
}
123 changes: 105 additions & 18 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AndreasReitberger.Maui.Cloud;
#endif
using AndreasReitberger.Maui.Enums;
using AndreasReitberger.Maui.Events;
using AndreasReitberger.Maui.Helper;
using AndreasReitberger.Maui.Utilities;
using AndreasReitberger.Shared.Core.Utilities;
Expand Down Expand Up @@ -68,7 +69,7 @@ public MauiSettingsGeneric(SO settingsObject, string settingsKey)

public static Task LoadSettingAsync<T>(Expression<Func<SO, T>> value, string? key = null) => Task.Run(async delegate
{
await LoadObjectSettingAsync(SettingsObject, value, key: key);
await LoadObjectSettingAsync(SettingsObject, value, key: key);
});

public static Task LoadSecureSettingAsync<T>(Expression<Func<SO, T>> value, string? key = null) => Task.Run(async delegate
Expand Down Expand Up @@ -336,7 +337,8 @@ static async Task GetClassMetaAsync(object settings, MauiSettingsActions mode, M
settingsObjectInfo.OrignalSettingsObject = settings;
settingsObjectInfo.Info = mInfo;
// Handles saving the settings to the Maui.Storage.Preferences
_ = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, key: key);
MauiSettingsResults result = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, key: key);
if (result == MauiSettingsResults.EncryptionError || result == MauiSettingsResults.Failed) { break; }
}
}
static async Task GetMetaFromDictionaryAsync(object settings, Dictionary<string, Tuple<object, Type>> dictionary, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null)
Expand Down Expand Up @@ -371,8 +373,9 @@ static async Task GetMetaFromDictionaryAsync(object settings, Dictionary<string,
settingsObjectInfo.OrignalSettingsObject = settings;
settingsObjectInfo.Info = mInfo;
// Handles saving the settings to the Maui.Storage.Preferences
_ = await ProcessSettingsInfoAsync(
MauiSettingsResults result = await ProcessSettingsInfoAsync(
settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, useValueFromSettingsInfo: useValueFromSettingsInfo, key: key);
if (result == MauiSettingsResults.EncryptionError || result == MauiSettingsResults.Failed) { break; }
}
}

Expand Down Expand Up @@ -574,7 +577,7 @@ List<MauiSettingAttribute> settingBaseAttributes
return true;
}

static async Task<bool> ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool secureOnly = false, bool useValueFromSettingsInfo = false, string? key = null, bool keepEncrypted = false)
static async Task<MauiSettingsResults> ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool secureOnly = false, bool useValueFromSettingsInfo = false, string? key = null, bool keepEncrypted = false)
{
settingsInfo ??= new();
MauiSettingBaseAttribute? settingBaseAttribute = null;
Expand All @@ -586,7 +589,7 @@ List<MauiSettingAttribute> settingBaseAttributes
if (settingBaseAttributes?.Count == 0)
{
// If the member has not the needed MauiSettingsAttribute, continue with the search
return false;
return MauiSettingsResults.Skipped;
}
settingBaseAttribute = settingBaseAttributes?.FirstOrDefault();
settingsInfo.Name = MauiSettingNameFormater.GetFullSettingName(settingsObjectInfo.OrignalSettingsObject.GetType(), settingsObjectInfo.Info, settingBaseAttribute);
Expand All @@ -605,7 +608,7 @@ List<MauiSettingAttribute> settingBaseAttributes
{
// If only secure storage should be loaded, stop here.
if (secureOnly)
return true;
return MauiSettingsResults.Skipped;
// If the value is not used from the passed settingsInfo, load it

switch (target)
Expand Down Expand Up @@ -673,8 +676,23 @@ List<MauiSettingAttribute> settingBaseAttributes
if (settingsInfo.Value is string secureString)
{
// Decrypt string
string decryptedString = EncryptionManager.DecryptStringFromBase64String(secureString, key);
MauiSettingsObjectHelper.SetSettingValue(settingsObjectInfo.Info, settingsObjectInfo.OrignalSettingsObject, decryptedString, settingsInfo.SettingsType);
try
{
string decryptedString = EncryptionManager.DecryptStringFromBase64String(secureString, key);
// Throw on key missmatch
if (string.IsNullOrEmpty(decryptedString) && !string.IsNullOrEmpty(secureString))
throw new Exception($"The secure string is not empty, but the decrypted string is. This indicates a key missmatch!");
MauiSettingsObjectHelper.SetSettingValue(settingsObjectInfo.Info, settingsObjectInfo.OrignalSettingsObject, decryptedString, settingsInfo.SettingsType);
}
catch(Exception ex)
{
OnEncryptionErrorEvent(new()
{
Exception = ex,
Key = key,
});
return MauiSettingsResults.EncryptionError;
}
break;
}
}
Expand Down Expand Up @@ -702,8 +720,20 @@ List<MauiSettingAttribute> settingBaseAttributes
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
// Encrypt string
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
try
{
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
}
catch (Exception ex)
{
OnEncryptionErrorEvent(new()
{
Exception = ex,
Key = key,
});
return MauiSettingsResults.EncryptionError;
}
}
else
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString);
Expand Down Expand Up @@ -752,9 +782,21 @@ List<MauiSettingAttribute> settingBaseAttributes
{
if (settingsInfo.Encrypt && !string.IsNullOrEmpty(key))
{
// Decrypt string
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
// Encrypt string
try
{
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
}
catch (Exception ex)
{
OnEncryptionErrorEvent(new()
{
Exception = ex,
Key = key,
});
return MauiSettingsResults.EncryptionError;
}
}
else
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString);
Expand Down Expand Up @@ -795,8 +837,20 @@ List<MauiSettingAttribute> settingBaseAttributes
if (settingsInfo.Encrypt && !string.IsNullOrEmpty(key))
{
// Decrypt string
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
try
{
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
}
catch (Exception ex)
{
OnEncryptionErrorEvent(new()
{
Exception = ex,
Key = key,
});
return MauiSettingsResults.EncryptionError;
}
}
else
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, secureString);
Expand All @@ -816,7 +870,7 @@ List<MauiSettingAttribute> settingBaseAttributes
default:
break;
}
return true;
return MauiSettingsResults.Success;
}

static async Task<MauiSettingsInfo?> ProcessSettingsInfoAsKeyValuePairAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, bool secureOnly = false, string? key = null, bool keeyEncrypted = false)
Expand Down Expand Up @@ -860,8 +914,26 @@ List<MauiSettingAttribute> settingBaseAttributes
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
// Decrypt string
string decryptedString = EncryptionManager.DecryptStringFromBase64String(settingsInfo.Value as string, key);
settingsInfo.Value = decryptedString;
if (settingsInfo.Value is string secureString)
{
try
{
string decryptedString = EncryptionManager.DecryptStringFromBase64String(secureString, key);
// Throw on key missmatch
if (string.IsNullOrEmpty(decryptedString) && !string.IsNullOrEmpty(secureString))
throw new Exception($"The secure string is not empty, but the decrypted string is. This indicates a key missmatch!");
settingsInfo.Value = decryptedString;
}
catch (Exception ex)
{
OnEncryptionErrorEvent(new()
{
Exception = ex,
Key = key,
});
return null;
}
}
}
}
else
Expand All @@ -875,5 +947,20 @@ List<MauiSettingAttribute> settingBaseAttributes
#endregion

#endregion

#region Events

public static event EventHandler? ErrorEvent;
protected static void OnErrorEvent(ErrorEventArgs e)
{
ErrorEvent?.Invoke(typeof(MauiSettingsGeneric<SO>), e);
}

public static event EventHandler? EncryptionErrorEvent;
protected static void OnEncryptionErrorEvent(EncryptionErrorEventArgs e)
{
EncryptionErrorEvent?.Invoke(typeof(MauiSettingsGeneric<SO>), e);
}
#endregion
}
}

0 comments on commit 662d0e5

Please sign in to comment.