@@ -94,7 +94,7 @@ public async Task<List<Claim>> FromJwtToSamlClaimsAsync(IEnumerable<Claim> jwtCl
9494 {
9595 if ( RouteBinding . AutoMapSamlClaims )
9696 {
97- samlClaims . Add ( new Claim ( AddNewJwtBasedMappingReturnSaml ( mappings , newMappings , jwtClaim . Type ) , jwtClaim . Value , jwtClaim . ValueType , jwtClaim . Issuer , jwtClaim . OriginalIssuer ) ) ;
97+ samlClaims . Add ( new Claim ( await AddNewJwtBasedMappingReturnSamlAsync ( mappings , newMappings , jwtClaim . Type ) , jwtClaim . Value , jwtClaim . ValueType , jwtClaim . Issuer , jwtClaim . OriginalIssuer ) ) ;
9898 }
9999 else
100100 {
@@ -113,8 +113,7 @@ public async Task<List<Claim>> FromJwtToSamlClaimsAsync(IEnumerable<Claim> jwtCl
113113 }
114114 catch ( Exception ex )
115115 {
116- logger . Error ( ex , "Failed to map JWT claims to SAML claims." ) ;
117- throw ;
116+ throw new Exception ( "Failed to map JWT claims to SAML claims." , ex ) ;
118117 }
119118 }
120119
@@ -178,7 +177,7 @@ public async Task<List<Claim>> FromSamlToJwtClaimsAsync(IEnumerable<Claim> samlC
178177 {
179178 if ( RouteBinding . AutoMapSamlClaims )
180179 {
181- jwtClaims . Add ( new Claim ( AddNewSamlBasedMappingReturnJwt ( mappings , newMappings , samlClaim . Type ) , samlClaim . Value , samlClaim . ValueType , samlClaim . Issuer , samlClaim . OriginalIssuer ) ) ;
180+ jwtClaims . Add ( new Claim ( await AddNewSamlBasedMappingReturnJwtAsync ( mappings , newMappings , samlClaim . Type ) , samlClaim . Value , samlClaim . ValueType , samlClaim . Issuer , samlClaim . OriginalIssuer ) ) ;
182181 }
183182 else
184183 {
@@ -198,8 +197,7 @@ public async Task<List<Claim>> FromSamlToJwtClaimsAsync(IEnumerable<Claim> samlC
198197 }
199198 catch ( Exception ex )
200199 {
201- logger . Error ( ex , "Failed to map SAML claims to JWT claims." ) ;
202- throw ;
200+ throw new Exception ( "Failed to map SAML claims to JWT claims." , ex ) ;
203201 }
204202 }
205203
@@ -226,8 +224,7 @@ public List<string> FromSamlToJwtInfoClaimType(string samlClaimType)
226224 }
227225 catch ( Exception ex )
228226 {
229- logger . Error ( ex , "Failed to map SAML claims to JWT claim types." ) ;
230- throw ;
227+ throw new Exception ( "Failed to map SAML claims to JWT claim types." , ex ) ;
231228 }
232229 }
233230
@@ -312,20 +309,30 @@ private List<ClaimMap> GetMappings(RouteBinding RouteBinding, bool toJwtClaims)
312309 }
313310
314311
315- private string AddNewJwtBasedMappingReturnSaml ( List < ClaimMap > mappings , List < ClaimMap > newMappings , string jwtClaim )
312+ private async Task < string > AddNewJwtBasedMappingReturnSamlAsync ( List < ClaimMap > mappings , List < ClaimMap > newMappings , string jwtClaim )
316313 {
314+ var samlClaim = $ "{ Constants . SamlAutoMapClaimTypes . Namespace } { jwtClaim . Replace ( "_" , "" ) } ";
317315 var claimMap = new ClaimMap
318316 {
319317 JwtClaim = jwtClaim . ToLower ( ) ,
320- SamlClaim = $ " { Constants . SamlAutoMapClaimTypes . Namespace } { jwtClaim . Replace ( "_" , "" ) } "
318+ SamlClaim = samlClaim
321319 } ;
322320 mappings . Add ( claimMap ) ;
323- newMappings . Add ( claimMap ) ;
321+
322+ try
323+ {
324+ await claimMap . ValidateObjectAsync ( ) ;
325+ newMappings . Add ( claimMap ) ;
326+ }
327+ catch ( Exception ex )
328+ {
329+ logger . Warning ( ex , $ "Unable to map JWT claim '{ jwtClaim } ' to SAML 2.0 claim '{ samlClaim } '.") ;
330+ }
324331
325332 return claimMap . SamlClaim ;
326333 }
327334
328- private string AddNewSamlBasedMappingReturnJwt ( List < ClaimMap > mappings , List < ClaimMap > newMappings , string samlClaim )
335+ private async Task < string > AddNewSamlBasedMappingReturnJwtAsync ( List < ClaimMap > mappings , List < ClaimMap > newMappings , string samlClaim )
329336 {
330337 string jwtClaim = null ;
331338 var claimSplit = samlClaim . Split ( '/' ) ;
@@ -361,7 +368,16 @@ private string AddNewSamlBasedMappingReturnJwt(List<ClaimMap> mappings, List<Cla
361368 SamlClaim = samlClaim ,
362369 } ;
363370 mappings . Add ( claimMap ) ;
364- newMappings . Add ( claimMap ) ;
371+
372+ try
373+ {
374+ await claimMap . ValidateObjectAsync ( ) ;
375+ newMappings . Add ( claimMap ) ;
376+ }
377+ catch ( Exception ex )
378+ {
379+ logger . Warning ( ex , $ "Unable to map SAML 2.0 claim '{ samlClaim } ' to JWT claim '{ jwtClaim } '.") ;
380+ }
365381
366382 return jwtClaim ;
367383 }
0 commit comments