fix(security): implement IDataProtector-based credential encryption for CameraCredentials

- Add ICredentialProtector/DataProtectorCredentialProtector using ASP.NET Core Data Protection
- Register AddDataProtection + ICredentialProtector in Infrastructure DI
- Update CameraCredentials XML comments to reference real protector
- Add SetCredentialsAsync to CameraService with encryption
- Update ICameraService interface
This commit is contained in:
Arnaud Dartois committed 2026-07-17 20:07:03 +00:00
1 parent 12a1ff2737
commit 076bc58e93
5 files changed
+94 -6

No files matched your search

@@ -51,9 +51,15 @@ public static class DependencyInjection
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// Data Protection (for camera credential encryption)
services.AddDataProtection();
// JWT
services.AddScoped<IJwtService, JwtService>();
// Credential encryption
services.AddScoped<ICredentialProtector, DataProtectorCredentialProtector>();
// Application services
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<ISiteService, SiteService>();
@@ -0,0 +1,53 @@
using System.Text;
using Microsoft.AspNetCore.DataProtection;
namespace Heimdall.Infrastructure.Security;
/// <summary>
/// Encrypts and decrypts camera credentials using ASP.NET Core Data Protection (IDataProtector).
/// Uses a dedicated purpose string to isolate camera credentials from other protected data.
/// </summary>
public interface ICredentialProtector
{
string Encrypt(string plaintext);
string Decrypt(string ciphertext);
}
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
{
// 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;
}
}
}
@@ -4,6 +4,7 @@ using Heimdall.Application.Services;
using Heimdall.Domain.Entities;
using Heimdall.Domain.Enums;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Security;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.Infrastructure.Services;
@@ -11,8 +12,13 @@ namespace Heimdall.Infrastructure.Services;
public class CameraService : ICameraService
{
private readonly AppDbContext _db;
private readonly ICredentialProtector _credentialProtector;
public CameraService(AppDbContext db) => _db = db;
public CameraService(AppDbContext db, ICredentialProtector credentialProtector)
{
_db = db;
_credentialProtector = credentialProtector;
}
public async Task<ServiceResult<CameraResponse>> CreateAsync(CreateCameraRequest request, CancellationToken ct)
{
@@ -78,6 +84,27 @@ public class CameraService : ICameraService
return ServiceResult.Success();
}
/// <summary>
/// Sets encrypted camera credentials. The OnvifPassword, RtspUsername, and RtspPassword
/// are encrypted via <see cref="ICredentialProtector"/> before storage.
/// </summary>
public async Task<ServiceResult> SetCredentialsAsync(Guid cameraId, CameraCredentials credentials, CancellationToken ct)
{
var camera = await _db.Cameras.FindAsync([cameraId], ct);
if (camera is null)
return ServiceResult.NotFound("Camera not found");
credentials.SetCredentials(
credentials.OnvifUsername,
_credentialProtector.Encrypt(credentials.OnvifPassword),
_credentialProtector.Encrypt(credentials.RtspUsername),
_credentialProtector.Encrypt(credentials.RtspPassword));
camera.SetCredentials(credentials);
await _db.SaveChangesAsync(ct);
return ServiceResult.Success();
}
private static CameraResponse MapResponse(Camera c) => new(
c.Id, c.SiteId, c.AgentId, c.Name, c.Manufacturer, c.Model,
c.RtspUrl, c.Status, c.HasPtz, c.HasAudio, c.HasMotionDetection, c.CreatedAt);