From 7a0c8bc3fa9bab9e4e468e10062b329ecb299f84 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 21 Nov 2024 23:14:31 +1100 Subject: [PATCH 1/2] remove redundant Any checks before enumeration --- src/IdentityServer/IdentityServerUser.cs | 7 ++----- .../Validation/Default/TokenValidator.cs | 17 +++++++---------- src/Storage/IdentityServerUser.cs | 7 ++----- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/IdentityServer/IdentityServerUser.cs b/src/IdentityServer/IdentityServerUser.cs index 8fb9acdb3..0527b92a9 100644 --- a/src/IdentityServer/IdentityServerUser.cs +++ b/src/IdentityServer/IdentityServerUser.cs @@ -94,12 +94,9 @@ public ClaimsPrincipal CreatePrincipal() claims.Add(new Claim(JwtClaimTypes.AuthenticationTime, new DateTimeOffset(AuthenticationTime.Value).ToUnixTimeSeconds().ToString())); } - if (AuthenticationMethods.Any()) + foreach (var amr in AuthenticationMethods) { - foreach (var amr in AuthenticationMethods) - { - claims.Add(new Claim(JwtClaimTypes.AuthenticationMethod, amr)); - } + claims.Add(new Claim(JwtClaimTypes.AuthenticationMethod, amr)); } claims.AddRange(AdditionalClaims); diff --git a/src/IdentityServer/Validation/Default/TokenValidator.cs b/src/IdentityServer/Validation/Default/TokenValidator.cs index ec2a51fc6..db70cdb9c 100644 --- a/src/IdentityServer/Validation/Default/TokenValidator.cs +++ b/src/IdentityServer/Validation/Default/TokenValidator.cs @@ -343,19 +343,16 @@ private async Task ValidateJwtAsync(string jwtString, // check the scope format (array vs space delimited string) var scopes = claims.Where(c => c.Type == JwtClaimTypes.Scope).ToArray(); - if (scopes.Any()) + foreach (var scope in scopes) { - foreach (var scope in scopes) + if (scope.Value.Contains(" ")) { - if (scope.Value.Contains(" ")) - { - claims.Remove(scope); + claims.Remove(scope); - var values = scope.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries); - foreach (var value in values) - { - claims.Add(new Claim(JwtClaimTypes.Scope, value)); - } + var values = scope.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries); + foreach (var value in values) + { + claims.Add(new Claim(JwtClaimTypes.Scope, value)); } } } diff --git a/src/Storage/IdentityServerUser.cs b/src/Storage/IdentityServerUser.cs index 9b3f90979..6b507f033 100644 --- a/src/Storage/IdentityServerUser.cs +++ b/src/Storage/IdentityServerUser.cs @@ -84,12 +84,9 @@ public ClaimsPrincipal CreatePrincipal() claims.Add(new Claim(JwtClaimTypes.AuthenticationTime, new DateTimeOffset(AuthenticationTime.Value).ToUnixTimeSeconds().ToString())); } - if (AuthenticationMethods.Any()) + foreach (var amr in AuthenticationMethods) { - foreach (var amr in AuthenticationMethods) - { - claims.Add(new Claim(JwtClaimTypes.AuthenticationMethod, amr)); - } + claims.Add(new Claim(JwtClaimTypes.AuthenticationMethod, amr)); } claims.AddRange(AdditionalClaims); From c8dbeb833f906da7993ff0039aaf9bd19acaa499 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 21 Nov 2024 23:59:07 +1100 Subject: [PATCH 2/2] Update DefaultKeyMaterialService.cs --- .../Services/Default/DefaultKeyMaterialService.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/IdentityServer/Services/Default/DefaultKeyMaterialService.cs b/src/IdentityServer/Services/Default/DefaultKeyMaterialService.cs index 4f37d6180..64f2e649f 100644 --- a/src/IdentityServer/Services/Default/DefaultKeyMaterialService.cs +++ b/src/IdentityServer/Services/Default/DefaultKeyMaterialService.cs @@ -118,10 +118,7 @@ public async Task> GetValidationKeysAsync() foreach (var store in _validationKeysStores) { var validationKeys = await store.GetValidationKeysAsync(); - if (validationKeys.Any()) - { - keys.AddRange(validationKeys); - } + keys.AddRange(validationKeys); } return keys;