diff --git a/EnvelopeGenerator.Application/DIExtensions.cs b/EnvelopeGenerator.Application/DIExtensions.cs deleted file mode 100644 index 79b33c45..00000000 --- a/EnvelopeGenerator.Application/DIExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using EnvelopeGenerator.Application.Contracts; -using EnvelopeGenerator.Application.Services; -using Microsoft.Extensions.DependencyInjection; -using static EnvelopeGenerator.Common.Constants; - -namespace EnvelopeGenerator.Application -{ - public static class DIExtensions - { - public static IServiceCollection AddHistoryService(this IServiceCollection services, Func? classifier = null) => services - .AddSingleton(classifier ?? EnvelopeHistoryService.DefaultClassifier) - .AddScoped(); - } -} \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs b/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs index b5f328a7..e7e63308 100644 --- a/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs +++ b/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs @@ -6,39 +6,17 @@ namespace EnvelopeGenerator.Web.Controllers { public static class ControllerBaseExtensions { - public static (string EnvelopeUuid, string ReceiverSignature)? GetAuthenticatedEnvelopeDetails(this ControllerBase controller) - { - if(controller?.User?.Identity?.IsAuthenticated ?? false) - { - var envelopeUuid = controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; - var receiverSignature = controller.User.FindFirst(ClaimTypes.Hash)?.Value; - if (!string.IsNullOrEmpty(envelopeUuid) && !string.IsNullOrEmpty(receiverSignature)) - return (EnvelopeUuid: envelopeUuid, ReceiverSignature: receiverSignature); - } - return null; - } + public static string? GetClaimValue(this ControllerBase controller, string claimType) => controller.User.FindFirstValue(claimType); - public static string? GetAuthenticatedEnvelopeUuid(this ControllerBase controller) - { - if (controller?.User?.Identity?.IsAuthenticated ?? false) - { - var envelopeUuid = controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; - if (!string.IsNullOrEmpty(envelopeUuid)) - return envelopeUuid; - } - return null; - } + public static string? GetAuthEnvelopeUuid(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.NameIdentifier); - public static string? GetAuthenticatedReceiverSignature(this ControllerBase controller) - { - if (controller?.User?.Identity?.IsAuthenticated ?? false) - { - var receiverSignature = controller.User.FindFirst(ClaimTypes.Hash)?.Value; - if (!string.IsNullOrEmpty(receiverSignature)) - return receiverSignature; - } - return null; - } + public static string? GetAuthReceiverSignature(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Hash); + + public static string? GetAuthReceiverName(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Name); + + public static string? GetAuthReceiverMail(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Email); + + public static string? GetAuthEnvelopeTitle(this ControllerBase controller) => controller.User.FindFirstValue(EnvelopeClaimTypes.Title); //TODO: integrate localizer for ready-to-use views public static ViewResult ViewError(this Controller controller, ErrorViewModel errorViewModel) => controller.View("_Error", errorViewModel); diff --git a/EnvelopeGenerator.Web/Controllers/DocumentController.cs b/EnvelopeGenerator.Web/Controllers/DocumentController.cs index 1e036de8..c418a143 100644 --- a/EnvelopeGenerator.Web/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.Web/Controllers/DocumentController.cs @@ -52,7 +52,7 @@ namespace EnvelopeGenerator.Web.Controllers { try { - var authSignature = this.GetAuthenticatedReceiverSignature(); + var authSignature = this.GetAuthReceiverSignature(); if (authSignature != envelopeKey.GetReceiverSignature()) return Forbid(); diff --git a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs index b85b7465..3bab3e6b 100644 --- a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs +++ b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs @@ -1,24 +1,43 @@ -using EnvelopeGenerator.Application; +using DigitalData.Core.DTO; +using EnvelopeGenerator.Application; +using EnvelopeGenerator.Application.Contracts; +using DigitalData.Core.DTO; using EnvelopeGenerator.Common; using EnvelopeGenerator.Web.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Text.Encodings.Web; +using EnvelopeGenerator.Application.DTOs.EnvelopeHistory; +using static EnvelopeGenerator.Common.Constants; namespace EnvelopeGenerator.Web.Controllers { [Authorize] + [ApiController] + [Route("api/[controller]")] public class EnvelopeController : BaseController { private readonly EnvelopeOldService envelopeService; private readonly ActionService? actionService; private readonly UrlEncoder _urlEncoder; + private readonly IEnvelopeHistoryService _histService; + private readonly IReceiverService _receiverService; + private readonly IEnvelopeReceiverService _envRcvService; - public EnvelopeController(DatabaseService database, EnvelopeOldService envelope, ILogger logger, UrlEncoder urlEncoder) : base(database, logger) + + public EnvelopeController(DatabaseService database, + EnvelopeOldService envelope, + ILogger logger, UrlEncoder urlEncoder, + IEnvelopeHistoryService envelopeHistoryService, + IReceiverService receiverService, + IEnvelopeReceiverService envelopeReceiverService) : base(database, logger) { envelopeService = envelope; actionService = database?.Services?.actionService; _urlEncoder = urlEncoder; + _histService = envelopeHistoryService; + _receiverService = receiverService; + _envRcvService = envelopeReceiverService; } [NonAction] @@ -49,17 +68,17 @@ namespace EnvelopeGenerator.Web.Controllers } [Authorize] - [HttpPost("api/envelope/{envelopeKey}")] + [HttpPost("{envelopeKey}")] public async Task Update(string envelopeKey, int index) { try { envelopeKey = _urlEncoder.Encode(envelopeKey); - var authSignature = this.GetAuthenticatedReceiverSignature(); + var authSignature = this.GetAuthReceiverSignature(); if (authSignature != envelopeKey.GetReceiverSignature()) - return Forbid(); + return Unauthorized(); // Validate Envelope Key and load envelope envelopeService.EnsureValidEnvelopeKey(envelopeKey); @@ -93,5 +112,45 @@ namespace EnvelopeGenerator.Web.Controllers return StatusCode(StatusCodes.Status500InternalServerError); } } + + [Authorize] + [HttpPost("reject")] + public async Task Reject() + { + try + { + var signature = this.GetAuthReceiverSignature(); + var uuid = this.GetAuthEnvelopeUuid(); + var mail = this.GetAuthReceiverMail(); + if(uuid is null || signature is null || mail is null) + { + _logger.LogEnvelopeError(uuid: uuid, signature: signature, + message: @$"Unauthorized POST request in api\envelope\reject. One of claims, Envelope, signature or mail ({mail}) is null."); + return Unauthorized(); + } + + var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature); + + if (envRcvRes.IsFailed) + { + _logger.LogNotice(envRcvRes.Notices); + return Unauthorized(); + } + + return await _histService.RecordAsync(envRcvRes.Data.EnvelopeId, userReference: mail, EnvelopeStatus.DocumentRejected).ThenAsync( + Success: id => NoContent(), + Fail: IActionResult (mssg, ntc) => + { + _logger.LogEnvelopeError(uuid: uuid, signature: signature, message: "Unexpected error happend in api/envelope/reject"); + _logger.LogNotice(ntc); + return this.ViewInnerServiceError(); + }); + } + catch (Exception e) + { + _logger.LogError(e, "{Message}", e.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } + } } } diff --git a/EnvelopeGenerator.Web/Program.cs b/EnvelopeGenerator.Web/Program.cs index 6751660e..33a9aaf9 100644 --- a/EnvelopeGenerator.Web/Program.cs +++ b/EnvelopeGenerator.Web/Program.cs @@ -95,6 +95,7 @@ try builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -104,7 +105,6 @@ try builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddHistoryService(); //Auto mapping profiles builder.Services.AddAutoMapper(typeof(BasicDtoMappingProfile).Assembly);