From b11f32bd3ce60367a13a299815b71ac42fa014a8 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 29 Nov 2024 11:08:01 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20CodeGenerator-Service=20mit=20Konfigura?= =?UTF-8?q?tionsunterst=C3=BCtzung=20implementiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CodeGenerator-Service erstellt, der zufällige Codes basierend auf einem konfigurierbaren Zeichensatz generiert. - IOptions für DI-Injektion der Konfigurationseinstellungen integriert. - Lazy-Initialisierung für statische Instanz des CodeGenerators hinzugefügt. - Validierung hinzugefügt, um sicherzustellen, dass die Code-Länge größer als null ist. - Geplante zukünftige Verbesserung: Random als Singleton injizieren, um die Multithreading-Performance zu verbessern. --- .../Configurations/CodeGeneratorConfig.cs | 7 ++++ .../Contracts/ICodeGenerator.cs | 7 ++++ EnvelopeGenerator.Application/DIExtensions.cs | 7 +++- .../Services/CodeGenerator.cs | 37 +++++++++++++++++++ .../Controllers/HomeController.cs | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 EnvelopeGenerator.Application/Configurations/CodeGeneratorConfig.cs create mode 100644 EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs create mode 100644 EnvelopeGenerator.Application/Services/CodeGenerator.cs diff --git a/EnvelopeGenerator.Application/Configurations/CodeGeneratorConfig.cs b/EnvelopeGenerator.Application/Configurations/CodeGeneratorConfig.cs new file mode 100644 index 00000000..d321401e --- /dev/null +++ b/EnvelopeGenerator.Application/Configurations/CodeGeneratorConfig.cs @@ -0,0 +1,7 @@ +namespace EnvelopeGenerator.Application.Configurations +{ + public class CodeGeneratorConfig + { + public string CharPool { get; init; } = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs new file mode 100644 index 00000000..4a282c66 --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs @@ -0,0 +1,7 @@ +namespace EnvelopeGenerator.Application.Contracts +{ + public interface ICodeGenerator + { + string GenerateCode(int length); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/DIExtensions.cs b/EnvelopeGenerator.Application/DIExtensions.cs index ecbef1aa..c8dd5eb2 100644 --- a/EnvelopeGenerator.Application/DIExtensions.cs +++ b/EnvelopeGenerator.Application/DIExtensions.cs @@ -15,7 +15,7 @@ namespace EnvelopeGenerator.Application { public static class DIExtensions { - public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfigurationSection dispatcherConfigSection, IConfigurationSection mailConfigSection, IConfigurationSection smsConfigSection) + public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfigurationSection dispatcherConfigSection, IConfigurationSection mailConfigSection, IConfigurationSection smsConfigSection, IConfigurationSection codeGeneratorConfigSection) { //Inject CRUD Service and repositoriesad services.TryAddScoped(); @@ -48,6 +48,7 @@ namespace EnvelopeGenerator.Application services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); + services.TryAddScoped(); //Auto mapping profiles services.AddAutoMapper(typeof(BasicDtoMappingProfile).Assembly); @@ -55,6 +56,7 @@ namespace EnvelopeGenerator.Application services.Configure(dispatcherConfigSection); services.Configure(mailConfigSection); + services.Configure(codeGeneratorConfigSection); services.AddHttpClientService(smsConfigSection); services.TryAddSingleton(); @@ -65,6 +67,7 @@ namespace EnvelopeGenerator.Application public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration config) => services.AddEnvelopeGenerator( dispatcherConfigSection: config.GetSection("DispatcherConfig"), mailConfigSection: config.GetSection("MailConfig"), - smsConfigSection: config.GetSection("SmsConfig")); + smsConfigSection: config.GetSection("SmsConfig"), + codeGeneratorConfigSection: config.GetSection("CodeGeneratorConfig")); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/CodeGenerator.cs b/EnvelopeGenerator.Application/Services/CodeGenerator.cs new file mode 100644 index 00000000..c09eaf34 --- /dev/null +++ b/EnvelopeGenerator.Application/Services/CodeGenerator.cs @@ -0,0 +1,37 @@ +using EnvelopeGenerator.Application.Configurations; +using EnvelopeGenerator.Application.Contracts; +using Microsoft.Extensions.Options; +using System.Text; + +namespace EnvelopeGenerator.Application.Services +{ + public class CodeGenerator : ICodeGenerator + { + public static Lazy LazyStatic => new(() => new CodeGenerator(Options.Create(new()))); + + public static CodeGenerator Static => LazyStatic.Value; + + private readonly string _charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + public CodeGenerator(IOptions options) + { + _charPool = options.Value.CharPool; + } + + public string GenerateCode(int length) + { + //TODO: Inject Random as a singleton to support multithreading to improve performance. + Random random = new(); + + if (length <= 0) + throw new ArgumentException("Password length must be greater than 0."); + + var passwordBuilder = new StringBuilder(length); + + for (int i = 0; i < length; i++) + passwordBuilder.Append(_charPool[random.Next(_charPool.Length)]); + + return passwordBuilder.ToString(); + } + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/HomeController.cs b/EnvelopeGenerator.Web/Controllers/HomeController.cs index b245a210..06db6647 100644 --- a/EnvelopeGenerator.Web/Controllers/HomeController.cs +++ b/EnvelopeGenerator.Web/Controllers/HomeController.cs @@ -192,6 +192,7 @@ namespace EnvelopeGenerator.Web.Controllers return View("EnvelopeLocked").WithData("ViaSms", true); } + //continue the process without important data to minimize security errors. var er = er_secret.WithoutSecrets; ViewData["EnvelopeKey"] = envelopeReceiverId;