9
9
using System . Text . RegularExpressions ;
10
10
using System . Threading ;
11
11
using System . Threading . Tasks ;
12
+ using Microsoft . IdentityModel . Abstractions ;
12
13
using Microsoft . IdentityModel . Logging ;
13
14
using Microsoft . IdentityModel . Tokens ;
14
15
using Newtonsoft . Json ;
@@ -158,7 +159,9 @@ public virtual bool CanReadToken(string token)
158
159
159
160
if ( token . Length > MaximumTokenSizeInBytes )
160
161
{
161
- LogHelper . LogInformation ( TokenLogMessages . IDX10209 , LogHelper . MarkAsNonPII ( token . Length ) , LogHelper . MarkAsNonPII ( MaximumTokenSizeInBytes ) ) ;
162
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
163
+ LogHelper . LogInformation ( TokenLogMessages . IDX10209 , LogHelper . MarkAsNonPII ( token . Length ) , LogHelper . MarkAsNonPII ( MaximumTokenSizeInBytes ) ) ;
164
+
162
165
return false ;
163
166
}
164
167
@@ -337,9 +340,12 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
337
340
if ( tokenDescriptor == null )
338
341
throw LogHelper . LogArgumentNullException ( nameof ( tokenDescriptor ) ) ;
339
342
340
- if ( ( tokenDescriptor . Subject == null || ! tokenDescriptor . Subject . Claims . Any ( ) )
341
- && ( tokenDescriptor . Claims == null || ! tokenDescriptor . Claims . Any ( ) ) )
342
- LogHelper . LogWarning ( LogMessages . IDX14114 , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Subject ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Claims ) ) ) ;
343
+ if ( LogHelper . IsEnabled ( EventLogLevel . Warning ) )
344
+ {
345
+ if ( ( tokenDescriptor . Subject == null || ! tokenDescriptor . Subject . Claims . Any ( ) )
346
+ && ( tokenDescriptor . Claims == null || ! tokenDescriptor . Claims . Any ( ) ) )
347
+ LogHelper . LogWarning ( LogMessages . IDX14114 , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Subject ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Claims ) ) ) ;
348
+ }
343
349
344
350
JObject payload ;
345
351
if ( tokenDescriptor . Subject != null )
@@ -354,39 +360,39 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
354
360
355
361
if ( tokenDescriptor . Audience != null )
356
362
{
357
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Aud ) )
363
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Aud ) )
358
364
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . Audience ) ) ) ) ;
359
365
360
366
payload [ JwtRegisteredClaimNames . Aud ] = tokenDescriptor . Audience ;
361
367
}
362
368
363
369
if ( tokenDescriptor . Expires . HasValue )
364
370
{
365
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Exp ) )
371
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Exp ) )
366
372
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . Expires ) ) ) ) ;
367
373
368
374
payload [ JwtRegisteredClaimNames . Exp ] = EpochTime . GetIntDate ( tokenDescriptor . Expires . Value ) ;
369
375
}
370
376
371
377
if ( tokenDescriptor . Issuer != null )
372
378
{
373
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Iss ) )
379
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Iss ) )
374
380
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . Issuer ) ) ) ) ;
375
381
376
382
payload [ JwtRegisteredClaimNames . Iss ] = tokenDescriptor . Issuer ;
377
383
}
378
384
379
385
if ( tokenDescriptor . IssuedAt . HasValue )
380
386
{
381
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Iat ) )
387
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Iat ) )
382
388
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . IssuedAt ) ) ) ) ;
383
389
384
390
payload [ JwtRegisteredClaimNames . Iat ] = EpochTime . GetIntDate ( tokenDescriptor . IssuedAt . Value ) ;
385
391
}
386
392
387
393
if ( tokenDescriptor . NotBefore . HasValue )
388
394
{
389
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Nbf ) )
395
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Nbf ) )
390
396
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . NotBefore ) ) ) ) ;
391
397
392
398
payload [ JwtRegisteredClaimNames . Nbf ] = EpochTime . GetIntDate ( tokenDescriptor . NotBefore . Value ) ;
@@ -698,7 +704,8 @@ private string CreateTokenPrivate(
698
704
}
699
705
catch ( Exception ex )
700
706
{
701
- LogHelper . LogExceptionMessage ( new SecurityTokenException ( LogMessages . IDX14307 , ex ) ) ;
707
+ if ( LogHelper . IsEnabled ( EventLogLevel . Error ) )
708
+ LogHelper . LogExceptionMessage ( new SecurityTokenException ( LogHelper . FormatInvariant ( LogMessages . IDX14307 , ex , payload ) ) ) ;
702
709
}
703
710
704
711
payload = jsonPayload != null ? jsonPayload . ToString ( Formatting . None ) : payload ;
@@ -841,7 +848,9 @@ private static string GetActualIssuer(JsonWebToken jwtToken)
841
848
string actualIssuer = jwtToken . Issuer ;
842
849
if ( string . IsNullOrWhiteSpace ( actualIssuer ) )
843
850
{
844
- LogHelper . LogVerbose ( TokenLogMessages . IDX10244 , ClaimsIdentity . DefaultIssuer ) ;
851
+ if ( LogHelper . IsEnabled ( EventLogLevel . Verbose ) )
852
+ LogHelper . LogVerbose ( TokenLogMessages . IDX10244 , ClaimsIdentity . DefaultIssuer ) ;
853
+
845
854
actualIssuer = ClaimsIdentity . DefaultIssuer ;
846
855
}
847
856
@@ -1136,12 +1145,13 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JsonWebToken jwtToken
1136
1145
var key = ResolveTokenDecryptionKey ( jwtToken . EncodedToken , jwtToken , validationParameters ) ;
1137
1146
if ( key != null )
1138
1147
{
1139
- LogHelper . LogInformation ( TokenLogMessages . IDX10904 , key ) ;
1148
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1149
+ LogHelper . LogInformation ( TokenLogMessages . IDX10904 , key ) ;
1140
1150
}
1141
1151
else if ( configuration != null )
1142
1152
{
1143
1153
key = ResolveTokenDecryptionKeyFromConfig ( jwtToken , configuration ) ;
1144
- if ( key != null )
1154
+ if ( key != null && LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1145
1155
LogHelper . LogInformation ( TokenLogMessages . IDX10905 , key ) ;
1146
1156
}
1147
1157
@@ -1465,7 +1475,8 @@ private async ValueTask<TokenValidationResult> ValidateTokenAsync(JsonWebToken j
1465
1475
{
1466
1476
// The exception is not re-thrown as the TokenValidationParameters may have the issuer and signing key set
1467
1477
// directly on them, allowing the library to continue with token validation.
1468
- LogHelper . LogWarning ( LogHelper . FormatInvariant ( TokenLogMessages . IDX10261 , validationParameters . ConfigurationManager . MetadataAddress , ex . ToString ( ) ) ) ;
1478
+ if ( LogHelper . IsEnabled ( EventLogLevel . Warning ) )
1479
+ LogHelper . LogWarning ( LogHelper . FormatInvariant ( TokenLogMessages . IDX10261 , validationParameters . ConfigurationManager . MetadataAddress , ex . ToString ( ) ) ) ;
1469
1480
}
1470
1481
}
1471
1482
@@ -1740,7 +1751,9 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
1740
1751
{
1741
1752
if ( ValidateSignature ( jwtToken , key , validationParameters ) )
1742
1753
{
1743
- LogHelper . LogInformation ( TokenLogMessages . IDX10242 , jwtToken ) ;
1754
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1755
+ LogHelper . LogInformation ( TokenLogMessages . IDX10242 , jwtToken ) ;
1756
+
1744
1757
jwtToken . SigningKey = key ;
1745
1758
return jwtToken ;
1746
1759
}
@@ -1823,7 +1836,9 @@ internal static bool ValidateSignature(byte[] encodedBytes, byte[] signature, Se
1823
1836
var cryptoProviderFactory = validationParameters . CryptoProviderFactory ?? key . CryptoProviderFactory ;
1824
1837
if ( ! cryptoProviderFactory . IsSupportedAlgorithm ( algorithm , key ) )
1825
1838
{
1826
- LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( algorithm ) , key ) ;
1839
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1840
+ LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( algorithm ) , key ) ;
1841
+
1827
1842
return false ;
1828
1843
}
1829
1844
@@ -1881,7 +1896,9 @@ internal static bool ValidateSignature(JsonWebToken jsonWebToken, SecurityKey ke
1881
1896
var cryptoProviderFactory = validationParameters . CryptoProviderFactory ?? key . CryptoProviderFactory ;
1882
1897
if ( ! cryptoProviderFactory . IsSupportedAlgorithm ( jsonWebToken . Alg , key ) )
1883
1898
{
1884
- LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( jsonWebToken . Alg ) , key ) ;
1899
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1900
+ LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( jsonWebToken . Alg ) , key ) ;
1901
+
1885
1902
return false ;
1886
1903
}
1887
1904
0 commit comments