diff --git a/EnvelopeGenerator.Application/Configurations/EnvelopeReceiverCacheParams.cs b/EnvelopeGenerator.Application/Configurations/EnvelopeReceiverCacheParams.cs new file mode 100644 index 00000000..b8ab323e --- /dev/null +++ b/EnvelopeGenerator.Application/Configurations/EnvelopeReceiverCacheParams.cs @@ -0,0 +1,19 @@ +namespace EnvelopeGenerator.Application.Configurations +{ + public class EnvelopeReceiverCacheParams + { + /// + /// Gets the cache key format for SMS codes. + /// The placeholder {0} represents the envelopeReceiverId. + /// + public string CodeCacheKeyFormat { get; init; } = "sms-code-{0}"; + + /// + /// Gets the cache expiration key format for SMS codes. + /// The placeholder {0} represents the envelopeReceiverId. + /// + public string CodeExpirationCacheKeyFormat { get; init; } = "sms-code-expiration-{0}"; + + public TimeSpan CodeCacheValidityPeriod { get; init; } = new(0, 5, 0); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs b/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs index e65be8ac..7cea8047 100644 --- a/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs +++ b/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs @@ -21,19 +21,5 @@ namespace EnvelopeGenerator.Application.Configurations.GtxMessaging public string MessageQueryParamName { get; init; } = "text"; public int CodeLength { get; init; } = 5; - - /// - /// Gets the cache key format for SMS codes. - /// The placeholder {0} represents the envelopeReceiverId. - /// - public string CodeCacheKeyFormat { get; init; } = "sms-code-{0}"; - - /// - /// Gets the cache expiration key format for SMS codes. - /// The placeholder {0} represents the envelopeReceiverId. - /// - public string CodeExpirationCacheKeyFormat { get; init; } = "sms-code-expiration-{0}"; - - public TimeSpan CodeCacheValidityPeriod { get; init; } = new(0, 5, 0); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverCache.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverCache.cs new file mode 100644 index 00000000..e2ac2a7c --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverCache.cs @@ -0,0 +1,17 @@ +namespace EnvelopeGenerator.Application.Contracts +{ + public interface IEnvelopeReceiverCache + { + Task GetSmsCodeAsync(string envelopeReceiverId); + + /// + /// Asynchronously stores an SMS verification code in the cache and returns the expiration date of the code. + /// + /// The unique identifier for the recipient of the envelope to associate with the SMS code. + /// The SMS verification code to be stored. + /// A task that represents the asynchronous operation. The task result contains the expiration date and time of the stored SMS code. + Task SetSmsCodeAsync(string envelopeReceiverId, string code); + + Task GetSmsCodeExpirationAsync(string envelopeReceiverId); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs index 83a92bd0..3c1c62d5 100644 --- a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs @@ -15,7 +15,7 @@ namespace EnvelopeGenerator.Application.Extensions { public static class DIExtensions { - public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfigurationSection dispatcherConfigSection, IConfigurationSection mailConfigSection, IConfigurationSection smsConfigSection, IConfigurationSection codeGeneratorConfigSection) + public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfigurationSection dispatcherConfigSection, IConfigurationSection mailConfigSection, IConfigurationSection smsConfigSection, IConfigurationSection codeGeneratorConfigSection, IConfigurationSection envelopeReceiverCacheParamsSection) { //Inject CRUD Service and repositoriesad services.TryAddScoped(); @@ -56,10 +56,12 @@ namespace EnvelopeGenerator.Application.Extensions services.Configure(dispatcherConfigSection); services.Configure(mailConfigSection); services.Configure(codeGeneratorConfigSection); + services.Configure(envelopeReceiverCacheParamsSection); services.AddHttpClientService(smsConfigSection); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); return services; } @@ -68,6 +70,7 @@ namespace EnvelopeGenerator.Application.Extensions dispatcherConfigSection: config.GetSection("DispatcherConfig"), mailConfigSection: config.GetSection("MailConfig"), smsConfigSection: config.GetSection("SmsConfig"), - codeGeneratorConfigSection: config.GetSection("CodeGeneratorConfig")); + codeGeneratorConfigSection: config.GetSection("CodeGeneratorConfig"), + envelopeReceiverCacheParamsSection: config.GetSection("EnvelopeReceiverCacheParams")); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/EnvelopeReceiverCache.cs b/EnvelopeGenerator.Application/Services/EnvelopeReceiverCache.cs new file mode 100644 index 00000000..525b5468 --- /dev/null +++ b/EnvelopeGenerator.Application/Services/EnvelopeReceiverCache.cs @@ -0,0 +1,50 @@ +using AngleSharp.Dom; +using EnvelopeGenerator.Application.Configurations; +using EnvelopeGenerator.Application.Contracts; +using EnvelopeGenerator.Application.Extensions; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Options; + +namespace EnvelopeGenerator.Application.Services +{ + public class EnvelopeReceiverCache : IEnvelopeReceiverCache + { + private readonly EnvelopeReceiverCacheParams _cacheParams; + + private readonly DistributedCacheEntryOptions _codeCacheOptions; + + private readonly IDistributedCache _cache; + + public EnvelopeReceiverCache(IOptions cacheParamOptions, IDistributedCache cache) + { + _cacheParams = cacheParamOptions.Value; + _codeCacheOptions = new() { AbsoluteExpirationRelativeToNow = cacheParamOptions.Value.CodeCacheValidityPeriod }; + _cache = cache; + } + + public async Task GetSmsCodeAsync(string envelopeReceiverId) + { + var code_key = string.Format(_cacheParams.CodeCacheKeyFormat, envelopeReceiverId); + return await _cache.GetStringAsync(code_key); + } + + public async Task SetSmsCodeAsync(string envelopeReceiverId, string code) + { + // set key + var code_key = string.Format(_cacheParams.CodeCacheKeyFormat, envelopeReceiverId); + await _cache.SetStringAsync(code_key, code, _codeCacheOptions); + + // set expiration + var code_expiration_key = string.Format(_cacheParams.CodeExpirationCacheKeyFormat, envelopeReceiverId); + var expiration = DateTime.Now + _cacheParams.CodeCacheValidityPeriod; + await _cache.SetDateTimeAsync(code_expiration_key, expiration, _codeCacheOptions); + return expiration; + } + + public async Task GetSmsCodeExpirationAsync(string envelopeReceiverId) + { + var code_expiration_key = string.Format(_cacheParams.CodeExpirationCacheKeyFormat, envelopeReceiverId); + return await _cache.GetDateTimeAsync(code_expiration_key); + } + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs index be2c1cc9..17d179eb 100644 --- a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs +++ b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs @@ -21,21 +21,18 @@ namespace EnvelopeGenerator.Application.Services private readonly ICodeGenerator _codeGen; - private readonly IDistributedCache _cache; + private readonly IEnvelopeReceiverCache _erCache; public string ServiceProvider { get; } - private readonly DistributedCacheEntryOptions _codeCacheOptions; - - public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper, ICodeGenerator codeGenerator, IDistributedCache distributedCache) + public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper, ICodeGenerator codeGenerator, IEnvelopeReceiverCache envelopeReceiverCache) { _smsClient = smsClient; _smsParams = smsParamsOptions.Value; _mapper = mapper; ServiceProvider = GetType().Name.Replace("Service", string.Empty); _codeGen = codeGenerator; - _cache = distributedCache; - _codeCacheOptions = new() { AbsoluteExpirationRelativeToNow = smsParamsOptions.Value.CodeCacheValidityPeriod }; + _erCache = envelopeReceiverCache; } public async Task SendSmsAsync(string recipient, string message) @@ -51,27 +48,19 @@ namespace EnvelopeGenerator.Application.Services public async Task SendSmsCodeAsync(string recipient, string envelopeReceiverId) { - var code_expiration_key = string.Format(_smsParams.CodeExpirationCacheKeyFormat, envelopeReceiverId); - - var code_key = string.Format(_smsParams.CodeCacheKeyFormat, envelopeReceiverId); - var code = await _cache.GetStringAsync(code_key); + var code = await _erCache.GetSmsCodeAsync(envelopeReceiverId); if (code is null) { - code = _codeGen.GenerateCode(_smsParams.CodeLength); - - await _cache.SetStringAsync(code_key, code, _codeCacheOptions); - - var expiration = DateTime.Now + _smsParams.CodeCacheValidityPeriod; - await _cache.SetDateTimeAsync(code_expiration_key, expiration, _codeCacheOptions); - + code = _codeGen.GenerateCode(_smsParams.CodeLength); + var expiration = await _erCache.SetSmsCodeAsync(envelopeReceiverId, code); var res = await SendSmsAsync(recipient: recipient, message: code); res.Expiration = expiration; return res; } else { - var code_expiration = await _cache.GetDateTimeAsync(code_expiration_key); + var code_expiration = await _erCache.GetSmsCodeExpirationAsync(envelopeReceiverId); return code_expiration is null ? new() { Ok = false } : new() { Ok = false, AllowedAt = code_expiration }; diff --git a/EnvelopeGenerator.Web/appsettings.json b/EnvelopeGenerator.Web/appsettings.json index 19d64dd2..6c4b4e50 100644 --- a/EnvelopeGenerator.Web/appsettings.json +++ b/EnvelopeGenerator.Web/appsettings.json @@ -136,6 +136,7 @@ "QueryParams": { "from": "signFlow" }, - "CodeCacheValidityPeriod": "00:10:00" - } + "CodeCacheValidityPeriod": "00:10:00" + }, + "EnvelopeReceiverCacheParams": {} } \ No newline at end of file