From 50ac7570eae9371b074446d6b2ca67f157238d5e Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 3 Feb 2026 10:06:03 +0100 Subject: [PATCH] Refactor GetDocument to unify sender and receiver logic Combined sender and receiver document retrieval into a single GetDocument endpoint. The endpoint now authorizes both Sender and Receiver.FullyAuth roles, handling their logic based on role detection. Sender requires a query parameter; receiver extracts envelope ID from claims and disallows query params. Updated method signature and endpoint documentation. --- .../Controllers/DocumentController.cs | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/EnvelopeGenerator.API/Controllers/DocumentController.cs b/EnvelopeGenerator.API/Controllers/DocumentController.cs index 39e503d7..dc55d7de 100644 --- a/EnvelopeGenerator.API/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.API/Controllers/DocumentController.cs @@ -19,33 +19,39 @@ namespace EnvelopeGenerator.API.Controllers; public class DocumentController(IMediator mediator, ILogger logger) : ControllerBase { /// - /// Returns the document bytes for the specified envelope receiver key. + /// Returns the document bytes receiver. /// /// Encoded envelope key. /// Cancellation token. [HttpGet] - [Authorize(Roles = Role.Sender)] - public async Task GetDocument(ReadDocumentQuery query, CancellationToken cancel) + [Authorize(Roles = $"{Role.Sender},{Role.Receiver.FullyAuth}")] + public async Task GetDocument(CancellationToken cancel, [FromQuery] ReadDocumentQuery? query = null) { - var doc = await mediator.Send(query, cancel); - return doc.ByteData is byte[] docByte - ? File(docByte, "application/octet-stream") - : NotFound("Document is empty."); - } + // Sender: expects query with envelope key + if (User.IsInRole(Role.Sender)) + { + if (query is null) + return BadRequest("Missing document query."); - /// - /// Returns the document bytes for the receiver. - /// - /// Cancellation token. - [HttpGet] - [Authorize(Roles = Role.Receiver.FullyAuth)] - public async Task GetDocument(CancellationToken cancel) - { - var envelopeId = User.GetEnvelopeIdOfReceiver(); + var senderDoc = await mediator.Send(query, cancel); + return senderDoc.ByteData is byte[] senderDocByte + ? File(senderDocByte, "application/octet-stream") + : NotFound("Document is empty."); + } + + // Receiver: resolve envelope id from claims + if (User.IsInRole(Role.Receiver.FullyAuth)) + { + if (query is not null) + return BadRequest("Query parameters are not allowed for receiver role."); + + var envelopeId = User.GetEnvelopeIdOfReceiver(); + var receiverDoc = await mediator.Send(new ReadDocumentQuery { EnvelopeId = envelopeId }, cancel); + return receiverDoc.ByteData is byte[] receiverDocByte + ? File(receiverDocByte, "application/octet-stream") + : NotFound("Document is empty."); + } - var doc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel); - return doc.ByteData is byte[] docByte - ? File(docByte, "application/octet-stream") - : NotFound("Document is empty."); + return Unauthorized(); } } \ No newline at end of file