Skip to content

Commit

Permalink
Throw Exception if key is missing and Encrypt is set
Browse files Browse the repository at this point in the history
Fixed #37
  • Loading branch information
AndreasReitberger committed Mar 23, 2024
1 parent ea81ace commit 1ccfe9b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ public static async Task<Tuple<string, Tuple<object, Type>>> ToSettingsTupleAsyn
}
#endregion

#region MyRegion
#region Encryption

public static Task ExhangeKeyAsync(string? newKey = null, string? oldKey = null)
public static Task ExhangeKeyAsync(string newKey, string? oldKey = null)
=> Task.Run(async delegate
{
await LoadSecureSettingsAsync(key: oldKey);
Expand Down Expand Up @@ -326,7 +326,7 @@ static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettings
settingsObjectInfo.OrignalSettingsObject = settings;
settingsObjectInfo.Info = mInfo;
// Handles saving the settings to the Maui.Storage.Preferences
_ = ProcessSettingsInfo(settingsObjectInfo, settingsInfo, mode, target, key: key);
_ = ProcessSettingsInfo(settingsObjectInfo, settingsInfo, mode, target);
}
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ static void GetExpressionMeta<T>(object settings, Expression<Func<SO, T>> value,
OrignalSettingsObject = settings,
Info = memberExpression.Member,

}, new MauiSettingsInfo(), mode, target, key: null);
}, new MauiSettingsInfo(), mode, target);
}
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ static async Task GetExpressionMetaAsync<T>(object settings, Expression<Func<SO,
return new();
}

static bool ProcessSettingsInfo(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool throwOnError = false, string? key = null)
static bool ProcessSettingsInfo(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool throwOnError = false)
{
settingsInfo ??= new();
MauiSettingBaseAttribute? settingBaseAttribute = null;
Expand Down Expand Up @@ -506,7 +506,6 @@ List<MauiSettingAttribute> settingBaseAttributes
if (throwOnError) throw new NotSupportedException("Encryption is only available on the Async methods and if the property is marked as 'Secure'");
}
}

switch (mode)
{
case MauiSettingsActions.Load:
Expand Down Expand Up @@ -605,9 +604,6 @@ List<MauiSettingAttribute> settingBaseAttributes
return false;
}
settingBaseAttribute = settingBaseAttributes?.FirstOrDefault();
//}
//if (settingsObjectInfo.Info is not null)
//{
settingsInfo.Name = MauiSettingNameFormater.GetFullSettingName(settingsObjectInfo.OrignalSettingsObject.GetType(), settingsObjectInfo.Info, settingBaseAttribute);
settingsInfo.SettingsType = (settingsInfo.SettingsType = MauiSettingsObjectHelper.GetSettingType(settingsObjectInfo.Info));
settingsInfo.Default = MauiSettingsObjectHelper.GetDefaultValue(settingBaseAttribute, settingsInfo.SettingsType);
Expand Down Expand Up @@ -678,16 +674,17 @@ List<MauiSettingAttribute> settingBaseAttributes
#endif
}
}

switch (mode)
{
case MauiSettingsActions.Load:
if (settingBaseAttribute?.DefaultValueInUse ?? false)
{
object defaultValue = MauiSettingsObjectHelper.GetDefaultValue(settingBaseAttribute, settingsInfo.SettingsType);
}
if (secure && settingsInfo.Encrypt && !string.IsNullOrEmpty(key) && !keepEncrypted)
if (secure && settingsInfo.Encrypt && !keepEncrypted)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (settingsInfo.Value is string secureString)
{
// Decrypt string
Expand Down Expand Up @@ -715,8 +712,10 @@ List<MauiSettingAttribute> settingBaseAttributes
{
if (settingsInfo.Value is string secureString)
{
if (settingsInfo.Encrypt && !string.IsNullOrEmpty(key))
if (settingsInfo.Encrypt)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
// Encrypt string
string encryptedString = EncryptionManager.EncryptStringToBase64String(secureString, key);
await MauiSettingsHelper.SetSecureSettingsValueAsync(settingsInfo.Name, encryptedString);
Expand Down Expand Up @@ -874,8 +873,10 @@ 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(key) && !keeyEncrypted)
if (settingsInfo.Encrypt && !keeyEncrypted)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
// Decrypt string
string decryptedString = EncryptionManager.DecryptStringFromBase64String(settingsInfo.Value as string, key);
settingsInfo.Value = decryptedString;
Expand Down

0 comments on commit 1ccfe9b

Please sign in to comment.