feat(EnvelopeSmsService): SendTotpAsync-Methode hinzufügen, um totp unter Berücksichtigung der Ablaufzeit zu senden.
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user