Introduced a mechanism to limit repeated access code requests within a configurable time period using `IMemoryCache` and a new `RepeatRequestBlockingPeriod` configuration in `SendAccessCodeParams`. Key changes: - Added `SendAccessCodeParams` configuration to `appsettings.json`. - Updated DI in `DependencyInjection.cs` to include `SendAccessCodeParams`. - Enhanced `SendAccessCodeCommand` with `MultipleRequestsAllowed`. - Updated `SendAccessCodeCommandHandler` to handle caching logic: - Added `IsAccessCodeRequestsDisabled` and `DisableAccessCodeRequests`. - Modified `AuthController` to support `autoGenerated` query parameter. - Improved error handling and documentation. These changes ensure better control over access code requests and prevent abuse by limiting repeated requests within a configurable time window.
151 lines
5.4 KiB
C#
151 lines
5.4 KiB
C#
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
|
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Domain.Exceptions;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace EnvelopeGenerator.Application.EnvelopeReceivers.Commands;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Key"></param>
|
|
public record SendAccessCodeCommand(string Key) : IRequest<SendAccessCodeResult>
|
|
{
|
|
internal bool MultipleRequestsAllowed { get; private set; } = false;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public SendAccessCodeCommand AllowMultipleRequests(bool allow = true)
|
|
{
|
|
MultipleRequestsAllowed = allow;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Business result of an access-code request.
|
|
/// </summary>
|
|
public enum SendAccessCodeResult
|
|
{
|
|
/// <summary>
|
|
/// Access code sent successfully. HTTP: 200 OK.
|
|
/// </summary>
|
|
Sent,
|
|
|
|
/// <summary>
|
|
/// Receiver has already signed this envelope; requesting a code is no longer applicable. HTTP: 409 Conflict.
|
|
/// </summary>
|
|
AlreadySigned,
|
|
|
|
/// <summary>
|
|
/// Envelope does not require an access code. HTTP: 200 OK (or 204 No Content).
|
|
/// </summary>
|
|
NoAccessCodeRequired,
|
|
|
|
/// <summary>
|
|
/// Access code was already requested and the request limit/rule blocks another send. HTTP: 429 Too Many Requests.
|
|
/// </summary>
|
|
RequestLimitExceeded
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class SendAccessCodeCommandHandler(IMediator Sender, IEnvelopeHistoryService HistoryService, IEnvelopeNotificationService NtfService, IOptions<SendAccessCodeParams> options, IMemoryCache Cache) : IRequestHandler<SendAccessCodeCommand, SendAccessCodeResult>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public readonly SendAccessCodeParams Options = options.Value;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<SendAccessCodeResult> Handle(SendAccessCodeCommand request, CancellationToken cancel)
|
|
{
|
|
// read envelope receiver
|
|
var ers = (await Sender.Send(new ReadEnvelopeReceiverQuery
|
|
{
|
|
Key = request.Key,
|
|
}, cancel));
|
|
|
|
if (ers.SingleOrDefault() is not EnvelopeReceiverDto er || er.Envelope is null || er.Receiver is null)
|
|
throw new NotFoundException($"Envelope receiver with key '{request.Key}' not found.");
|
|
|
|
if (!er.Envelope.UseAccessCode)
|
|
return SendAccessCodeResult.NoAccessCodeRequired;
|
|
|
|
var userReference = er.Receiver.EmailAddress;
|
|
|
|
if (await HistoryService.IsSigned(er.EnvelopeId, userReference))
|
|
return SendAccessCodeResult.AlreadySigned;
|
|
|
|
var accessCodeAlreadyRequested = await HistoryService.AccessCodeAlreadyRequested(er.EnvelopeId, userReference);
|
|
if (accessCodeAlreadyRequested && (!request.MultipleRequestsAllowed || await IsAccessCodeRequestsDisabled(request.Key, cancel)))
|
|
return SendAccessCodeResult.RequestLimitExceeded;
|
|
|
|
// create a history record
|
|
if (!accessCodeAlreadyRequested)
|
|
{
|
|
var historyResult = await HistoryService.RecordAsync(er.EnvelopeId, userReference, EnvelopeStatus.AccessCodeRequested);
|
|
if (historyResult.IsFailed)
|
|
{
|
|
throw new ApplicationException($"Failed to record AccessCodeRequested history for envelope {er.EnvelopeId} and receiver {userReference}.");
|
|
}
|
|
}
|
|
|
|
if (er.AccessCode is null)
|
|
throw new DataIntegrityException($"Access code is null for envelope receiver with key '{request.Key}' although use-access-code is true.");
|
|
|
|
await NtfService.Notify(er.Receiver.EmailAddress, new DocumentAccessCodeReceivedNotification()
|
|
{
|
|
DocumentAccessCode = er.AccessCode,
|
|
DocumentTitle = er.Envelope.Title,
|
|
NameReceiver = er.Name ?? string.Empty,
|
|
IsReadAndConfirm = er.Envelope.Type == EnvelopeType.ReadAndConfirm,
|
|
NameSender = er.Envelope.User!.Username,
|
|
}, cancel);
|
|
|
|
await DisableAccessCodeRequests(request.Key, cancel);
|
|
|
|
return SendAccessCodeResult.Sent;
|
|
|
|
}
|
|
|
|
private static string DisableAccessCodeRequestsCacheKeyPrefix => "DisableAccessCodeRequests_";
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeKey"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private Task<bool> IsAccessCodeRequestsDisabled(string envelopeKey, CancellationToken cancellationToken = default)
|
|
{
|
|
bool hasCache = Cache.TryGetValue<bool>(DisableAccessCodeRequestsCacheKeyPrefix + envelopeKey, out var disabled);
|
|
return Task.FromResult(hasCache && disabled);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeKey"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private Task DisableAccessCodeRequests(string envelopeKey, CancellationToken cancellationToken = default)
|
|
{
|
|
Cache.Set(DisableAccessCodeRequestsCacheKeyPrefix + envelopeKey, true, Options.RepeatRequestBlockingPeriod);
|
|
return Task.CompletedTask;
|
|
}
|
|
} |