diff --git a/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs b/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs index 3e5f487b..7e1e3c41 100644 --- a/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs +++ b/EnvelopeGenerator.Application/EnvelopeGeneratorExtensions.cs @@ -1,11 +1,107 @@ -using EnvelopeGenerator.Application.Services; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using System.Text; namespace EnvelopeGenerator.Application { + /// + /// Provides extension methods for decoding and extracting information from an envelope receiver ID. + /// public static class EnvelopeGeneratorExtensions { + /// + /// Validates whether a given string is a correctly formatted Base-64 encoded string. + /// + /// + /// This method checks the string for proper Base-64 formatting, which includes validating + /// the length of the string (must be divisible by 4). It also checks each character to ensure + /// it belongs to the Base-64 character set (A-Z, a-z, 0-9, '+', '/', and '=' for padding). + /// The method ensures that padding characters ('=') only appear at the end of the string and + /// are in a valid configuration (either one '=' at the end if the string's length % 4 is 3, + /// or two '==' if the length % 4 is 2). + /// + /// The Base-64 encoded string to validate. + /// + /// true if the string is a valid Base-64 encoded string; otherwise, false. + /// + /// + /// + /// string testString = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnk="; + /// bool isValid = IsValidBase64String(testString); + /// Console.WriteLine(isValid); // Output: true + /// + /// + public static bool IsBase64String(this string input) + { + // Check if the string is null or empty + if (string.IsNullOrEmpty(input)) + { + return false; + } + + // Replace valid base-64 padding + input = input.Trim(); + int mod4 = input.Length % 4; + if (mod4 > 0) + { + // Base-64 string lengths should be divisible by 4 + return false; + } + + // Check each character to ensure it is valid base-64 + foreach (char c in input) + { + if (!char.IsLetterOrDigit(c) && c != '+' && c != '/' && c != '=') + { + // Invalid character detected + return false; + } + } + + // Ensure no invalid padding scenarios exist + if (input.EndsWith("==") && (input.Length % 4 == 0) || + input.EndsWith("=") && (input.Length % 4 == 3)) + { + return true; + } + + return input.IndexOf('=') == -1; // No padding allowed except at the end + } + + /// + /// Decodes the envelope receiver ID and extracts the envelope UUID and receiver signature. + /// + /// The base64 encoded string containing the envelope UUID and receiver signature. + /// A tuple containing the envelope UUID and receiver signature. + public static (string? EnvelopeUuid, string? ReceiverSignature) DecodeEnvelopeReceiverId(this string envelopeReceiverId) + { + if (!envelopeReceiverId.IsBase64String()) + { + return (null, null); + } + byte[] bytes = Convert.FromBase64String(envelopeReceiverId); + string decodedString = System.Text.Encoding.UTF8.GetString(bytes); + string[] parts = decodedString.Split(new string[] { "::" }, StringSplitOptions.None); + + if (parts.Length > 1) + return (EnvelopeUuid: parts[0], ReceiverSignature: parts[1]); + else + return (string.Empty, string.Empty); + } + + /// + /// Gets the envelope UUID from the decoded envelope receiver ID. + /// + /// The base64 encoded string to decode. + /// The envelope UUID. + public static string? GetEnvelopeUuid(this string envelopeReceiverId) => envelopeReceiverId.DecodeEnvelopeReceiverId().EnvelopeUuid; + + /// + /// Gets the receiver signature from the decoded envelope receiver ID. + /// + /// The base64 encoded string to decode. + /// The receiver signature. + public static string? GetReceiverSignature(this string envelopeReceiverId) => envelopeReceiverId.DecodeEnvelopeReceiverId().ReceiverSignature; + public static void LogEnvelopeError(this ILogger logger, string envelopeEeceiverId, Exception? exception = null, string? message = null, params object?[] args) { var sb = new StringBuilder().AppendLine(envelopeEeceiverId.DecodeEnvelopeReceiverId().ToTitle()); diff --git a/EnvelopeGenerator.Application/Services/EnvelopeGeneratorExtensions.cs b/EnvelopeGenerator.Application/Services/EnvelopeGeneratorExtensions.cs deleted file mode 100644 index a384e2db..00000000 --- a/EnvelopeGenerator.Application/Services/EnvelopeGeneratorExtensions.cs +++ /dev/null @@ -1,102 +0,0 @@ -namespace EnvelopeGenerator.Application.Services -{ - /// - /// Provides extension methods for decoding and extracting information from an envelope receiver ID. - /// - public static class EnvelopeGeneratorExtensions - { - /// - /// Validates whether a given string is a correctly formatted Base-64 encoded string. - /// - /// - /// This method checks the string for proper Base-64 formatting, which includes validating - /// the length of the string (must be divisible by 4). It also checks each character to ensure - /// it belongs to the Base-64 character set (A-Z, a-z, 0-9, '+', '/', and '=' for padding). - /// The method ensures that padding characters ('=') only appear at the end of the string and - /// are in a valid configuration (either one '=' at the end if the string's length % 4 is 3, - /// or two '==' if the length % 4 is 2). - /// - /// The Base-64 encoded string to validate. - /// - /// true if the string is a valid Base-64 encoded string; otherwise, false. - /// - /// - /// - /// string testString = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnk="; - /// bool isValid = IsValidBase64String(testString); - /// Console.WriteLine(isValid); // Output: true - /// - /// - public static bool IsBase64String(this string input) - { - // Check if the string is null or empty - if (string.IsNullOrEmpty(input)) - { - return false; - } - - // Replace valid base-64 padding - input = input.Trim(); - int mod4 = input.Length % 4; - if (mod4 > 0) - { - // Base-64 string lengths should be divisible by 4 - return false; - } - - // Check each character to ensure it is valid base-64 - foreach (char c in input) - { - if (!char.IsLetterOrDigit(c) && c != '+' && c != '/' && c != '=') - { - // Invalid character detected - return false; - } - } - - // Ensure no invalid padding scenarios exist - if (input.EndsWith("==") && (input.Length % 4 == 0) || - input.EndsWith("=") && (input.Length % 4 == 3)) - { - return true; - } - - return input.IndexOf('=') == -1; // No padding allowed except at the end - } - - /// - /// Decodes the envelope receiver ID and extracts the envelope UUID and receiver signature. - /// - /// The base64 encoded string containing the envelope UUID and receiver signature. - /// A tuple containing the envelope UUID and receiver signature. - public static (string? EnvelopeUuid, string? ReceiverSignature) DecodeEnvelopeReceiverId(this string envelopeReceiverId) - { - if (!envelopeReceiverId.IsBase64String()) - { - return (null, null); - } - byte[] bytes = Convert.FromBase64String(envelopeReceiverId); - string decodedString = System.Text.Encoding.UTF8.GetString(bytes); - string[] parts = decodedString.Split(new string[] { "::" }, StringSplitOptions.None); - - if (parts.Length > 1) - return (EnvelopeUuid: parts[0], ReceiverSignature: parts[1]); - else - return (string.Empty, string.Empty); - } - - /// - /// Gets the envelope UUID from the decoded envelope receiver ID. - /// - /// The base64 encoded string to decode. - /// The envelope UUID. - public static string? GetEnvelopeUuid(this string envelopeReceiverId) => envelopeReceiverId.DecodeEnvelopeReceiverId().EnvelopeUuid; - - /// - /// Gets the receiver signature from the decoded envelope receiver ID. - /// - /// The base64 encoded string to decode. - /// The receiver signature. - public static string? GetReceiverSignature(this string envelopeReceiverId) => envelopeReceiverId.DecodeEnvelopeReceiverId().ReceiverSignature; - } -} \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/DocumentController.cs b/EnvelopeGenerator.Web/Controllers/DocumentController.cs index 7313f192..1e036de8 100644 --- a/EnvelopeGenerator.Web/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.Web/Controllers/DocumentController.cs @@ -3,7 +3,7 @@ using EnvelopeGenerator.Common; using EnvelopeGenerator.Web.Services; using EnvelopeGenerator.Application.Contracts; using Microsoft.AspNetCore.Authorization; -using EnvelopeGenerator.Application.Services; +using EnvelopeGenerator.Application; namespace EnvelopeGenerator.Web.Controllers { diff --git a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs index a83926f4..b85b7465 100644 --- a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs +++ b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs @@ -1,4 +1,4 @@ -using EnvelopeGenerator.Application.Services; +using EnvelopeGenerator.Application; using EnvelopeGenerator.Common; using EnvelopeGenerator.Web.Services; using Microsoft.AspNetCore.Authorization; diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeController.cs index bbde679d..d20e901f 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeController.cs @@ -1,6 +1,6 @@ -using EnvelopeGenerator.Application.Contracts; +using EnvelopeGenerator.Application; +using EnvelopeGenerator.Application.Contracts; using EnvelopeGenerator.Application.DTOs; -using EnvelopeGenerator.Application.Services; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Infrastructure.Contracts; using Microsoft.AspNetCore.Mvc; diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs index 32f5e2ef..f3ef1892 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs @@ -2,7 +2,7 @@ using DigitalData.Core.DTO; using EnvelopeGenerator.Application.Contracts; using EnvelopeGenerator.Application.DTOs; -using EnvelopeGenerator.Application.Services; +using EnvelopeGenerator.Application; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Infrastructure.Contracts; using Microsoft.AspNetCore.Mvc;