diff --git a/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs b/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs index b5b1c27f..875dad1c 100644 --- a/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs +++ b/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using System.Security.Cryptography; using System.Text; namespace EnvelopeGenerator.Application @@ -88,6 +89,22 @@ namespace EnvelopeGenerator.Application return (string.Empty, string.Empty); } + public static long? DecodeEnvelopeReceiverReadOnlyId(this string envelopeReceiverReadOnlyId) + { + if (!envelopeReceiverReadOnlyId.IsBase64String()) + { + return null; + } + byte[] bytes = Convert.FromBase64String(envelopeReceiverReadOnlyId); + string decodedString = System.Text.Encoding.UTF8.GetString(bytes); + string[] parts = decodedString.Split(new string[] { "::" }, StringSplitOptions.None); + + if (parts.Length > 2) + return long.TryParse(parts[1], out long readOnlyId) ? readOnlyId : null; + else + return null; + } + /// /// Gets the envelope UUID from the decoded envelope receiver ID. /// @@ -111,7 +128,16 @@ namespace EnvelopeGenerator.Application return base64String; } - public static void LogEnvelopeError(this ILogger logger, string envelopeReceiverId, Exception? exception = null, string? message = null, params object?[] args) + public static string EncodeEnvelopeReceiverId(this long readOnlyId) + { + string combinedString = $"{Random.Shared.Next()}::{readOnlyId}::{Random.Shared.Next()}"; + byte[] bytes = Encoding.UTF8.GetBytes(combinedString); + string base64String = Convert.ToBase64String(bytes); + + return base64String; + } + + public static void LogEnvelopeError(this ILogger logger, string envelopeReceiverId, Exception? exception = null, string? message = null, params object?[] args) { var sb = new StringBuilder().AppendLine(envelopeReceiverId.DecodeEnvelopeReceiverId().ToTitle()); diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs index 592688de..3d69a6f0 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs @@ -40,13 +40,27 @@ namespace EnvelopeGenerator.Web.Controllers.Test } [HttpGet("decode")] - public IActionResult DecodeEnvelopeReceiverId(string envelopeReceiverId) + public IActionResult DecodeEnvelopeReceiverId(string envelopeReceiverId, bool isReadOnly = false) { - var decoded = envelopeReceiverId.DecodeEnvelopeReceiverId(); - return Ok(new { uuid = decoded.EnvelopeUuid, signature = decoded.ReceiverSignature }); + if (isReadOnly) + { + var (Random1, ReadOnlyId, Random2) = envelopeReceiverId.DecodeEnvelopeReceiverReadOnlyId(); + return Ok(new { random = new string?[] { Random1, Random2 }, readOnlyId = ReadOnlyId }); + } + else + { + var (EnvelopeUuid, ReceiverSignature) = envelopeReceiverId.DecodeEnvelopeReceiverId(); + return Ok(new { uuid = EnvelopeUuid, signature = ReceiverSignature }); + } } [HttpGet("encode")] - public IActionResult EncodeEnvelopeReceiverId(string uuid, string signature) => Ok((uuid, signature).EncodeEnvelopeReceiverId()); + public IActionResult EncodeEnvelopeReceiverId(string? uuid = null, string? signature = null, long? readOnlyId = null) + { + if(readOnlyId is long readOnlyId_long) + return Ok(readOnlyId_long.EncodeEnvelopeReceiverId()); + else + return Ok((uuid ?? string.Empty, signature ?? string.Empty).EncodeEnvelopeReceiverId()); + } } } \ No newline at end of file