feat(EnvelopeSmsService): SendTotpAsync-Methode hinzufügen, um totp unter Berücksichtigung der Ablaufzeit zu senden.
This commit is contained in:
parent
aa918d875d
commit
772d510705
@ -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);
|
||||||
|
}
|
||||||
@ -1,5 +0,0 @@
|
|||||||
namespace EnvelopeGenerator.Application.Contracts;
|
|
||||||
|
|
||||||
public interface IEnvelopeSmsService
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@ -60,7 +60,7 @@ namespace EnvelopeGenerator.Application.Extensions
|
|||||||
|
|
||||||
services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams)));
|
services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams)));
|
||||||
services.TryAddSingleton<ISmsSender, GTXSmsSender>();
|
services.TryAddSingleton<ISmsSender, GTXSmsSender>();
|
||||||
services.TryAddSingleton<IEnvelopeSmsService, EnvelopeSmsService>();
|
services.TryAddSingleton<IEnvelopeSmsHandler, EnvelopeSmsHandler>();
|
||||||
services.TryAddSingleton<ICodeGenerator, CodeGenerator>();
|
services.TryAddSingleton<ICodeGenerator, CodeGenerator>();
|
||||||
services.TryAddSingleton<QRCodeGenerator>();
|
services.TryAddSingleton<QRCodeGenerator>();
|
||||||
|
|
||||||
|
|||||||
50
EnvelopeGenerator.Application/Services/EnvelopeSmsHandler.cs
Normal file
50
EnvelopeGenerator.Application/Services/EnvelopeSmsHandler.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -21,9 +21,9 @@ using EnvelopeGenerator.Application.DTOs;
|
|||||||
using DigitalData.Core.Client;
|
using DigitalData.Core.Client;
|
||||||
using EnvelopeGenerator.Application.Extensions;
|
using EnvelopeGenerator.Application.Extensions;
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using System.Globalization;
|
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using EnvelopeGenerator.Application.Configurations;
|
using EnvelopeGenerator.Application.Configurations;
|
||||||
|
using EnvelopeGenerator.Application.DTOs.Messaging;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers
|
namespace EnvelopeGenerator.Web.Controllers
|
||||||
{
|
{
|
||||||
@ -44,8 +44,9 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
private readonly IReceiverService _rcvService;
|
private readonly IReceiverService _rcvService;
|
||||||
private readonly IDistributedCache _dCache;
|
private readonly IDistributedCache _dCache;
|
||||||
private readonly TotpSmsParams _totpSmsParams;
|
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;
|
this.envelopeOldService = envelopeOldService;
|
||||||
_envRcvService = envelopeReceiverService;
|
_envRcvService = envelopeReceiverService;
|
||||||
@ -62,6 +63,7 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
_rcvService = receiverService;
|
_rcvService = receiverService;
|
||||||
_dCache = distributedCache;
|
_dCache = distributedCache;
|
||||||
_totpSmsParams = totpSmsParamsOptions.Value;
|
_totpSmsParams = totpSmsParamsOptions.Value;
|
||||||
|
_envSmsHandler = envelopeSmsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("/")]
|
[HttpGet("/")]
|
||||||
@ -174,22 +176,13 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
{
|
{
|
||||||
if (viaSms)
|
if (viaSms)
|
||||||
{
|
{
|
||||||
//TODO: create a service (like EnvelopeSmsService)
|
var (smsRes, expiration) = await _envSmsHandler.SendTotpAsync(er_secret);
|
||||||
//add date time cache
|
|
||||||
var key = string.Format(_totpSmsParams.Expiration.CacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId);
|
if (smsRes is not null && smsRes.Failed)
|
||||||
var expiration = await _dCache.GetDateTimeAsync(key);
|
|
||||||
if (expiration is null || expiration <= DateTime.Now)
|
|
||||||
{
|
{
|
||||||
var new_expiration = DateTime.Now.AddSeconds(_totpSmsParams.TotpStep);
|
var res_json = JsonConvert.SerializeObject(smsRes);
|
||||||
var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
|
_logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}");
|
||||||
var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
|
return this.ViewInnerServiceError();
|
||||||
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);
|
return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", expiration);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user