Browse Source
Merge pull request #21744 from abpframework/secretvalues
Add an option to return value if decrypt failed
pull/21752/head
maliming
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
18 additions and
1 deletions
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs
@ -11,10 +11,17 @@ public class AbpSettingOptions
public HashSet < string > DeletedSettings { get ; }
/// <summary>
/// Default: true.
/// This is useful when you change <see cref="SettingDefinition.IsEncrypted"/> of an existing setting definition to true and don't want to lose the original value.
/// </summary>
public bool ReturnOriginalValueIfDecryptFailed { get ; set ; }
public AbpSettingOptions ( )
{
DefinitionProviders = new TypeList < ISettingDefinitionProvider > ( ) ;
ValueProviders = new TypeList < ISettingValueProvider > ( ) ;
DeletedSettings = new HashSet < string > ( ) ;
ReturnOriginalValueIfDecryptFailed = true ;
}
}
@ -1,6 +1,7 @@
using System ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging.Abstractions ;
using Microsoft.Extensions.Options ;
using Volo.Abp.DependencyInjection ;
using Volo.Abp.Security.Encryption ;
@ -10,10 +11,12 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep
{
protected IStringEncryptionService StringEncryptionService { get ; }
public ILogger < SettingEncryptionService > Logger { get ; set ; }
protected IOptions < AbpSettingOptions > Options { get ; }
public SettingEncryptionService ( IStringEncryptionService stringEncryptionService )
public SettingEncryptionService ( IStringEncryptionService stringEncryptionService , IOptions < AbpSettingOptions > options )
{
StringEncryptionService = stringEncryptionService ;
Options = options ;
Logger = NullLogger < SettingEncryptionService > . Instance ;
}
@ -40,7 +43,14 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep
}
catch ( Exception e )
{
if ( Options . Value . ReturnOriginalValueIfDecryptFailed )
{
Logger . LogWarning ( e , "Failed to decrypt the setting: {0}. Returning the original value..." , settingDefinition . Name ) ;
return encryptedValue ;
}
Logger . LogException ( e ) ;
return string . Empty ;
}
}