Refactor EmailAccountDto and improve encryption logic
Refactored `EmailAccountDto` to focus on SMTP-related properties, removing unused fields. Updated `DependencyInjection` to configure data protection with a new key storage path. Simplified `DataProtectionEncryptionService` by removing redundant checks and error handling for encryption and decryption methods.
This commit is contained in:
@@ -5,19 +5,15 @@ namespace DigitalData.EmailProfiler.Application.Common.Dtos;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class EmailAccountDto
|
public class EmailAccountDto
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public string Username { get; set; } = null!;
|
||||||
public string AccountName { get; set; } = string.Empty;
|
|
||||||
public string ImapServer { get; set; } = string.Empty;
|
|
||||||
public int ImapPort { get; set; }
|
|
||||||
public bool ImapUseSsl { get; set; }
|
|
||||||
public string SmtpServer { get; set; } = string.Empty;
|
|
||||||
public int SmtpPort { get; set; }
|
|
||||||
public bool SmtpUseSsl { get; set; }
|
|
||||||
public string Username { get; set; } = string.Empty;
|
|
||||||
public string? EncryptedPassword { get; set; }
|
public string? EncryptedPassword { get; set; }
|
||||||
|
|
||||||
|
public string SmtpServer { get; set; } = null!;
|
||||||
|
|
||||||
|
public int SmtpPort { get; set; }
|
||||||
|
|
||||||
|
public bool SmtpUseSsl { get; set; }
|
||||||
|
|
||||||
public bool UseOAuth2 { get; set; }
|
public bool UseOAuth2 { get; set; }
|
||||||
public string? TenantId { get; set; }
|
|
||||||
public string? ClientId { get; set; }
|
|
||||||
public string? EncryptedClientSecret { get; set; }
|
|
||||||
public bool IsActive { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
|||||||
using DigitalData.EmailProfiler.Infrastructure.Messaging;
|
using DigitalData.EmailProfiler.Infrastructure.Messaging;
|
||||||
using DigitalData.EmailProfiler.Infrastructure.Queue;
|
using DigitalData.EmailProfiler.Infrastructure.Queue;
|
||||||
using DigitalData.EmailProfiler.Infrastructure.Services;
|
using DigitalData.EmailProfiler.Infrastructure.Services;
|
||||||
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
@@ -43,9 +44,9 @@ public static class DependencyInjection
|
|||||||
services.AddHostedService<RabbitMqCommandConsumer>();
|
services.AddHostedService<RabbitMqCommandConsumer>();
|
||||||
|
|
||||||
// --- Data Protection (for encryption) ---
|
// --- Data Protection (for encryption) ---
|
||||||
services.AddDataProtection();
|
services.AddDataProtection()
|
||||||
// .PersistKeysToFileSystem(new DirectoryInfo(@"C:\ProgramData\EmailProfiler\Keys"))
|
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\ProgramData\EmailService\Keys"))
|
||||||
// .SetApplicationName("EmailProfiler");
|
.SetApplicationName("EmailProfiler");
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,25 +13,11 @@ public class DataProtectionEncryptionService(IDataProtectionProvider Provider) :
|
|||||||
|
|
||||||
public string Encrypt(string plainText)
|
public string Encrypt(string plainText)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(plainText))
|
|
||||||
return string.Empty;
|
|
||||||
|
|
||||||
return Protector.Protect(plainText);
|
return Protector.Protect(plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Decrypt(string cipherText)
|
public string Decrypt(string cipherText)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(cipherText))
|
return Protector.Unprotect(cipherText);
|
||||||
return string.Empty;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Protector.Unprotect(cipherText);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// If decryption fails, return empty (corrupt data or wrong key)
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user