- Add CancellationTokenSource timeout to DB migration loop (5 min) - Make seed admin password configurable via env/config - Improve ExceptionFilter with typed exception handling (409/499/500) - Document Hangfire.PostgreSql CS0618 pragma with upgrade link - Narrow CredentialProtector fallback catch to non-empty ciphertext - Fix ApiBaseUrl in appsettings.json (:5000 -> :8080) for Docker consistency - Improve HTTPS reverse proxy comment in middleware config Build: 0 errors, 0 warnings | Tests: 799/799 passed
45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System.Text;
|
|
using Heimdall.Application.Common;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
namespace Heimdall.Infrastructure.Security;
|
|
|
|
public sealed class DataProtectorCredentialProtector : ICredentialProtector
|
|
{
|
|
private readonly IDataProtector _protector;
|
|
|
|
public DataProtectorCredentialProtector(IDataProtectionProvider provider)
|
|
{
|
|
_protector = provider.CreateProtector("Heimdall.CameraCredentials.v1");
|
|
}
|
|
|
|
public string Encrypt(string plaintext)
|
|
{
|
|
if (string.IsNullOrEmpty(plaintext))
|
|
return string.Empty;
|
|
|
|
var protectedBytes = _protector.Protect(Encoding.UTF8.GetBytes(plaintext));
|
|
return Convert.ToBase64String(protectedBytes);
|
|
}
|
|
|
|
public string Decrypt(string ciphertext)
|
|
{
|
|
if (string.IsNullOrEmpty(ciphertext))
|
|
return string.Empty;
|
|
|
|
try
|
|
{
|
|
var protectedBytes = Convert.FromBase64String(ciphertext);
|
|
var unprotectedBytes = _protector.Unprotect(protectedBytes);
|
|
return Encoding.UTF8.GetString(unprotectedBytes);
|
|
}
|
|
catch (Exception) when (!string.IsNullOrEmpty(ciphertext))
|
|
{
|
|
// If data is not encrypted (plaintext fallback for migration),
|
|
// return as-is. This prevents data loss if the protector is
|
|
// added to a database that already has plaintext values.
|
|
return ciphertext;
|
|
}
|
|
}
|
|
}
|