diff --git a/src/Heimdall.Application/Services/ICameraService.cs b/src/Heimdall.Application/Services/ICameraService.cs index 8de8822..17f3992 100644 --- a/src/Heimdall.Application/Services/ICameraService.cs +++ b/src/Heimdall.Application/Services/ICameraService.cs @@ -10,4 +10,5 @@ public interface ICameraService Task>> ListBySiteAsync(Guid siteId, int page, int pageSize, CancellationToken ct); Task> UpdateAsync(Guid id, UpdateCameraRequest request, CancellationToken ct); Task DeleteAsync(Guid id, CancellationToken ct); + Task SetCredentialsAsync(Guid cameraId, Domain.Entities.CameraCredentials credentials, CancellationToken ct); } diff --git a/src/Heimdall.Domain/Entities/CameraCredentials.cs b/src/Heimdall.Domain/Entities/CameraCredentials.cs index 977b7fa..3a58f6d 100644 --- a/src/Heimdall.Domain/Entities/CameraCredentials.cs +++ b/src/Heimdall.Domain/Entities/CameraCredentials.cs @@ -1,21 +1,22 @@ namespace Heimdall.Domain.Entities; /// -/// Encrypted ONVIF/RTSP credentials for a camera. -/// All sensitive fields are encrypted at rest via IDataProtector. +/// ONVIF/RTSP credentials for a camera. +/// Sensitive fields are encrypted at rest via +/// applied in the service layer before storage. /// public class CameraCredentials { public Guid CameraId { get; private set; } public string OnvifUsername { get; private set; } = "admin"; - /// Encrypted at rest via IDataProtector (applied at service layer). + /// Encrypted via ICredentialProtector at the service layer. public string OnvifPassword { get; private set; } = string.Empty; - /// Encrypted at rest via IDataProtector (applied at service layer). + /// Encrypted via ICredentialProtector at the service layer. public string RtspUsername { get; private set; } = string.Empty; - /// Encrypted at rest via IDataProtector (applied at service layer). + /// Encrypted via ICredentialProtector at the service layer. public string RtspPassword { get; private set; } = string.Empty; public DateTime CreatedAt { get; private set; } = DateTime.UtcNow; diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 4a073b1..ab4f905 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -51,9 +51,15 @@ public static class DependencyInjection .AddEntityFrameworkStores() .AddDefaultTokenProviders(); + // Data Protection (for camera credential encryption) + services.AddDataProtection(); + // JWT services.AddScoped(); + // Credential encryption + services.AddScoped(); + // Application services services.AddScoped(); services.AddScoped(); diff --git a/src/Heimdall.Infrastructure/Security/CredentialProtector.cs b/src/Heimdall.Infrastructure/Security/CredentialProtector.cs new file mode 100644 index 0000000..769ff03 --- /dev/null +++ b/src/Heimdall.Infrastructure/Security/CredentialProtector.cs @@ -0,0 +1,53 @@ +using System.Text; +using Microsoft.AspNetCore.DataProtection; + +namespace Heimdall.Infrastructure.Security; + +/// +/// 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. +/// +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; + } + } +} diff --git a/src/Heimdall.Infrastructure/Services/CameraService.cs b/src/Heimdall.Infrastructure/Services/CameraService.cs index 53d1d34..97248fa 100644 --- a/src/Heimdall.Infrastructure/Services/CameraService.cs +++ b/src/Heimdall.Infrastructure/Services/CameraService.cs @@ -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> CreateAsync(CreateCameraRequest request, CancellationToken ct) { @@ -78,6 +84,27 @@ public class CameraService : ICameraService return ServiceResult.Success(); } + /// + /// Sets encrypted camera credentials. The OnvifPassword, RtspUsername, and RtspPassword + /// are encrypted via before storage. + /// + public async Task 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);