diff --git a/EnvelopeGenerator.Application/Configurations/CodeGeneratorParams.cs b/EnvelopeGenerator.Application/Configurations/CodeGeneratorParams.cs
new file mode 100644
index 00000000..2ba65399
--- /dev/null
+++ b/EnvelopeGenerator.Application/Configurations/CodeGeneratorParams.cs
@@ -0,0 +1,19 @@
+namespace EnvelopeGenerator.Application.Configurations
+{
+ public class CodeGeneratorParams
+ {
+ public string CharPool { get; init; } = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567890123456789";
+
+ //TODO: Increase the DefaultTotpSecretKeyLength (e.g. to 32) but make sure that the QR code is generated correctly and can be scanned by the authenticator.
+ public int DefaultTotpSecretKeyLength { get; init; } = 20;
+
+ public string TotpIssuer { get; init; } = "signFlow";
+
+ ///
+ /// 0 is user email, 1 is secret key and 2 is issuer.
+ ///
+ public string TotpUrlFormat { get; init; } = "otpauth://totp/{0}?secret={1}&issuer={2}";
+
+ public int TotpQRPixelsPerModule { get; init; } = 20;
+ }
+}
\ No newline at end of file
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
new file mode 100644
index 00000000..7cea8047
--- /dev/null
+++ b/EnvelopeGenerator.Application/Configurations/GtxMessaging/SmsParams.cs
@@ -0,0 +1,25 @@
+using DigitalData.Core.Abstractions.Client;
+using Microsoft.Extensions.Caching.Distributed;
+
+namespace EnvelopeGenerator.Application.Configurations.GtxMessaging
+{
+ ///
+ /// https://www.gtx-messaging.com/en/api-docs/sms-rest-api/
+ ///
+ public class SmsParams : IHttpClientOptions
+ {
+ public required string Uri { get; init; }
+
+ public string? Path { get; init; }
+
+ public Dictionary? Headers { get; init; }
+
+ public Dictionary? QueryParams { get; init; }
+
+ public string RecipientQueryParamName { get; init; } = "to";
+
+ public string MessageQueryParamName { get; init; } = "text";
+
+ public int CodeLength { get; init; } = 5;
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs
new file mode 100644
index 00000000..38a27b85
--- /dev/null
+++ b/EnvelopeGenerator.Application/Contracts/ICodeGenerator.cs
@@ -0,0 +1,13 @@
+namespace EnvelopeGenerator.Application.Contracts
+{
+ public interface ICodeGenerator
+ {
+ string GenerateCode(int length);
+
+ public string GenerateTotpSecretKey(int? length = null);
+
+ public byte[] GenerateTotpQrCode(string userEmail, string secretKey, string? issuer = null, string? totpUrlFormat = null, int? pixelsPerModule = null);
+
+ public byte[] GenerateTotpQrCode(string userEmail, int? length = null, string? issuer = null, string? totpUrlFormat = null, int? pixelsPerModule = null);
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeMailService.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeMailService.cs
index 06a4ab18..14d94057 100644
--- a/EnvelopeGenerator.Application/Contracts/IEnvelopeMailService.cs
+++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeMailService.cs
@@ -8,10 +8,12 @@ namespace EnvelopeGenerator.Application.Contracts
{
public interface IEnvelopeMailService : IEmailOutService
{
- Task> SendAsync(EnvelopeReceiverDto envelopeReceiverDto, Constants.EmailTemplateType tempType);
+ Task> SendAsync(EnvelopeReceiverDto envelopeReceiverDto, Constants.EmailTemplateType tempType, Dictionary? optionalPlaceholders = null);
- Task> SendAsync(EnvelopeReceiverReadOnlyDto dto);
+ Task> SendAsync(EnvelopeReceiverReadOnlyDto dto, Dictionary? optionalPlaceholders = null);
Task> SendAccessCodeAsync(EnvelopeReceiverDto envelopeReceiverDto);
+
+ Task> SendTFAQrCodeAsync(EnvelopeReceiverDto envelopeReceiverDto);
}
}
\ 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/Contracts/IEnvelopeReceiverService.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs
index 75a897a2..f22fde28 100644
--- a/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs
+++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs
@@ -1,7 +1,7 @@
using DigitalData.Core.Abstractions.Application;
using DigitalData.Core.DTO;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
-using EnvelopeGenerator.Application.DTOs.Receiver;
+using EnvelopeGenerator.Application.DTOs.Messaging;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.Contracts
@@ -9,15 +9,17 @@ namespace EnvelopeGenerator.Application.Contracts
public interface IEnvelopeReceiverService : IBasicCRUDService
{
- Task>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false);
+ Task>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false, bool readOnly = true);
- Task>> ReadSecretByUuidAsync(string uuid, bool withEnvelope = false, bool withReceiver = true);
+ Task>> ReadAccessCodeByUuidAsync(string uuid, bool withEnvelope = false, bool withReceiver = true);
- Task>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true);
+ Task>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true, bool readOnly = true);
- Task> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true);
+ Task> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true);
- Task> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true);
+ Task> ReadWithSecretByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true);
+
+ Task> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true);
Task> ReadAccessCodeByIdAsync(int envelopeId, int receiverId);
@@ -30,5 +32,7 @@ namespace EnvelopeGenerator.Application.Contracts
Task>> ReadByUsernameAsync(string username, int? min_status = null, int? max_status = null, params int[] ignore_statuses);
Task> ReadLastUsedReceiverNameByMail(string mail);
+
+ Task> SendSmsAsync(string envelopeReceiverId, string message);
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Contracts/IMessagingService.cs b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs
new file mode 100644
index 00000000..5940e50d
--- /dev/null
+++ b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs
@@ -0,0 +1,13 @@
+using EnvelopeGenerator.Application.DTOs.Messaging;
+
+namespace EnvelopeGenerator.Application.Contracts
+{
+ public interface IMessagingService
+ {
+ string ServiceProvider { get; }
+
+ Task SendSmsAsync(string recipient, string message);
+
+ Task SendSmsCodeAsync(string recipient, string envelopeReceiverId);
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Contracts/IReceiverService.cs b/EnvelopeGenerator.Application/Contracts/IReceiverService.cs
index 3c08f17b..c474363b 100644
--- a/EnvelopeGenerator.Application/Contracts/IReceiverService.cs
+++ b/EnvelopeGenerator.Application/Contracts/IReceiverService.cs
@@ -1,4 +1,5 @@
-using DigitalData.Core.Abstractions.Application;
+using DigitalData.Core.Abstractions;
+using DigitalData.Core.Abstractions.Application;
using DigitalData.Core.DTO;
using EnvelopeGenerator.Application.DTOs.Receiver;
using EnvelopeGenerator.Domain.Entities;
@@ -7,8 +8,10 @@ namespace EnvelopeGenerator.Application.Contracts
{
public interface IReceiverService : ICRUDService
{
- public Task> ReadByAsync(string? emailAddress = null, string? signature = null);
+ Task> ReadByAsync(string? emailAddress = null, string? signature = null);
- public Task DeleteByAsync(string? emailAddress = null, string? signature = null);
+ Task DeleteByAsync(string? emailAddress = null, string? signature = null);
+
+ Task UpdateAsync(TUpdateDto updateDto) where TUpdateDto : IUnique;
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DIExtensions.cs b/EnvelopeGenerator.Application/DIExtensions.cs
deleted file mode 100644
index c8179fa6..00000000
--- a/EnvelopeGenerator.Application/DIExtensions.cs
+++ /dev/null
@@ -1,63 +0,0 @@
-using DigitalData.UserManager.Application.MappingProfiles;
-using EnvelopeGenerator.Application.Contracts;
-using EnvelopeGenerator.Application.MappingProfiles;
-using EnvelopeGenerator.Application.Configurations;
-using EnvelopeGenerator.Application.Services;
-using EnvelopeGenerator.Infrastructure.Contracts;
-using EnvelopeGenerator.Infrastructure.Repositories;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
-
-namespace EnvelopeGenerator.Application
-{
- public static class DIExtensions
- {
- public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration dispatcherConfigSection, IConfiguration mailConfigSection)
- {
- //Inject CRUD Service and repositoriesad
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
- services.AddScoped();
-
- //Auto mapping profiles
- services.AddAutoMapper(typeof(BasicDtoMappingProfile).Assembly);
- services.AddAutoMapper(typeof(UserMappingProfile).Assembly);
-
- services.Configure(dispatcherConfigSection);
- services.Configure(mailConfigSection);
-
- return services;
- }
-
- public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration config) => services.AddEnvelopeGenerator(
- dispatcherConfigSection: config.GetSection("DispatcherConfig"),
- mailConfigSection: config.GetSection("MailConfig"));
- }
-}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DTOs/ConfigDto.cs b/EnvelopeGenerator.Application/DTOs/ConfigDto.cs
index f96f131d..2dc76824 100644
--- a/EnvelopeGenerator.Application/DTOs/ConfigDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/ConfigDto.cs
@@ -1,4 +1,5 @@
using DigitalData.Core.Abstractions;
+using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
namespace EnvelopeGenerator.Application.DTOs
@@ -8,11 +9,9 @@ namespace EnvelopeGenerator.Application.DTOs
int SendingProfile,
string SignatureHost,
string ExternalProgramName,
- string ExportPath,
- string DocumentPathDmz,
- string ExportPathDmz,
- string DocumentPathMoveAftsend) : IUnique
+ string ExportPath) : IUnique
{
+ [NotMapped]
[JsonIgnore]
[Obsolete("Configuration does not have an ID; it represents a single table in the database.")]
public int Id => throw new InvalidOperationException("This configuration does not support an ID as it represents a single row in the database.");
diff --git a/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverBasicDto.cs b/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverBasicDto.cs
index 4dfdf6a8..1302f14f 100644
--- a/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverBasicDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverBasicDto.cs
@@ -25,5 +25,9 @@ namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiver
public DateTime AddedWhen { get; init; }
public DateTime? ChangedWhen { get; init; }
+
+ public bool HasPhoneNumber { get; init; }
+
+ public bool TFAEnabled { get; init; }
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverSecretDto.cs b/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverSecretDto.cs
index 511c6d42..9470766e 100644
--- a/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverSecretDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/EnvelopeReceiver/EnvelopeReceiverSecretDto.cs
@@ -1,4 +1,9 @@
namespace EnvelopeGenerator.Application.DTOs.EnvelopeReceiver
{
- public record EnvelopeReceiverSecretDto(string? AccessCode) : EnvelopeReceiverDto;
+ public record EnvelopeReceiverSecretDto() : EnvelopeReceiverDto()
+ {
+ public string? AccessCode { get; init; }
+
+ public string? PhoneNumber { get; init; }
+ }
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs
new file mode 100644
index 00000000..96b67515
--- /dev/null
+++ b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs
@@ -0,0 +1,19 @@
+namespace EnvelopeGenerator.Application.DTOs.Messaging
+{
+ public record SmsResponse
+ {
+ public required bool Ok { get; init; }
+
+ public DateTime? Expiration { get; set; }
+
+ public DateTime? AllowedAt { get; set; }
+
+ public TimeSpan AllowedAfter => Allowed ? TimeSpan.Zero : AllowedAt!.Value - DateTime.Now;
+
+ public bool Allowed => AllowedAt is null || DateTime.Now >= AllowedAt;
+
+ public bool Error => !Ok && Allowed;
+
+ public dynamic? Errors { get; init; }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverCreateDto.cs b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverCreateDto.cs
index 949ee06a..87e4d3f9 100644
--- a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverCreateDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverCreateDto.cs
@@ -4,7 +4,7 @@ using System.Text;
namespace EnvelopeGenerator.Application.DTOs.Receiver
{
- public record ReceiverCreateDto([EmailAddress] string EmailAddress)
+ public record ReceiverCreateDto([EmailAddress] string EmailAddress, string? TotpSecretkey = null, DateTime? TotpExpiration = null)
{
public string Signature => sha256HexOfMail.Value;
diff --git a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverReadDto.cs b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverReadDto.cs
index ec6e5b76..6d1b5a19 100644
--- a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverReadDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverReadDto.cs
@@ -1,4 +1,6 @@
-using DigitalData.Core.DTO;
+using DigitalData.Core.Abstractions;
+using DigitalData.Core.DTO;
+using DigitalData.EmailProfilerDispatcher.Abstraction.Attributes;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
using System.Text.Json.Serialization;
@@ -8,12 +10,17 @@ namespace EnvelopeGenerator.Application.DTOs.Receiver
int Id,
string EmailAddress,
string Signature,
- DateTime AddedWhen
- ) : BaseDTO(Id)
+ DateTime AddedWhen
+ ) : BaseDTO(Id), IUnique
{
[JsonIgnore]
public IEnumerable? EnvelopeReceivers { get; init; }
public string? LastUsedName => EnvelopeReceivers?.LastOrDefault()?.Name;
+
+ public string? TotpSecretkey { get; set; } = null;
+
+ [TemplatePlaceholder("[TFA_QR_EXPIRATION]")]
+ public DateTime? TotpExpiration { get; set; } = null;
};
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverUpdateDto.cs b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverUpdateDto.cs
index 8f765667..08ec5616 100644
--- a/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverUpdateDto.cs
+++ b/EnvelopeGenerator.Application/DTOs/Receiver/ReceiverUpdateDto.cs
@@ -2,5 +2,5 @@
namespace EnvelopeGenerator.Application.DTOs.Receiver
{
- public record ReceiverUpdateDto(int Id) : IUnique;
-}
\ No newline at end of file
+ public record ReceiverUpdateDto(int Id, string? TotpSecretkey = null, DateTime? TotpExpiration = null) : IUnique;
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj
index f1267868..6032bb17 100644
--- a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj
+++ b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj
@@ -12,11 +12,14 @@
-
+
+
+
+
diff --git a/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs
new file mode 100644
index 00000000..45f43109
--- /dev/null
+++ b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs
@@ -0,0 +1,82 @@
+using Microsoft.Extensions.Caching.Distributed;
+
+namespace EnvelopeGenerator.Application.Extensions
+{
+ public static class CacheExtensions
+ {
+ public static Task SetLongAsync(this IDistributedCache cache, string key, long value, DistributedCacheEntryOptions? options = null)
+ => options is null
+ ? cache.SetAsync(key, BitConverter.GetBytes(value))
+ : cache.SetAsync(key, BitConverter.GetBytes(value), options: options);
+
+ public static async Task GetLongAsync(this IDistributedCache cache, string key)
+ {
+ var value = await cache.GetAsync(key);
+ return value is null ? null : BitConverter.ToInt64(value, 0);
+ }
+
+ public static Task SetDateTimeAsync(this IDistributedCache cache, string key, DateTime value, DistributedCacheEntryOptions? options = null)
+ => cache.SetLongAsync(key: key, value: value.Ticks, options: options);
+
+ public static async Task GetDateTimeAsync(this IDistributedCache cache, string key)
+ {
+ var value = await cache.GetAsync(key);
+ return value is null ? null : new(BitConverter.ToInt64(value, 0));
+ }
+
+ public static Task SetTimeSpanAsync(this IDistributedCache cache, string key, TimeSpan value, DistributedCacheEntryOptions? options = null)
+ => cache.SetLongAsync(key: key, value: value.Ticks, options: options);
+
+ public static async Task GetTimeSpanAsync(this IDistributedCache cache, string key)
+ {
+ var value = await cache.GetAsync(key);
+ return value is null ? null : new(BitConverter.ToInt64(value, 0));
+ }
+
+ public static string GetOrSet(this IDistributedCache cache, string key, Func factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
+ {
+ var value = cache.GetString(key);
+ if (value is null)
+ {
+ // create new and save
+ value = factory();
+
+ void Cache()
+ {
+ if (options is null)
+ cache.SetString(key: key, value: value);
+ else
+ cache.SetString(key: key, value: value, options: options);
+ }
+
+ if (cacheInBackground)
+ _ = Task.Run(() => Cache(), token);
+ else
+ Cache();
+ }
+
+ return value;
+ }
+
+ public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
+ {
+ var value = await cache.GetStringAsync(key, token: token);
+ if(value is null)
+ {
+ // create new and save
+ value = await factory();
+
+ Task CacheAsync() => options is null
+ ? cache.SetStringAsync(key: key, value: value, token: token)
+ : cache.SetStringAsync(key: key, value: value, options: options, token: token);
+
+ if (cacheInBackground)
+ _ = Task.Run(async () => await CacheAsync(), token);
+ else
+ await CacheAsync();
+ }
+
+ return value;
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs
new file mode 100644
index 00000000..ae8fedf9
--- /dev/null
+++ b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs
@@ -0,0 +1,78 @@
+using DigitalData.UserManager.Application.MappingProfiles;
+using EnvelopeGenerator.Application.Contracts;
+using EnvelopeGenerator.Application.MappingProfiles;
+using EnvelopeGenerator.Application.Configurations;
+using EnvelopeGenerator.Application.Services;
+using EnvelopeGenerator.Infrastructure.Contracts;
+using EnvelopeGenerator.Infrastructure.Repositories;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using DigitalData.Core.Client;
+using EnvelopeGenerator.Application.Configurations.GtxMessaging;
+using QRCoder;
+
+namespace EnvelopeGenerator.Application.Extensions
+{
+ public static class DIExtensions
+ {
+ public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfigurationSection dispatcherConfigSection, IConfigurationSection mailConfigSection, IConfigurationSection smsConfigSection, IConfigurationSection codeGeneratorConfigSection, IConfigurationSection envelopeReceiverCacheParamsSection)
+ {
+ //Inject CRUD Service and repositoriesad
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+
+ //Auto mapping profiles
+ services.AddAutoMapper(typeof(BasicDtoMappingProfile).Assembly);
+ services.AddAutoMapper(typeof(UserMappingProfile).Assembly);
+
+ services.Configure(dispatcherConfigSection);
+ services.Configure(mailConfigSection);
+ services.Configure(codeGeneratorConfigSection);
+ services.Configure(envelopeReceiverCacheParamsSection);
+
+ services.AddHttpClientService(smsConfigSection);
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+
+ return services;
+ }
+
+ public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration config) => services.AddEnvelopeGenerator(
+ dispatcherConfigSection: config.GetSection("DispatcherConfig"),
+ mailConfigSection: config.GetSection("MailConfig"),
+ smsConfigSection: config.GetSection("SmsConfig"),
+ codeGeneratorConfigSection: config.GetSection("CodeGeneratorParams"),
+ envelopeReceiverCacheParamsSection: config.GetSection("EnvelopeReceiverCacheParams"));
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Extensions/DTOExtensions.cs b/EnvelopeGenerator.Application/Extensions/DTOExtensions.cs
new file mode 100644
index 00000000..f1cd28ca
--- /dev/null
+++ b/EnvelopeGenerator.Application/Extensions/DTOExtensions.cs
@@ -0,0 +1,22 @@
+using EnvelopeGenerator.Application.DTOs.Receiver;
+using EnvelopeGenerator.Extensions;
+using Newtonsoft.Json;
+
+namespace EnvelopeGenerator.Application.Extensions
+{
+ public static class DTOExtensions
+ {
+ public static bool IsTotpSecretExpired(this ReceiverReadDto dto, int minutesBeforeExpiration = 30)
+ => dto.TotpExpiration < DateTime.Now.AddMinutes(minutesBeforeExpiration * -1);
+
+ public static bool IsTotpSecretInvalid(this ReceiverReadDto dto, int minutesBeforeExpiration = 30)
+ => dto.IsTotpSecretExpired(minutesBeforeExpiration) || dto.TotpSecretkey is null;
+
+ public static bool IsTotpSecretValid(this ReceiverReadDto dto, int minutesBeforeExpiration = 30)
+ => !dto.IsTotpSecretInvalid(minutesBeforeExpiration);
+
+ public static bool IsTotpValid(this ReceiverReadDto dto, string totp) => dto.TotpSecretkey is null ? throw new ArgumentNullException(nameof(dto), $"TotpSecretkey of DTO cannot validate without TotpSecretkey. Dto: {JsonConvert.SerializeObject(dto)}") : totp.IsValidTotp(dto.TotpSecretkey);
+
+ public static bool IsTotpInvalid(this ReceiverReadDto dto, string totp) => !dto.IsTotpValid(totp: totp);
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Extensions/MappingExtensions.cs b/EnvelopeGenerator.Application/Extensions/MappingExtensions.cs
new file mode 100644
index 00000000..bd996b58
--- /dev/null
+++ b/EnvelopeGenerator.Application/Extensions/MappingExtensions.cs
@@ -0,0 +1,14 @@
+using EnvelopeGenerator.Domain.HttpResponse;
+
+namespace EnvelopeGenerator.Application.Extensions
+{
+ public static class MappingExtensions
+ {
+ public static bool Ok(this GtxMessagingResponse gtxMessagingResponse)
+ => gtxMessagingResponse.TryGetValue("message-status", out var status)
+ && status?.ToString()?.ToLower() == "ok";
+
+ public static string ToBase64String(this byte[] bytes)
+ => Convert.ToBase64String(bytes);
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Key.cs b/EnvelopeGenerator.Application/Key.cs
index 4509d0ea..52f35d19 100644
--- a/EnvelopeGenerator.Application/Key.cs
+++ b/EnvelopeGenerator.Application/Key.cs
@@ -14,6 +14,7 @@
public static readonly string PossibleSecurityBreach = nameof(PossibleSecurityBreach);
public static readonly string WrongEnvelopeReceiverId = nameof(WrongEnvelopeReceiverId);
public static readonly string EnvelopeOrReceiverNonexists = nameof(EnvelopeOrReceiverNonexists);
+ public static readonly string PhoneNumberNonexists = nameof(PhoneNumberNonexists);
public static readonly string Default = nameof(Default);
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs b/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs
index 18462893..cf3c006b 100644
--- a/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs
+++ b/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs
@@ -3,8 +3,11 @@ using EnvelopeGenerator.Application.DTOs;
using EnvelopeGenerator.Application.DTOs.EnvelopeHistory;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly;
+using EnvelopeGenerator.Application.DTOs.Messaging;
using EnvelopeGenerator.Application.DTOs.Receiver;
+using EnvelopeGenerator.Application.Extensions;
using EnvelopeGenerator.Domain.Entities;
+using EnvelopeGenerator.Domain.HttpResponse;
namespace EnvelopeGenerator.Application.MappingProfiles
{
@@ -43,13 +46,20 @@ namespace EnvelopeGenerator.Application.MappingProfiles
CreateMap();
CreateMap();
CreateMap();
- CreateMap();
+ CreateMap().ForMember(rcv => rcv.EnvelopeReceivers, rcvReadDto => rcvReadDto.Ignore());
CreateMap();
CreateMap();
CreateMap();
CreateMap();
CreateMap();
CreateMap();
+
+ // Messaging mappings
+ // for GTX messaging
+ CreateMap()
+ .ConstructUsing(gtxRes => gtxRes.Ok()
+ ? new SmsResponse() { Ok = true }
+ : new SmsResponse() { Ok = false, Errors = gtxRes });
}
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Resources/Resource.de-DE.resx b/EnvelopeGenerator.Application/Resources/Resource.de-DE.resx
index 244fbc28..6a951a19 100644
--- a/EnvelopeGenerator.Application/Resources/Resource.de-DE.resx
+++ b/EnvelopeGenerator.Application/Resources/Resource.de-DE.resx
@@ -159,27 +159,60 @@
Das digitale Unterschriftenportal ist eine Plattform, die entwickelt wurde, um Ihre Dokumente sicher zu unterschreiben und zu verwalten. Mit seiner benutzerfreundlichen Oberfläche können Sie Ihre Dokumente schnell hochladen, die Unterschriftsprozesse verfolgen und Ihre digitalen Unterschriftenanwendungen einfach durchführen. Dieses Portal beschleunigt Ihren Arbeitsablauf mit rechtlich gültigen Unterschriften und erhöht gleichzeitig die Sicherheit Ihrer Dokumente.
-
- Öffnen
-
Bitte überprüfen Sie die Standortinformationen. Wenn sie falsch sind, korrigieren Sie diese bitte.
-
- Zugriffscode
-
-
+
Wir haben Ihnen gerade den Zugriffscode an die hinterlegte Email Adresse gesendet. Dies kann evtl. einige Minuten dauern.
-
+
+ Ihr QR-Code ist bis {0} gültig.
+
+
+ Wir haben den QR-Code an Ihre E-Mail-Adresse gesendet. Ihr QR-Code ist bis {0} gültig. Sie können ihn für alle Umschläge verwenden, die Sie an diese E-Mail-Adresse erhalten.
+
+
+ Wir haben soeben den Zugangscode als SMS an die von Ihnen angegebene Telefonnummer gesendet.
+
+
+ Zugriffscode
+
+
+ TOTP
+
+
+ SMS-Code
+
+
Bitte überprüfen Sie Ihr Email Postfach inklusive Spam-Ordner. Sie können auch den Absender bitten, Ihnen den Code auf anderem Wege zukommen zu lassen.
-
+
+ Der neue QR-Code wird nur einmal für einen bestimmten Zeitraum gesendet und nach dem Scannen in Ihrer Authenticator-App gespeichert. Er kann für alle Umschläge verwendet werden, die an dieselbe E-Mail-Adresse gesendet werden, bis er abläuft. Wenn Sie die QR-Code-Mail nicht erhalten oder sie sowohl aus der Mail als auch aus authenticator löschen, kontaktieren Sie bitte den Absender.
+
+
+ Sie können den Absender bitten, Ihre Rufnummer zu überprüfen. Die Telefonnummer muss mit der Ortsvorwahl eingegeben werden. Andernfalls können Sie beantragen, den Zwei-Faktor-Schutz zu entfernen.
+
+
Sie haben keinen Zugriffscode erhalten?
-
+
+ Sie haben keinen QR-Code erhalten?
+
+
+ Sie haben keine SMS erhalten?
+
+
Dokument erfordert einen Zugriffscode
+
+ 2-Faktor-Authentifizierung
+
+
+ 2-Faktor-Authentifizierung
+
+
+ Datenschutz
+
Weitergeleitet von {0}. Gültig bis {1}.
diff --git a/EnvelopeGenerator.Application/Resources/Resource.en-US.resx b/EnvelopeGenerator.Application/Resources/Resource.en-US.resx
index b99951ec..4ae7197e 100644
--- a/EnvelopeGenerator.Application/Resources/Resource.en-US.resx
+++ b/EnvelopeGenerator.Application/Resources/Resource.en-US.resx
@@ -159,27 +159,60 @@
The Digital Signature Portal is a platform developed for securely signing and managing your documents. With its user-friendly interface, you can quickly upload your documents, track the signing processes, and easily carry out your digital signature applications. This portal accelerates your workflow with legally valid signatures while enhancing the security of your documents.
-
- Open
-
Please review the location information. If it is incorrect, kindly make the necessary corrections.
-
- Access Code
-
-
+
We have just sent you the access code to the email address you provided. This may take a few minutes.
-
+
+ Your QR code is valid until {0}.
+
+
+ We have sent the QR code to your e-mail address. Your QR code is valid until {0}. You can use it for all envelopes received at this email address.
+
+
+ We have just sent the access code as an SMS to the phone number you provided.
+
+
+ Access Code
+
+
+ TOTP
+
+
+ SMS Code
+
+
Please check your email inbox including your spam folder. Furthermore, you can also ask the sender to send the code by other means.
-
+
+ The new QR code is sent only once for a given period and is saved in your authenticator app once scanned. It can be used for all envelopes received at the same email address until it expires. If you do not receive the QR code mail or delete it both from the mail and from authenticator, please contact the sender.
+
+
+ You can ask the sender to check your phone number. The phone number must be entered with the area code. Otherwise you can request to remove the two-factor protection.
+
+
You have not received an access code?
-
+
+ You have not received a QR code?
+
+
+ You have not received an SMS?
+
+
Document requires an access code
+
+ 2-Factor Authentication
+
+
+ 2-Factor Authentication
+
+
+ Privacy
+
Forwarded by {0}. Valid until {1}.
diff --git a/EnvelopeGenerator.Application/Services/CodeGenerator.cs b/EnvelopeGenerator.Application/Services/CodeGenerator.cs
new file mode 100644
index 00000000..89d22522
--- /dev/null
+++ b/EnvelopeGenerator.Application/Services/CodeGenerator.cs
@@ -0,0 +1,66 @@
+using EnvelopeGenerator.Application.Configurations;
+using EnvelopeGenerator.Application.Contracts;
+using Microsoft.Extensions.Options;
+using OtpNet;
+using QRCoder;
+using System.Text;
+
+namespace EnvelopeGenerator.Application.Services
+{
+ public class CodeGenerator : ICodeGenerator
+ {
+ public static Lazy LazyStatic => new(() => new CodeGenerator(Options.Create(new()), new QRCodeGenerator()));
+
+ public static CodeGenerator Static => LazyStatic.Value;
+
+ private readonly CodeGeneratorParams _params;
+
+ private readonly QRCodeGenerator _qrCodeGenerator;
+
+ public CodeGenerator(IOptions options, QRCodeGenerator qrCodeGenerator)
+ {
+ _params = options.Value;
+ _qrCodeGenerator = qrCodeGenerator;
+ }
+
+ public string GenerateCode(int length)
+ {
+ //TODO: Inject Random as a singleton to support multithreading to improve performance.
+ Random random = new();
+
+ if (length <= 0)
+ throw new ArgumentException("Password length must be greater than 0.");
+
+ var passwordBuilder = new StringBuilder(length);
+
+ for (int i = 0; i < length; i++)
+ passwordBuilder.Append(_params.CharPool[random.Next(_params.CharPool.Length)]);
+
+ return passwordBuilder.ToString();
+ }
+
+ public string GenerateTotpSecretKey(int? length = null)
+ => Base32Encoding.ToString(KeyGeneration.GenerateRandomKey(length ?? _params.DefaultTotpSecretKeyLength));
+
+ public byte[] GenerateTotpQrCode(string userEmail, string secretKey, string? issuer = null, string? totpUrlFormat = null, int? pixelsPerModule = null)
+ {
+ var url = string.Format(totpUrlFormat ?? _params.TotpUrlFormat,
+ Uri.EscapeDataString(userEmail),
+ Uri.EscapeDataString(secretKey),
+ Uri.EscapeDataString(issuer ?? _params.TotpIssuer));
+ using var qrCodeData = _qrCodeGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
+ using var qrCode = new BitmapByteQRCode(qrCodeData);
+ return qrCode.GetGraphic(pixelsPerModule ?? _params.TotpQRPixelsPerModule);
+ }
+
+ public byte[] GenerateTotpQrCode(string userEmail, int? length = null, string? issuer = null, string? totpUrlFormat = null, int? pixelsPerModule = null)
+ {
+ return GenerateTotpQrCode(
+ userEmail: userEmail,
+ secretKey: GenerateTotpSecretKey(length: length),
+ issuer: issuer,
+ totpUrlFormat: totpUrlFormat,
+ pixelsPerModule: pixelsPerModule);
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Services/EnvelopeMailService.cs b/EnvelopeGenerator.Application/Services/EnvelopeMailService.cs
index 5e6ca13e..99a77ca0 100644
--- a/EnvelopeGenerator.Application/Services/EnvelopeMailService.cs
+++ b/EnvelopeGenerator.Application/Services/EnvelopeMailService.cs
@@ -12,6 +12,8 @@ using static EnvelopeGenerator.Common.Constants;
using EnvelopeGenerator.Extensions;
using EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly;
using EnvelopeGenerator.Application.Configurations;
+using EnvelopeGenerator.Application.Extensions;
+using Newtonsoft.Json;
namespace EnvelopeGenerator.Application.Services
{
@@ -22,17 +24,19 @@ namespace EnvelopeGenerator.Application.Services
private readonly DispatcherConfig _dConfig;
private readonly IConfigService _configService;
private readonly Dictionary _placeholders;
+ private readonly ICodeGenerator _codeGenerator;
- public EnvelopeMailService(IEmailOutRepository repository, IMapper mapper, IEmailTemplateService tempService, IEnvelopeReceiverService envelopeReceiverService, IOptions dispatcherConfigOptions, IConfigService configService, IOptions mailConfig) : base(repository, mapper)
+ public EnvelopeMailService(IEmailOutRepository repository, IMapper mapper, IEmailTemplateService tempService, IEnvelopeReceiverService envelopeReceiverService, IOptions dispatcherConfigOptions, IConfigService configService, IOptions mailConfig, ICodeGenerator codeGenerator) : base(repository, mapper)
{
_tempService = tempService;
_envRcvService = envelopeReceiverService;
_dConfig = dispatcherConfigOptions.Value;
_configService = configService;
_placeholders = mailConfig.Value.Placeholders;
+ _codeGenerator = codeGenerator;
}
- private async Task> CreatePlaceholders(string? accessCode = null, EnvelopeReceiverDto? envelopeReceiverDto = null, EnvelopeReceiverReadOnlyDto? readOnlyDto = null)
+ private async Task> CreatePlaceholders(string? accessCode = null, EnvelopeReceiverDto? envelopeReceiverDto = null)
{
if (accessCode is not null)
_placeholders["[DOCUMENT_ACCESS_CODE]"] = accessCode;
@@ -63,10 +67,8 @@ namespace EnvelopeGenerator.Application.Services
return _placeholders;
}
-
- public async Task> SendAccessCodeAsync(EnvelopeReceiverDto dto) => await SendAsync(dto: dto, tempType: Constants.EmailTemplateType.DocumentAccessCodeReceived);
-
- public async Task> SendAsync(EnvelopeReceiverDto dto, Constants.EmailTemplateType tempType)
+
+ public async Task> SendAsync(EnvelopeReceiverDto dto, EmailTemplateType tempType, Dictionary? optionalPlaceholders = null)
{
var tempSerResult = await _tempService.ReadByNameAsync(tempType);
if (tempSerResult.IsFailed)
@@ -104,14 +106,19 @@ namespace EnvelopeGenerator.Application.Services
var placeholders = await CreatePlaceholders(accessCode: accessCode, envelopeReceiverDto: dto);
+ // Add optional place holders.
+ if (optionalPlaceholders is not null)
+ foreach (var oph in optionalPlaceholders)
+ placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
+
//TODO: remove the requirement to add the models using reflections
return await CreateWithTemplateAsync(createDto: mail,placeholders: placeholders,
dto, dto.Envelope.User!, dto.Envelope);
}
- public async Task> SendAsync(EnvelopeReceiverReadOnlyDto dto)
+ public async Task> SendAsync(EnvelopeReceiverReadOnlyDto dto, Dictionary? optionalPlaceholders = null)
{
- var tempSerResult = await _tempService.ReadByNameAsync(Constants.EmailTemplateType.DocumentShared);
+ var tempSerResult = await _tempService.ReadByNameAsync(EmailTemplateType.DocumentShared);
if (tempSerResult.IsFailed)
return tempSerResult.ToFail().Notice(LogLevel.Error, Flag.DataIntegrityIssue, $"The email cannot send because '{Constants.EmailTemplateType.DocumentShared}' template cannot found.");
var temp = tempSerResult.Data;
@@ -140,7 +147,32 @@ namespace EnvelopeGenerator.Application.Services
var placeholders = await CreatePlaceholders(readOnlyDto: dto);
+ // Add optional place holders.
+ if (optionalPlaceholders is not null)
+ foreach (var oph in optionalPlaceholders)
+ placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
+
return await CreateWithTemplateAsync(createDto: mail, placeholders: placeholders, dto.Envelope);
}
+
+ public async Task> SendAccessCodeAsync(EnvelopeReceiverDto dto) => await SendAsync(dto: dto, tempType: EmailTemplateType.DocumentAccessCodeReceived);
+
+ public Task> SendTFAQrCodeAsync(EnvelopeReceiverDto dto)
+ {
+ // Check if receiver or secret key is null
+ if (dto.Receiver is null)
+ throw new ArgumentNullException(nameof(dto), $"TFA Qr Code cannot sent. Receiver information is missing. Envelope receiver dto is {JsonConvert.SerializeObject(dto)}");
+ if (dto.Receiver.TotpSecretkey is null)
+ throw new ArgumentNullException(nameof(dto), $"TFA Qr Code cannot sent. Receiver.TotpSecretKey is null. Envelope receiver dto is {JsonConvert.SerializeObject(dto)}");
+ if (dto.Receiver.TotpExpiration is null)
+ throw new ArgumentNullException(nameof(dto), $"TFA Qr Code cannot sent. Receiver.TotpExpiration is null. Envelope receiver dto is {JsonConvert.SerializeObject(dto)}");
+
+ var totp_qr_64 = _codeGenerator.GenerateTotpQrCode(userEmail: dto.Receiver.EmailAddress, secretKey: dto.Receiver.TotpSecretkey).ToBase64String();
+ return SendAsync(dto, EmailTemplateType.TotpSecret, new()
+ {
+ {"[TFA_QR_CODE]", totp_qr_64 },
+ {"[TFA_EXPIRATION]", dto.Receiver.TotpExpiration }
+ });
+ }
}
}
\ 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/EnvelopeReceiverService.cs b/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs
index 12b5ea11..ee3e07fe 100644
--- a/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs
+++ b/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs
@@ -9,6 +9,7 @@ using EnvelopeGenerator.Infrastructure.Contracts;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using EnvelopeGenerator.Extensions;
+using EnvelopeGenerator.Application.DTOs.Messaging;
namespace EnvelopeGenerator.Application.Services
{
@@ -16,33 +17,36 @@ namespace EnvelopeGenerator.Application.Services
{
private readonly IStringLocalizer _localizer;
- public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer localizer, IMapper mapper)
+ private readonly IMessagingService _messagingService;
+
+ public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer localizer, IMapper mapper, IMessagingService messagingService)
: base(repository, mapper)
{
_localizer = localizer;
+ _messagingService = messagingService;
}
- public async Task>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true)
+ public async Task>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true, bool readOnly = true)
{
- var env_rcvs = await _repository.ReadBySignatureAsync(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
+ var env_rcvs = await _repository.ReadBySignatureAsync(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
return Result.Success(_mapper.Map>(env_rcvs));
}
- public async Task>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false)
+ public async Task>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false, bool readOnly = true)
{
- var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver);
+ var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
return Result.Success(_mapper.Map>(env_rcvs));
}
- public async Task>> ReadSecretByUuidAsync(string uuid, bool withEnvelope = false, bool withReceiver = true)
+ public async Task>> ReadAccessCodeByUuidAsync(string uuid, bool withEnvelope = false, bool withReceiver = true)
{
var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver);
- return Result.Success(_mapper.Map>(env_rcvs));
+ return Result.Success(env_rcvs.Select(er => er.AccessCode));
}
- public async Task> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true)
+ public async Task> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
{
- var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
+ var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
if (env_rcv is null)
return Result.Fail()
.Message(Key.EnvelopeReceiverNotFound);
@@ -50,7 +54,17 @@ namespace EnvelopeGenerator.Application.Services
return Result.Success(_mapper.Map(env_rcv));
}
- public async Task> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true)
+ public async Task> ReadWithSecretByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
+ {
+ var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
+ if (env_rcv is null)
+ return Result.Fail()
+ .Message(Key.EnvelopeReceiverNotFound);
+
+ return Result.Success(_mapper.Map(env_rcv));
+ }
+
+ public async Task> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
{
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
@@ -61,7 +75,7 @@ namespace EnvelopeGenerator.Application.Services
.Notice(LogLevel.Warning, EnvelopeFlag.WrongEnvelopeReceiverId)
.Notice(LogLevel.Warning, Flag.PossibleSecurityBreach);
- return await ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
+ return await ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
}
public async Task> VerifyAccessCodeAsync(string uuid, string signature, string accessCode)
@@ -135,5 +149,31 @@ namespace EnvelopeGenerator.Application.Services
var er = await _repository.ReadLastByReceiver(mail);
return er is null ? Result.Fail().Notice(LogLevel.None, Flag.NotFound) : Result.Success(er.Name);
}
+
+ public async Task> SendSmsAsync(string envelopeReceiverId, string message)
+ {
+ (string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
+
+ if (uuid is null || signature is null)
+ return Result.Fail()
+ .Message(_localizer[Key.WrongEnvelopeReceiverId])
+ .Notice(LogLevel.Warning, (uuid, signature).ToTitle())
+ .Notice(LogLevel.Warning, EnvelopeFlag.WrongEnvelopeReceiverId)
+ .Notice(LogLevel.Warning, Flag.PossibleSecurityBreach);
+
+ var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: false, withReceiver: false);
+ if (env_rcv is null)
+ return Result.Fail()
+ .Message(Key.EnvelopeReceiverNotFound);
+
+ if (env_rcv.PhoneNumber is null)
+ return Result.Fail()
+ .Message(Key.PhoneNumberNonexists)
+ .Notice(LogLevel.Error, Flag.NotFound, $"An attempt was made to send sms to the user whose phone number is null. Envelope recipient ID is {envelopeReceiverId}, UUID is {uuid} and signature is {signature}.");
+
+ var res = await _messagingService.SendSmsAsync(recipient: env_rcv.PhoneNumber, message: message);
+
+ return Result.Success(res);
+ }
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs
new file mode 100644
index 00000000..17d179eb
--- /dev/null
+++ b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs
@@ -0,0 +1,70 @@
+using AutoMapper;
+using DigitalData.Core.Abstractions.Client;
+using DigitalData.Core.Client;
+using EnvelopeGenerator.Application.Configurations.GtxMessaging;
+using EnvelopeGenerator.Application.Contracts;
+using EnvelopeGenerator.Application.DTOs.Messaging;
+using EnvelopeGenerator.Application.Extensions;
+using EnvelopeGenerator.Domain.HttpResponse;
+using Microsoft.Extensions.Caching.Distributed;
+using Microsoft.Extensions.Options;
+
+namespace EnvelopeGenerator.Application.Services
+{
+ public class GtxMessagingService : IMessagingService
+ {
+ private readonly IHttpClientService _smsClient;
+
+ private readonly SmsParams _smsParams;
+
+ private readonly IMapper _mapper;
+
+ private readonly ICodeGenerator _codeGen;
+
+ private readonly IEnvelopeReceiverCache _erCache;
+
+ public string ServiceProvider { get; }
+
+ 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;
+ _erCache = envelopeReceiverCache;
+ }
+
+ public async Task SendSmsAsync(string recipient, string message)
+ {
+ return await _smsClient.FetchAsync(queryParams: new Dictionary()
+ {
+ { _smsParams.RecipientQueryParamName, recipient },
+ { _smsParams.MessageQueryParamName, message }
+ })
+ .ThenAsync(res => res.Json())
+ .ThenAsync(_mapper.Map);
+ }
+
+ public async Task SendSmsCodeAsync(string recipient, string envelopeReceiverId)
+ {
+ var code = await _erCache.GetSmsCodeAsync(envelopeReceiverId);
+
+ if (code is null)
+ {
+ 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 _erCache.GetSmsCodeExpirationAsync(envelopeReceiverId);
+ return code_expiration is null
+ ? new() { Ok = false }
+ : new() { Ok = false, AllowedAt = code_expiration };
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Services/ReceiverService.cs b/EnvelopeGenerator.Application/Services/ReceiverService.cs
index 422f5c0f..97015953 100644
--- a/EnvelopeGenerator.Application/Services/ReceiverService.cs
+++ b/EnvelopeGenerator.Application/Services/ReceiverService.cs
@@ -5,6 +5,8 @@ using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Infrastructure.Contracts;
using EnvelopeGenerator.Application.DTOs.Receiver;
using DigitalData.Core.DTO;
+using DigitalData.Core.Abstractions;
+using Microsoft.Extensions.Logging;
namespace EnvelopeGenerator.Application.Services
{
@@ -34,5 +36,17 @@ namespace EnvelopeGenerator.Application.Services
return await _repository.DeleteAsync(rcv) ? Result.Success() : Result.Fail();
}
+
+ public virtual async Task UpdateAsync(TUpdateDto updateDto) where TUpdateDto : IUnique
+ {
+ var val = await _repository.ReadByIdAsync(updateDto.Id);
+ if (val == null)
+ {
+ return Result.Fail().Notice(LogLevel.Warning, Flag.NotFound, $"{updateDto.Id} is not found in update process of {GetType()} entity.");
+ }
+
+ var entity = _mapper.Map(updateDto, val);
+ return (await _repository.UpdateAsync(entity)) ? Result.Success() : Result.Fail();
+ }
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Common/Constants.vb b/EnvelopeGenerator.Common/Constants.vb
index 969aac71..2fd6c739 100644
--- a/EnvelopeGenerator.Common/Constants.vb
+++ b/EnvelopeGenerator.Common/Constants.vb
@@ -100,6 +100,7 @@
DocumentCompleted
DocumentAccessCodeReceived
DocumentShared
+ TotpSecret
End Enum
Public Enum EncodeType
diff --git a/EnvelopeGenerator.Domain/Entities/Config.cs b/EnvelopeGenerator.Domain/Entities/Config.cs
index 6621beb5..0f5d48e5 100644
--- a/EnvelopeGenerator.Domain/Entities/Config.cs
+++ b/EnvelopeGenerator.Domain/Entities/Config.cs
@@ -1,6 +1,7 @@
using DigitalData.Core.Abstractions;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
+using System.Text.Json.Serialization;
namespace EnvelopeGenerator.Domain.Entities
{
@@ -23,19 +24,9 @@ namespace EnvelopeGenerator.Domain.Entities
[Column("EXPORT_PATH", TypeName = "nvarchar(256)")]
public string? ExportPath { get; init; }
- [Column("DOCUMENT_PATH_DMZ", TypeName = "nvarchar(512)")]
- [Required]
- public string? DocumentPathDmz { get; init; }
-
- [Column("EXPORT_PATH_DMZ", TypeName = "nvarchar(512)")]
- [Required]
- public required string ExportPathDmz { get; init; }
-
- [Column("DOCUMENT_PATH_MOVE_AFTSEND", TypeName = "nvarchar(512)")]
- [Required]
- public required string DocumentPathMoveAftsend { get; init; }
-
[Obsolete("Configuration does not have an ID; it represents a single table in the database.")]
+ [NotMapped]
+ [JsonIgnore]
public int Id => throw new InvalidOperationException("This configuration does not support an ID as it represents a single table in the database.");
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Domain/Entities/EnvelopeDocument.cs b/EnvelopeGenerator.Domain/Entities/EnvelopeDocument.cs
index 85d9ce77..97723f0a 100644
--- a/EnvelopeGenerator.Domain/Entities/EnvelopeDocument.cs
+++ b/EnvelopeGenerator.Domain/Entities/EnvelopeDocument.cs
@@ -16,21 +16,10 @@ namespace EnvelopeGenerator.Domain.Entities
[Column("ENVELOPE_ID")]
public int EnvelopeId { get; set; }
- [Required]
- [Column("FILENAME", TypeName = "nvarchar(256)")]
- public required string Filename { get; set; }
-
- [Required]
- [Column("FILEPATH", TypeName = "nvarchar(256)")]
- public required string Filepath { get; set; }
-
[Required]
[Column("ADDED_WHEN", TypeName = "datetime")]
public required DateTime AddedWhen { get; set; }
- [Column("FILENAME_ORIGINAL", TypeName = "nvarchar(256)")]
- public required string FilenameOriginal { get; set; }
-
[Column("BYTE_DATA", TypeName = "varbinary(max)")]
public byte[]? ByteData { get; init; }
diff --git a/EnvelopeGenerator.Domain/Entities/EnvelopeReceiverBase.cs b/EnvelopeGenerator.Domain/Entities/EnvelopeReceiverBase.cs
index ef934a19..64bc6802 100644
--- a/EnvelopeGenerator.Domain/Entities/EnvelopeReceiverBase.cs
+++ b/EnvelopeGenerator.Domain/Entities/EnvelopeReceiverBase.cs
@@ -41,6 +41,18 @@ namespace EnvelopeGenerator.Domain.Entities
[Column("CHANGED_WHEN", TypeName = "datetime")]
public DateTime? ChangedWhen { get; set; }
+ [Column("PHONE_NUMBER")]
+ [StringLength(20)]
+ [RegularExpression(@"^\+[0-9]+$", ErrorMessage = "Phone number must start with '+' followed by digits.")]
+ public string? PhoneNumber { get; set; }
+
+ [Column("TFA_ENABLED", TypeName = "bit")]
+ public bool TFAEnabled { get; set; }
+
+ [NotMapped]
public (int Envelope, int Receiver) Id => (Envelope: EnvelopeId, Receiver: ReceiverId);
+
+ [NotMapped]
+ public bool HasPhoneNumber => PhoneNumber is not null;
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Domain/Entities/Receiver.cs b/EnvelopeGenerator.Domain/Entities/Receiver.cs
index ae39daf4..da0928f8 100644
--- a/EnvelopeGenerator.Domain/Entities/Receiver.cs
+++ b/EnvelopeGenerator.Domain/Entities/Receiver.cs
@@ -24,6 +24,12 @@ namespace EnvelopeGenerator.Domain.Entities
[Column("ADDED_WHEN", TypeName = "datetime")]
public DateTime AddedWhen { get; set; }
+ [Column("TOTP_SECRET_KEY", TypeName = "nvarchar(MAX)")]
+ public string? TotpSecretkey { get; set; }
+
+ [Column("TOTP_EXPIRATION", TypeName = "datetime")]
+ public DateTime? TotpExpiration { get; set; }
+
public IEnumerable? EnvelopeReceivers { get; init; }
}
}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Domain/EnvelopeGenerator.Domain.csproj b/EnvelopeGenerator.Domain/EnvelopeGenerator.Domain.csproj
index 3480802f..50561806 100644
--- a/EnvelopeGenerator.Domain/EnvelopeGenerator.Domain.csproj
+++ b/EnvelopeGenerator.Domain/EnvelopeGenerator.Domain.csproj
@@ -7,7 +7,7 @@
-
+
diff --git a/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs b/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs
new file mode 100644
index 00000000..cc948e52
--- /dev/null
+++ b/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs
@@ -0,0 +1,4 @@
+namespace EnvelopeGenerator.Domain.HttpResponse
+{
+ public class GtxMessagingResponse : Dictionary { }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Extensions/EnvelopeGenerator.Extensions.csproj b/EnvelopeGenerator.Extensions/EnvelopeGenerator.Extensions.csproj
index 8467bc11..f4ea6b21 100644
--- a/EnvelopeGenerator.Extensions/EnvelopeGenerator.Extensions.csproj
+++ b/EnvelopeGenerator.Extensions/EnvelopeGenerator.Extensions.csproj
@@ -10,6 +10,7 @@
+
diff --git a/EnvelopeGenerator.Extensions/StringExtension.cs b/EnvelopeGenerator.Extensions/StringExtension.cs
new file mode 100644
index 00000000..b08a0f66
--- /dev/null
+++ b/EnvelopeGenerator.Extensions/StringExtension.cs
@@ -0,0 +1,14 @@
+using OtpNet;
+
+namespace EnvelopeGenerator.Extensions
+{
+ public static class StringExtension
+ {
+ public static bool IsValidTotp(this string totp, string secret)
+ {
+ var secret_bytes = Base32Encoding.ToBytes(secret);
+ var secret_totp = new Totp(secret_bytes);
+ return secret_totp.VerifyTotp(totp, out _, VerificationWindow.RfcSpecifiedNetworkDelay);
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.GeneratorAPI/EnvelopeGenerator.GeneratorAPI.csproj b/EnvelopeGenerator.GeneratorAPI/EnvelopeGenerator.GeneratorAPI.csproj
index 2684ed2c..51efca53 100644
--- a/EnvelopeGenerator.GeneratorAPI/EnvelopeGenerator.GeneratorAPI.csproj
+++ b/EnvelopeGenerator.GeneratorAPI/EnvelopeGenerator.GeneratorAPI.csproj
@@ -7,7 +7,7 @@
-
+
diff --git a/EnvelopeGenerator.GeneratorAPI/Program.cs b/EnvelopeGenerator.GeneratorAPI/Program.cs
index d95f039b..5d734ea5 100644
--- a/EnvelopeGenerator.GeneratorAPI/Program.cs
+++ b/EnvelopeGenerator.GeneratorAPI/Program.cs
@@ -1,7 +1,7 @@
using DigitalData.Core.API;
using DigitalData.Core.Application;
using DigitalData.UserManager.Application;
-using EnvelopeGenerator.Application;
+using EnvelopeGenerator.Application.Extensions;
using EnvelopeGenerator.Infrastructure;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Localization;
diff --git a/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeReceiverRepository.cs b/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeReceiverRepository.cs
index e471ddf7..ffd56528 100644
--- a/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeReceiverRepository.cs
+++ b/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeReceiverRepository.cs
@@ -5,19 +5,19 @@ namespace EnvelopeGenerator.Infrastructure.Contracts
{
public interface IEnvelopeReceiverRepository : ICRUDRepository
{
- Task> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false);
+ Task> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false, bool readOnly = true);
- Task> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true);
+ Task> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true, bool readOnly = true);
- Task ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true);
+ Task ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true);
- Task ReadAccessCodeAsync(string uuid, string signature);
+ Task ReadAccessCodeAsync(string uuid, string signature, bool readOnly = true);
Task CountAsync(string uuid, string signature);
- Task ReadByIdAsync(int envelopeId, int receiverId);
+ Task ReadByIdAsync(int envelopeId, int receiverId, bool readOnly = true);
- Task ReadAccessCodeByIdAsync(int envelopeId, int receiverId);
+ Task ReadAccessCodeByIdAsync(int envelopeId, int receiverId, bool readOnly = true);
Task> ReadByUsernameAsync(string username, int? min_status = null, int? max_status = null, params int[] ignore_statuses);
diff --git a/EnvelopeGenerator.Infrastructure/EnvelopeGenerator.Infrastructure.csproj b/EnvelopeGenerator.Infrastructure/EnvelopeGenerator.Infrastructure.csproj
index f295ff69..146af618 100644
--- a/EnvelopeGenerator.Infrastructure/EnvelopeGenerator.Infrastructure.csproj
+++ b/EnvelopeGenerator.Infrastructure/EnvelopeGenerator.Infrastructure.csproj
@@ -7,7 +7,7 @@
-
+
diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs
index 7b72f29e..9bfb26e4 100644
--- a/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs
+++ b/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs
@@ -11,9 +11,9 @@ namespace EnvelopeGenerator.Infrastructure.Repositories
{
}
- private IQueryable ReadWhere(string? uuid = null, string? signature = null, bool withEnvelope = false, bool withReceiver = false)
+ private IQueryable ReadWhere(string? uuid = null, string? signature = null, bool withEnvelope = false, bool withReceiver = false, bool readOnly = true)
{
- var query = _dbSet.AsNoTracking();
+ var query = readOnly ? _dbSet.AsNoTracking() : _dbSet;
if (uuid is not null)
query = query.Where(er => er.Envelope != null && er.Envelope.Uuid == uuid);
@@ -32,31 +32,34 @@ namespace EnvelopeGenerator.Infrastructure.Repositories
return query;
}
- public async Task> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false)
- => await ReadWhere(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver).ToListAsync();
+ public async Task> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false, bool readOnly = true)
+ => await ReadWhere(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly).ToListAsync();
- public async Task> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true)
- => await ReadWhere(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver).ToListAsync();
+ public async Task> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true, bool readOnly = true)
+ => await ReadWhere(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly).ToListAsync();
- public async Task ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true)
- => await ReadWhere(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver).FirstOrDefaultAsync();
+ public async Task ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
+ => await ReadWhere(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly).FirstOrDefaultAsync();
- public async Task ReadAccessCodeAsync(string uuid, string signature)
- => await ReadWhere(uuid: uuid, signature: signature)
+ public async Task ReadAccessCodeAsync(string uuid, string signature, bool readOnly = true)
+ => await ReadWhere(uuid: uuid, signature: signature, readOnly: readOnly)
.Select(er => er.AccessCode)
.FirstOrDefaultAsync();
public async Task CountAsync(string uuid, string signature) => await ReadWhere(uuid: uuid, signature: signature).CountAsync();
- public IQueryable ReadById(int envelopeId, int receiverId) => _dbSet.AsNoTracking()
- .Where(er => er.EnvelopeId == envelopeId && er.ReceiverId == receiverId);
+ private IQueryable ReadById(int envelopeId, int receiverId, bool readOnly = true)
+ {
+ var query = readOnly ? _dbSet.AsNoTracking() : _dbSet;
+ return query.Where(er => er.EnvelopeId == envelopeId && er.ReceiverId == receiverId);
+ }
- public async Task ReadByIdAsync(int envelopeId, int receiverId)
- => await ReadById(envelopeId: envelopeId, receiverId: receiverId)
+ public async Task ReadByIdAsync(int envelopeId, int receiverId, bool readOnly = true)
+ => await ReadById(envelopeId: envelopeId, receiverId: receiverId, readOnly: readOnly)
.FirstOrDefaultAsync();
- public async Task ReadAccessCodeByIdAsync(int envelopeId, int receiverId)
- => await ReadById(envelopeId: envelopeId, receiverId: receiverId)
+ public async Task ReadAccessCodeByIdAsync(int envelopeId, int receiverId, bool readOnly = true)
+ => await ReadById(envelopeId: envelopeId, receiverId: receiverId, readOnly: readOnly)
.Select(er => er.AccessCode)
.FirstOrDefaultAsync();
diff --git a/EnvelopeGenerator.Web/Controllers/HomeController.cs b/EnvelopeGenerator.Web/Controllers/HomeController.cs
index ad5687d7..d67d9b86 100644
--- a/EnvelopeGenerator.Web/Controllers/HomeController.cs
+++ b/EnvelopeGenerator.Web/Controllers/HomeController.cs
@@ -18,7 +18,8 @@ using static EnvelopeGenerator.Common.Constants;
using Ganss.Xss;
using Newtonsoft.Json;
using EnvelopeGenerator.Application.DTOs;
-using EnvelopeGenerator.Domain.Entities;
+using DigitalData.Core.Client;
+using EnvelopeGenerator.Application.Extensions;
namespace EnvelopeGenerator.Web.Controllers
{
@@ -34,8 +35,12 @@ namespace EnvelopeGenerator.Web.Controllers
private readonly Cultures _cultures;
private readonly IEnvelopeMailService _mailService;
private readonly IEnvelopeReceiverReadOnlyService _readOnlyService;
+ private readonly IMessagingService _msgService;
+ private readonly IEnvelopeReceiverCache _erCache;
+ private readonly ICodeGenerator _codeGenerator;
+ private readonly IReceiverService _rcvService;
- public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService)
+ public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, IMessagingService messagingService, IEnvelopeReceiverCache envelopeReceiverCache, ICodeGenerator codeGenerator, IReceiverService receiverService)
{
this.envelopeOldService = envelopeOldService;
_envRcvService = envelopeReceiverService;
@@ -47,6 +52,10 @@ namespace EnvelopeGenerator.Web.Controllers
_mailService = envelopeMailService;
_logger = logger;
_readOnlyService = readOnlyService;
+ _msgService = messagingService;
+ _erCache = envelopeReceiverCache;
+ _codeGenerator = codeGenerator;
+ _rcvService = receiverService;
}
[HttpGet("/")]
@@ -102,7 +111,7 @@ namespace EnvelopeGenerator.Web.Controllers
bool accessCodeAlreadyRequested = await _historyService.AccessCodeAlreadyRequested(envelopeId: er.Envelope!.Id, userReference: er.Receiver!.EmailAddress);
if (!accessCodeAlreadyRequested)
{
- await _historyService.RecordAsync(er.EnvelopeId, er.Receiver.EmailAddress, Constants.EnvelopeStatus.AccessCodeRequested);
+ await _historyService.RecordAsync(er.EnvelopeId, er.Receiver.EmailAddress, EnvelopeStatus.AccessCodeRequested);
var mailRes = await _mailService.SendAccessCodeAsync(envelopeReceiverDto: er);
if (mailRes.IsFailed)
@@ -134,9 +143,12 @@ namespace EnvelopeGenerator.Web.Controllers
{
ViewData["UserCulture"] = _cultures[UserLanguage];
- return await _envRcvService.IsExisting(envelopeReceiverId: envelopeReceiverId).ThenAsync(
- Success: isExisting => isExisting ? View().WithData("EnvelopeKey", envelopeReceiverId) : this.ViewEnvelopeNotFound(),
- Fail: IActionResult (messages,notices) =>
+ return await _envRcvService.ReadByEnvelopeReceiverIdAsync(envelopeReceiverId: envelopeReceiverId).ThenAsync(
+ Success: er => View()
+ .WithData("EnvelopeKey", envelopeReceiverId)
+ .WithData("TFAEnabled", er.TFAEnabled)
+ .WithData("HasPhoneNumber", er.HasPhoneNumber),
+ Fail: IActionResult (messages, notices) =>
{
_logger.LogNotice(notices);
Response.StatusCode = StatusCodes.Status401Unauthorized;
@@ -151,7 +163,7 @@ namespace EnvelopeGenerator.Web.Controllers
}
[HttpPost("EnvelopeKey/{envelopeReceiverId}/Locked")]
- public async Task LogInEnvelope([FromRoute] string envelopeReceiverId, [FromForm] string access_code)
+ public async Task LogInEnvelope([FromRoute] string envelopeReceiverId, [FromForm] Auth auth)
{
try
{
@@ -166,33 +178,102 @@ namespace EnvelopeGenerator.Web.Controllers
return Unauthorized();
}
- _logger.LogInformation($"Envelope UUID: [{uuid}]\nReceiver Signature: [{signature}]");
+ _logger.LogInformation("Envelope UUID: [{uuid}]\nReceiver Signature: [{signature}]", uuid, signature);
//check access code
EnvelopeResponse response = await envelopeOldService.LoadEnvelope(envelopeReceiverId);
- var verification = await _envRcvService.VerifyAccessCodeAsync(uuid: uuid, signature: signature, accessCode: access_code);
- if (verification.IsFailed)
- {
- _logger.LogNotice(verification.Notices);
- Response.StatusCode = StatusCodes.Status401Unauthorized;
- return View("EnvelopeLocked")
- .WithData("ErrorMessage", _localizer[WebKey.WrongAccessCode].Value);
- }
- return await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature).ThenAsync(
- SuccessAsync: async er =>
+ return await _envRcvService.ReadWithSecretByUuidSignatureAsync(uuid: uuid, signature: signature).ThenAsync(
+ SuccessAsync: async er_secret =>
{
- //check the access code verification
- if (verification.IsWrong())
- {
- //Constants.EnvelopeStatus.AccessCodeIncorrect
- await _historyService.RecordAsync(er.EnvelopeId, er.Receiver!.EmailAddress, Constants.EnvelopeStatus.AccessCodeIncorrect);
- Response.StatusCode = StatusCodes.Status401Unauthorized;
- return View("EnvelopeLocked")
- .WithData("ErrorMessage", _localizer[WebKey.WrongAccessCode].Value);
- }
+ async Task TFAView(bool viaSms)
+ {
+ if (viaSms)
+ {
+ var res = await _msgService.SendSmsCodeAsync(er_secret.PhoneNumber!, envelopeReceiverId: envelopeReceiverId);
+ if (res.Ok)
+ return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.Expiration);
+ else if (!res.Allowed)
+ return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.AllowedAt);
+ else
+ {
+ var res_json = JsonConvert.SerializeObject(res);
+ _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}");
+ return this.ViewInnerServiceError();
+ }
+ }
+ else
+ {
+ return View("EnvelopeLocked").WithData("CodeType", "authenticatorCode").WithData("QRCodeExpiration", er_secret.Receiver?.TotpExpiration);
+ }
+ }
- await _historyService.RecordAsync(er.EnvelopeId, er.Receiver!.EmailAddress, Constants.EnvelopeStatus.AccessCodeCorrect);
+ if (auth.HasMulti)
+ {
+ Response.StatusCode = StatusCodes.Status401Unauthorized;
+ return View("EnvelopeLocked")
+ .WithData("ErrorMessage", _localizer[WebKey.WrongAccessCode].Value);
+ }
+ else if (auth.HasAccessCode)
+ {
+ //check the access code verification
+ if (er_secret.AccessCode != auth.AccessCode)
+ {
+ //Constants.EnvelopeStatus.AccessCodeIncorrect
+ await _historyService.RecordAsync(er_secret.EnvelopeId, er_secret.Receiver!.EmailAddress, EnvelopeStatus.AccessCodeIncorrect);
+ Response.StatusCode = StatusCodes.Status401Unauthorized;
+ return View("EnvelopeLocked")
+ .WithData("ErrorMessage", _localizer[WebKey.WrongAccessCode].Value);
+ }
+
+ await _historyService.RecordAsync(er_secret.EnvelopeId, er_secret.Receiver!.EmailAddress, EnvelopeStatus.AccessCodeCorrect);
+
+ //check if the user has phone is added
+ if (er_secret.TFAEnabled)
+ {
+ var rcv = er_secret.Receiver;
+ if (rcv.IsTotpSecretInvalid())
+ {
+ rcv.TotpSecretkey = _codeGenerator.GenerateTotpSecretKey();
+ rcv.TotpExpiration = DateTime.Now.AddMonths(1);
+ await _rcvService.UpdateAsync(rcv);
+ await _mailService.SendTFAQrCodeAsync(er_secret);
+ }
+ return await TFAView(auth.UserSelectSMS);
+ }
+
+ }
+ else if (auth.HasSmsCode)
+ {
+ var smsCode = await _erCache.GetSmsCodeAsync(envelopeReceiverId);
+ if (smsCode is null)
+ return RedirectToAction("EnvelopeLocked", new { envelopeReceiverId });
+
+ if(auth.SmsCode != smsCode)
+ {
+ Response.StatusCode = StatusCodes.Status401Unauthorized;
+ ViewData["ErrorMessage"] = _localizer[WebKey.WrongAccessCode].Value;
+ return await TFAView(viaSms: true);
+ }
+ }
+ else if (auth.HasAuthenticatorCode)
+ {
+ if (er_secret.Receiver!.IsTotpInvalid(totp: auth.AuthenticatorCode!))
+ {
+ Response.StatusCode = StatusCodes.Status401Unauthorized;
+ ViewData["ErrorMessage"] = _localizer[WebKey.WrongAccessCode].Value;
+ return await TFAView(viaSms: false);
+ }
+ }
+ else
+ {
+ Response.StatusCode = StatusCodes.Status401Unauthorized;
+ return View("EnvelopeLocked")
+ .WithData("ErrorMessage", _localizer[WebKey.WrongAccessCode].Value);
+ }
+
+ //continue the process without important data to minimize security errors.
+ EnvelopeReceiverDto er = er_secret;
ViewData["EnvelopeKey"] = envelopeReceiverId;
//check rejection
diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestCacheController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestCacheController.cs
new file mode 100644
index 00000000..114aab85
--- /dev/null
+++ b/EnvelopeGenerator.Web/Controllers/Test/TestCacheController.cs
@@ -0,0 +1,35 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Caching.Distributed;
+
+namespace EnvelopeGenerator.Web.Controllers.Test
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class TestCacheController : ControllerBase
+ {
+ private readonly IDistributedCache _cache;
+
+ public TestCacheController(IDistributedCache cache)
+ {
+ _cache = cache;
+ }
+
+ [HttpPost]
+ public async Task SetCacheAsync(string key, string value)
+ {
+ var options = new DistributedCacheEntryOptions()
+ .SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
+
+ await _cache.SetStringAsync(key, value, options);
+
+ return Ok();
+ }
+
+ [HttpGet]
+ public async Task GetCacheAsync(string key)
+ {
+ var value = await _cache.GetStringAsync(key);
+ return value is null ? BadRequest() : Ok(value);
+ }
+ }
+}
diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs
new file mode 100644
index 00000000..b0d6ee7a
--- /dev/null
+++ b/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs
@@ -0,0 +1,24 @@
+using EnvelopeGenerator.Application.Contracts;
+using Microsoft.AspNetCore.Mvc;
+
+namespace EnvelopeGenerator.Web.Controllers.Test
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class TestMessagingController : ControllerBase
+ {
+ private readonly IMessagingService _service;
+
+ public TestMessagingController(IMessagingService service)
+ {
+ _service = service;
+ }
+
+ [HttpPost]
+ public async Task SendAsync(string recipient, string message, bool staticResponse = true)
+ {
+ var res = await _service.SendSmsAsync(recipient: recipient, message: message);
+ return res is null? StatusCode(StatusCodes.Status500InternalServerError) : Ok(res);
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj b/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj
index 0d04bfa5..97402542 100644
--- a/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj
+++ b/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj
@@ -5,7 +5,7 @@