feat(EnvelopeSmsService): SendTotpAsync-Methode hinzufügen, um totp unter Berücksichtigung der Ablaufzeit zu senden.

This commit is contained in:
Developer 02 2025-01-31 14:59:39 +01:00
parent aa918d875d
commit 772d510705
6 changed files with 76 additions and 41 deletions

View File

@ -0,0 +1,15 @@
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
using EnvelopeGenerator.Application.DTOs.Messaging;
namespace EnvelopeGenerator.Application.Contracts;
public interface IEnvelopeSmsHandler
{
/// <summary>
/// If expiration is passed then, sends sms and returns smsResponse and up-to-date expiration; otherwise send expiration.
/// </summary>
/// <param name="er_secret"></param>
/// <param name="cToken"></param>
/// <returns></returns>
Task<(SmsResponse? SmsResponse, DateTime Expiration)> SendTotpAsync(EnvelopeReceiverSecretDto er_secret, CancellationToken cToken = default);
}

View File

@ -1,5 +0,0 @@
namespace EnvelopeGenerator.Application.Contracts;
public interface IEnvelopeSmsService
{
}

View File

@ -60,7 +60,7 @@ namespace EnvelopeGenerator.Application.Extensions
services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams)));
services.TryAddSingleton<ISmsSender, GTXSmsSender>();
services.TryAddSingleton<IEnvelopeSmsService, EnvelopeSmsService>();
services.TryAddSingleton<IEnvelopeSmsHandler, EnvelopeSmsHandler>();
services.TryAddSingleton<ICodeGenerator, CodeGenerator>();
services.TryAddSingleton<QRCodeGenerator>();

View File

@ -0,0 +1,50 @@
using EnvelopeGenerator.Application.Configurations;
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
using EnvelopeGenerator.Application.DTOs.Messaging;
using EnvelopeGenerator.Application.Extensions;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Application.Services;
public class EnvelopeSmsHandler : IEnvelopeSmsHandler
{
private readonly ISmsSender _sender;
private readonly TotpSmsParams _totpSmsParams;
private readonly IDistributedCache _dCache;
private readonly ICodeGenerator _codeGenerator;
public EnvelopeSmsHandler(ISmsSender sender, IOptions<TotpSmsParams> totpSmsParamsOptions, IDistributedCache distributedCache, ICodeGenerator codeGenerator)
{
_sender = sender;
_totpSmsParams = totpSmsParamsOptions.Value;
_dCache = distributedCache;
_codeGenerator = codeGenerator;
}
/// <summary>
/// If expiration is passed then, sends sms and returns smsResponse and up-to-date expiration; otherwise send expiration.
/// </summary>
/// <param name="er_secret"></param>
/// <param name="cToken"></param>
/// <returns></returns>
public async Task<(SmsResponse? SmsResponse, DateTime Expiration)> SendTotpAsync(EnvelopeReceiverSecretDto er_secret, CancellationToken cToken = default)
{
var key = string.Format(_totpSmsParams.Expiration.CacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId);
var expiration = await _dCache.GetDateTimeAsync(key, cToken);
if(expiration is DateTime expirationDateTime && expirationDateTime < DateTime.Now)
return (null, expirationDateTime);
else
{
var new_expiration = DateTime.Now.AddSeconds(_totpSmsParams.TotpStep);
var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
return (await _sender.SendSmsAsync(er_secret.PhoneNumber!, msg), new_expiration);
}
}
}

View File

@ -1,18 +0,0 @@
using EnvelopeGenerator.Application.Configurations;
using EnvelopeGenerator.Application.Contracts;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Application.Services;
public class EnvelopeSmsService : IEnvelopeSmsService
{
private readonly ISmsSender _sender;
private readonly TotpSmsParams _totpSmsParams;
public EnvelopeSmsService(ISmsSender sender, IOptions<TotpSmsParams> totpSmsParamsOptions)
{
_sender = sender;
_totpSmsParams = totpSmsParamsOptions.Value;
}
}

View File

@ -21,9 +21,9 @@ using EnvelopeGenerator.Application.DTOs;
using DigitalData.Core.Client;
using EnvelopeGenerator.Application.Extensions;
using Microsoft.Extensions.Caching.Distributed;
using System.Globalization;
using Microsoft.Extensions.Options;
using EnvelopeGenerator.Application.Configurations;
using EnvelopeGenerator.Application.DTOs.Messaging;
namespace EnvelopeGenerator.Web.Controllers
{
@ -44,8 +44,9 @@ namespace EnvelopeGenerator.Web.Controllers
private readonly IReceiverService _rcvService;
private readonly IDistributedCache _dCache;
private readonly TotpSmsParams _totpSmsParams;
private readonly IEnvelopeSmsHandler _envSmsHandler;
public HomeController(EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer<Resource> localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, ISmsSender messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService, IDistributedCache distributedCache, IOptions<TotpSmsParams> totpSmsParamsOptions)
public HomeController(EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer<Resource> localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, ISmsSender messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService, IDistributedCache distributedCache, IOptions<TotpSmsParams> totpSmsParamsOptions, IEnvelopeSmsHandler envelopeSmsService)
{
this.envelopeOldService = envelopeOldService;
_envRcvService = envelopeReceiverService;
@ -62,6 +63,7 @@ namespace EnvelopeGenerator.Web.Controllers
_rcvService = receiverService;
_dCache = distributedCache;
_totpSmsParams = totpSmsParamsOptions.Value;
_envSmsHandler = envelopeSmsService;
}
[HttpGet("/")]
@ -174,23 +176,14 @@ namespace EnvelopeGenerator.Web.Controllers
{
if (viaSms)
{
//TODO: create a service (like EnvelopeSmsService)
//add date time cache
var key = string.Format(_totpSmsParams.Expiration.CacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId);
var expiration = await _dCache.GetDateTimeAsync(key);
if (expiration is null || expiration <= DateTime.Now)
{
var new_expiration = DateTime.Now.AddSeconds(_totpSmsParams.TotpStep);
var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
var smsRes = await _msgService.SendSmsAsync(er_secret.PhoneNumber!, msg);
if (smsRes.Failed)
var (smsRes, expiration) = await _envSmsHandler.SendTotpAsync(er_secret);
if (smsRes is not null && 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);
}