mirror of
https://github.com/bitwarden/server
synced 2025-12-24 20:23:21 +00:00
* ServerProtectedData for user entity * remove using statements * formatting * use data protection libs * no async * add data protection to ef user repo * switch to `SetApplicationName` per ASPNET docs * null checks * cleanup * value converter for EF * new line at eof * fix using * remove folder ref * restore ctor * fix lint * use global constant * UseApplicationServiceProvider for integration tests * implement constant for DatabaseFieldProtectedPrefix * Fix EF IntegrationTest * restore original values after protect and save * lint fixes * Use Constants Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using Bit.Core;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
namespace Bit.Infrastructure.EntityFramework.Converters;
|
|
public class DataProtectionConverter : ValueConverter<string, string>
|
|
{
|
|
public DataProtectionConverter(IDataProtector dataProtector) :
|
|
base(s => Protect(dataProtector, s), s => Unprotect(dataProtector, s))
|
|
{ }
|
|
|
|
private static string Protect(IDataProtector dataProtector, string value)
|
|
{
|
|
if (value?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? true)
|
|
{
|
|
return value;
|
|
}
|
|
|
|
return string.Concat(
|
|
Constants.DatabaseFieldProtectedPrefix, dataProtector.Protect(value));
|
|
}
|
|
|
|
private static string Unprotect(IDataProtector dataProtector, string value)
|
|
{
|
|
if (!value?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? true)
|
|
{
|
|
return value;
|
|
}
|
|
|
|
return dataProtector.Unprotect(
|
|
value.Substring(Constants.DatabaseFieldProtectedPrefix.Length));
|
|
}
|
|
}
|