70 lines
2.9 KiB
C#
70 lines
2.9 KiB
C#
using EnvelopeGenerator.Application.Interfaces.Services;
|
|
using EnvelopeGenerator.Application.Extensions;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Options;
|
|
using EnvelopeGenerator.Application.Common.Configurations;
|
|
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
|
|
using EnvelopeGenerator.Application.Common.Dto.Messaging;
|
|
|
|
namespace EnvelopeGenerator.Application.Services;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class EnvelopeSmsHandler : IEnvelopeSmsHandler
|
|
{
|
|
private readonly ISmsSender _sender;
|
|
|
|
private readonly TotpSmsParams _totpSmsParams;
|
|
|
|
private readonly IDistributedCache _dCache;
|
|
|
|
private readonly IAuthenticator _authenticator;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="totpSmsParamsOptions"></param>
|
|
/// <param name="distributedCache"></param>
|
|
/// <param name="authenticator"></param>
|
|
public EnvelopeSmsHandler(ISmsSender sender, IOptions<TotpSmsParams> totpSmsParamsOptions, IDistributedCache distributedCache, IAuthenticator authenticator)
|
|
{
|
|
_sender = sender;
|
|
_totpSmsParams = totpSmsParamsOptions.Value;
|
|
_dCache = distributedCache;
|
|
_authenticator = authenticator;
|
|
}
|
|
|
|
/// <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 = _authenticator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
|
|
var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
|
|
await _dCache.SetDateTimeAsync(key, new_expiration, cToken: cToken);
|
|
return (await _sender.SendSmsAsync(er_secret.PhoneNumber!, msg), new_expiration);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="totpCode"></param>
|
|
/// <param name="secretKey"></param>
|
|
/// <returns></returns>
|
|
public bool VerifyTotp(string totpCode, string secretKey) => _authenticator
|
|
.VerifyTotp(totpCode, secretKey, _totpSmsParams.TotpStep, _totpSmsParams.TotpVerificationWindow);
|
|
} |