From b8d6634fcf194b2ba52a9b0ef1756d04bf902a94 Mon Sep 17 00:00:00 2001 From: Daniel Murrmann Date: Fri, 24 Nov 2023 14:08:39 +0100 Subject: [PATCH] Improved error message --- .../Authentication/GatewayAuthentication.cs | 11 +++++++++-- .../Fancy.ResourceLinker.Gateway.csproj | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Fancy.ResourceLinker.Gateway/Authentication/GatewayAuthentication.cs b/src/Fancy.ResourceLinker.Gateway/Authentication/GatewayAuthentication.cs index 899b2c0..7cd7bde 100644 --- a/src/Fancy.ResourceLinker.Gateway/Authentication/GatewayAuthentication.cs +++ b/src/Fancy.ResourceLinker.Gateway/Authentication/GatewayAuthentication.cs @@ -110,8 +110,15 @@ internal static void AddGatewayAuthentication(IServiceCollection services, Gatew if (!string.IsNullOrWhiteSpace(tokenSessionId) && context.Principal != null) { - var sessionIdClaim = new Claim("TokenSessionId", tokenSessionId); - var uniqueNameClaim = context.Principal.Claims.Single(c => c.Type == settings.UniqueIdentifierClaimType); + Claim sessionIdClaim = new Claim("TokenSessionId", tokenSessionId); + Claim? uniqueNameClaim = context.Principal.Claims.SingleOrDefault(c => c.Type == settings.UniqueIdentifierClaimType); + + if(uniqueNameClaim == null) + { + ILogger logger = context.HttpContext.RequestServices.GetRequiredService>(); + logger.LogError($"No claim was found with the specified unique identifier type '{settings.UniqueIdentifierClaimType}'"); + throw new Exception($"No claim was found with the specified unique identifier type '{settings.UniqueIdentifierClaimType}'"); + } // Setup a new default identity which only contians the token session id context.Principal = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { sessionIdClaim, uniqueNameClaim }, context.Principal?.Identity?.AuthenticationType, settings.UniqueIdentifierClaimType, null)); diff --git a/src/Fancy.ResourceLinker.Gateway/Fancy.ResourceLinker.Gateway.csproj b/src/Fancy.ResourceLinker.Gateway/Fancy.ResourceLinker.Gateway.csproj index 31cfd15..d61bf03 100644 --- a/src/Fancy.ResourceLinker.Gateway/Fancy.ResourceLinker.Gateway.csproj +++ b/src/Fancy.ResourceLinker.Gateway/Fancy.ResourceLinker.Gateway.csproj @@ -1,7 +1,7 @@  - 0.0.5 + 0.0.5-local net6.0 enable enable