From 6abc17c3bfc02ddb78cb2eea268c2af5c083cb69 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 27 Jan 2025 17:09:23 +0100 Subject: [PATCH] =?UTF-8?q?feat(HomeController):=20Aktualisiert,=20um=20SM?= =?UTF-8?q?S=20=C3=BCber=20zu=20senden.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Unnötige Parameter in SmsParams entfernt. - Code-Sendefunktion von IMessagingService entfernt. - GetTotpExpirationTime Methode im CodeGenerator entfernt. --- .../Configurations/GtxMessaging/SmsParams.cs | 9 ----- .../Contracts/ICodeGenerator.cs | 2 - .../Contracts/IMessagingService.cs | 2 - .../DTOs/Messaging/SmsResponse.cs | 2 + .../Extensions/DIExtensions.cs | 1 - .../Services/CodeGenerator.cs | 5 --- .../Services/GTXMessagingService.cs | 12 +----- .../Controllers/HomeController.cs | 39 +++++++++++++------ 8 files changed, 30 insertions(+), 42 deletions(-) diff --git a/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs b/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs index 9db3c8c5..eae4b63f 100644 --- a/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs +++ b/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs @@ -1,7 +1,4 @@ using DigitalData.Core.Abstractions.Client; -using Microsoft.Extensions.Caching.Distributed; -using OtpNet; - namespace EnvelopeGenerator.Application.Configurations.GtxMessaging { /// @@ -20,11 +17,5 @@ namespace EnvelopeGenerator.Application.Configurations.GtxMessaging public string RecipientQueryParamName { get; init; } = "to"; public string MessageQueryParamName { get; init; } = "text"; - - public int CodeLength { get; init; } = 5; - - public int SmsTotpStep { get; init; } = 300; - - public string DefaultTotpMessageFormat { get; init; } = "{0}"; } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs index c8d47976..d4e93c6b 100644 --- a/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs +++ b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs @@ -15,7 +15,5 @@ namespace EnvelopeGenerator.Application.Contracts string GenerateTotp(string secretKey, int step = 30); bool VerifyTotp(string totpCode, string secretKey, int step = 30, VerificationWindow? window = null); - - bool GetTotpExpirationTime(int step = 30); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/IMessagingService.cs b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs index 9a0c050a..886585a5 100644 --- a/EnvelopeGenerator.Application/Contracts/IMessagingService.cs +++ b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs @@ -7,6 +7,4 @@ public interface IMessagingService string ServiceProvider { get; } Task SendSmsAsync(string recipient, string message); - - Task SendSmsCodeAsync(string recipient, string secretKey, string messageFormat = "{0}"); } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs index a546d0c7..1d82020b 100644 --- a/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs +++ b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs @@ -4,6 +4,8 @@ { public required bool Ok { get; init; } + public bool Failed => !Ok; + public dynamic? Errors { get; init; } } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs index ae8fedf9..465b5b23 100644 --- a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs @@ -62,7 +62,6 @@ namespace EnvelopeGenerator.Application.Extensions services.AddHttpClientService(smsConfigSection); services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); services.TryAddSingleton(); return services; diff --git a/EnvelopeGenerator.Application/Services/CodeGenerator.cs b/EnvelopeGenerator.Application/Services/CodeGenerator.cs index 94ad6720..ca84bfc9 100644 --- a/EnvelopeGenerator.Application/Services/CodeGenerator.cs +++ b/EnvelopeGenerator.Application/Services/CodeGenerator.cs @@ -67,10 +67,5 @@ namespace EnvelopeGenerator.Application.Services public bool VerifyTotp(string totpCode, string secretKey, int step = 30, VerificationWindow? window = null) => new Totp(Base32Encoding.ToBytes(secretKey), step).VerifyTotp(totpCode, out _, window); - - public bool GetTotpExpirationTime(int step = 30) - { - throw new NotImplementedException(); - } } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs index 78269522..3d6e8620 100644 --- a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs +++ b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs @@ -16,17 +16,14 @@ public class GtxMessagingService : IMessagingService private readonly IMapper _mapper; - private readonly ICodeGenerator _codeGen; - public string ServiceProvider { get; } - public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper, ICodeGenerator codeGenerator) + public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper) { _smsClient = smsClient; _smsParams = smsParamsOptions.Value; _mapper = mapper; ServiceProvider = GetType().Name.Replace("Service", string.Empty); - _codeGen = codeGenerator; } public async Task SendSmsAsync(string recipient, string message) @@ -39,11 +36,4 @@ public class GtxMessagingService : IMessagingService .ThenAsync(res => res.Json()) .ThenAsync(_mapper.Map); } - - public async Task SendSmsCodeAsync(string recipient, string secretKey, string? messageFormat = null) - { - var code = _codeGen.GenerateTotp(secretKey, _smsParams.SmsTotpStep); - var message = string.Format(messageFormat ?? _smsParams.DefaultTotpMessageFormat, code); - return await SendSmsAsync(recipient: recipient, message: message); - } } \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/HomeController.cs b/EnvelopeGenerator.Web/Controllers/HomeController.cs index 6aec1ca6..9a6ecee2 100644 --- a/EnvelopeGenerator.Web/Controllers/HomeController.cs +++ b/EnvelopeGenerator.Web/Controllers/HomeController.cs @@ -20,6 +20,11 @@ using Newtonsoft.Json; using EnvelopeGenerator.Application.DTOs; using DigitalData.Core.Client; using EnvelopeGenerator.Application.Extensions; +using Microsoft.Extensions.Caching.Distributed; +using System.Globalization; +using EnvelopeGenerator.Application.Configurations.GtxMessaging; +using EnvelopeGenerator.Application.DTOs.Messaging; +using OtpNet; namespace EnvelopeGenerator.Web.Controllers { @@ -38,10 +43,13 @@ namespace EnvelopeGenerator.Web.Controllers private readonly IMessagingService _msgService; private readonly ICodeGenerator _codeGenerator; private readonly IReceiverService _rcvService; - private static readonly int SmsTotpStep = 60 * 3; - private static readonly string SmsFormat = "{0}"; + private readonly IDistributedCache _dCache; + private static readonly int SmsTotpStep = 60 * 1; + private static readonly string SmsFormat = "signFlow TFA-Passwort ist {0}. Dieses Passwort ist bis {1} Uhr gültig."; + private static readonly string SmsCodeExpirationCacheKeyFormat = "e{0}_r{1}_sms_code_expiration"; + private static readonly (string DateTimeFormat, CultureInfo CultureInfo) SmsCodeExpiration = ("HH:mm:ss", new CultureInfo("de-DE")); - public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, IMessagingService messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService) + public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, IMessagingService messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService, IDistributedCache distributedCache) { this.envelopeOldService = envelopeOldService; _envRcvService = envelopeReceiverService; @@ -56,6 +64,7 @@ namespace EnvelopeGenerator.Web.Controllers _msgService = messagingService; _codeGenerator = codeGenerator; _rcvService = receiverService; + _dCache = distributedCache; } [HttpGet("/")] @@ -197,17 +206,23 @@ namespace EnvelopeGenerator.Web.Controllers if (viaSms) { //add date time cache - var res = await _msgService.SendSmsCodeAsync(er_secret.PhoneNumber!, er_secret.Receiver!.TotpSecretkey!, SmsFormat); - if (res.Ok) - return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", _codeGenerator.GetTotpExpirationTime(SmsTotpStep)); - else if (!res.Allowed) - return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.AllowedAt); - else + var key = string.Format(SmsCodeExpirationCacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId); + var expiration = await _dCache.GetDateTimeAsync(key); + if(expiration is null || expiration <= DateTime.Now) { - var res_json = JsonConvert.SerializeObject(res); - _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}"); - return this.ViewInnerServiceError(); + var new_expiration = DateTime.Now.AddMinutes(SmsTotpStep); + var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, SmsTotpStep); + var msg = string.Format(SmsFormat, totp, new_expiration.ToString(SmsCodeExpiration.DateTimeFormat, SmsCodeExpiration.CultureInfo)); + var smsRes = await _msgService.SendSmsAsync(er_secret.PhoneNumber!, msg); + if (smsRes.Failed) + { + var res_json = JsonConvert.SerializeObject(smsRes); + _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}"); + return this.ViewInnerServiceError(); + } } + + return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", expiration); } else {