diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs index 2f3ca908..beac46da 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs @@ -3,26 +3,60 @@ using System.Security.Claims; namespace EnvelopeGenerator.GeneratorAPI.Controllers { + /// + /// Provides extension methods for extracting user information from a . + /// public static class ControllerExtensions { + /// + /// Attempts to retrieve the user's ID from the claims. Returns null if the ID is not found or invalid. + /// + /// The representing the user. + /// The user's ID as an integer, or null if not found or invalid. public static int? GetIdOrDefault(this ClaimsPrincipal user) => int.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier) ?? user.FindFirstValue("sub"), out int result) ? result : null; + /// + /// Retrieves the user's ID from the claims. Throws an exception if the ID is missing or invalid. + /// + /// The representing the user. + /// The user's ID as an integer. + /// Thrown if the user ID claim is missing or invalid. public static int GetId(this ClaimsPrincipal user) => user.GetIdOrDefault() ?? throw new InvalidOperationException("User ID claim is missing or invalid. This may indicate a misconfigured or forged JWT token."); - public static string? GetUsername(this ClaimsPrincipal user) + /// + /// Retrieves the username from the claims, if available. + /// + /// The representing the user. + /// The username as a string, or null if not found. + public static string? GetUsernameOrDefault(this ClaimsPrincipal user) => user.FindFirst(ClaimTypes.Name)?.Value; - - public static string? GetName(this ClaimsPrincipal user) + + /// + /// Retrieves the user's surname (last name) from the claims, if available. + /// + /// The representing the user. + /// The surname as a string, or null if not found. + public static string? GetNameOrDefault(this ClaimsPrincipal user) => user.FindFirst(ClaimTypes.Surname)?.Value; - - public static string? GetPrename(this ClaimsPrincipal user) + + /// + /// Retrieves the user's given name (first name) from the claims, if available. + /// + /// The representing the user. + /// The given name as a string, or null if not found. + public static string? GetPrenameOrDefault(this ClaimsPrincipal user) => user.FindFirst(ClaimTypes.GivenName)?.Value; - - public static string? GetEmail(this ClaimsPrincipal user) + + /// + /// Retrieves the user's email address from the claims, if available. + /// + /// The representing the user. + /// The email address as a string, or null if not found. + public static string? GetEmailOrDefault(this ClaimsPrincipal user) => user.FindFirst(ClaimTypes.Email)?.Value; } } \ No newline at end of file diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs index 6a1a927f..39fc6587 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs @@ -86,12 +86,12 @@ public class EnvelopeReceiverController : ControllerBase { try { - var username = User.GetUsername(); + var username = User.GetUsernameOrDefault(); if (username is null) { _logger.LogError(@"Envelope Receiver dto cannot be sent because username claim is null. Potential authentication and authorization error. The value of other claims are [id: {id}], [username: {username}], [name: {name}], [prename: {prename}], [email: {email}].", - User.GetId(), User.GetUsername(), User.GetName(), User.GetPrename(), User.GetEmail()); + User.GetId(), User.GetUsernameOrDefault(), User.GetNameOrDefault(), User.GetPrenameOrDefault(), User.GetEmailOrDefault()); return StatusCode(StatusCodes.Status500InternalServerError); }