From b5c2481d0cc6a1e072e6210f6c0c4072c1ba77e0 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 20 Dec 2024 14:37:06 +1100 Subject: [PATCH 1/2] remove IsMissing and move to ArgumentException.ThrowIfNullOrWhiteSpace --- identity-server/src/Storage/Extensions/StringsExtensions.cs | 6 ------ identity-server/src/Storage/IdentityServerUser.cs | 3 +-- identity-server/src/Storage/Models/ApiResource.cs | 2 +- identity-server/src/Storage/Models/ApiScope.cs | 2 +- identity-server/src/Storage/Models/IdentityResource.cs | 2 +- 5 files changed, 4 insertions(+), 11 deletions(-) diff --git a/identity-server/src/Storage/Extensions/StringsExtensions.cs b/identity-server/src/Storage/Extensions/StringsExtensions.cs index 1fd734d1d..1df027228 100644 --- a/identity-server/src/Storage/Extensions/StringsExtensions.cs +++ b/identity-server/src/Storage/Extensions/StringsExtensions.cs @@ -8,12 +8,6 @@ namespace Duende.IdentityServer.Extensions; internal static class StringExtensions { - [DebuggerStepThrough] - public static bool IsMissing(this string value) - { - return string.IsNullOrWhiteSpace(value); - } - [DebuggerStepThrough] public static bool IsPresent(this string value) { diff --git a/identity-server/src/Storage/IdentityServerUser.cs b/identity-server/src/Storage/IdentityServerUser.cs index 6b507f033..e6edb516d 100644 --- a/identity-server/src/Storage/IdentityServerUser.cs +++ b/identity-server/src/Storage/IdentityServerUser.cs @@ -54,7 +54,7 @@ internal class IdentityServerUser /// The subject ID public IdentityServerUser(string subjectId) { - if (subjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(subjectId)); + ArgumentException.ThrowIfNullOrWhiteSpace(subjectId); SubjectId = subjectId; } @@ -66,7 +66,6 @@ public IdentityServerUser(string subjectId) /// public ClaimsPrincipal CreatePrincipal() { - if (SubjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(SubjectId)); var claims = new List { new Claim(JwtClaimTypes.Subject, SubjectId) }; if (DisplayName.IsPresent()) diff --git a/identity-server/src/Storage/Models/ApiResource.cs b/identity-server/src/Storage/Models/ApiResource.cs index a6535bad7..25ae7a0b1 100644 --- a/identity-server/src/Storage/Models/ApiResource.cs +++ b/identity-server/src/Storage/Models/ApiResource.cs @@ -64,7 +64,7 @@ public ApiResource(string name, IEnumerable userClaims) /// name public ApiResource(string name, string? displayName, IEnumerable? userClaims) { - if (name.IsMissing()) throw new ArgumentNullException(nameof(name)); + ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; DisplayName = displayName; diff --git a/identity-server/src/Storage/Models/ApiScope.cs b/identity-server/src/Storage/Models/ApiScope.cs index 4f949a7d4..5e4d9e588 100644 --- a/identity-server/src/Storage/Models/ApiScope.cs +++ b/identity-server/src/Storage/Models/ApiScope.cs @@ -64,7 +64,7 @@ public ApiScope(string name, IEnumerable userClaims) /// name public ApiScope(string name, string displayName, IEnumerable? userClaims) { - if (name.IsMissing()) throw new ArgumentNullException(nameof(name)); + ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; DisplayName = displayName; diff --git a/identity-server/src/Storage/Models/IdentityResource.cs b/identity-server/src/Storage/Models/IdentityResource.cs index fe0f292b2..1e8466950 100644 --- a/identity-server/src/Storage/Models/IdentityResource.cs +++ b/identity-server/src/Storage/Models/IdentityResource.cs @@ -46,7 +46,7 @@ public IdentityResource(string name, IEnumerable userClaims) /// Must provide at least one claim type - claimTypes public IdentityResource(string name, string displayName, IEnumerable userClaims) { - if (name.IsMissing()) throw new ArgumentNullException(nameof(name)); + ArgumentException.ThrowIfNullOrWhiteSpace(name); if (userClaims.IsNullOrEmpty()) throw new ArgumentException("Must provide at least one claim type", nameof(userClaims)); Name = name; From c1950a3069175ee15584cceb16cfa7314bc16abe Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 20 Dec 2024 14:41:41 +1100 Subject: [PATCH 2/2] more ThrowIfNullOrWhiteSpace --- identity-server/src/IdentityServer/IdentityServerUser.cs | 4 +--- .../src/IdentityServer/Models/Contexts/IsActiveContext.cs | 2 +- .../src/IdentityServer/Stores/Default/DefaultGrantStore.cs | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/identity-server/src/IdentityServer/IdentityServerUser.cs b/identity-server/src/IdentityServer/IdentityServerUser.cs index 0527b92a9..5ff87e668 100644 --- a/identity-server/src/IdentityServer/IdentityServerUser.cs +++ b/identity-server/src/IdentityServer/IdentityServerUser.cs @@ -59,8 +59,7 @@ public class IdentityServerUser /// The subject ID public IdentityServerUser(string subjectId) { - if (subjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(subjectId)); - + ArgumentException.ThrowIfNullOrWhiteSpace(subjectId); SubjectId = subjectId; } @@ -71,7 +70,6 @@ public IdentityServerUser(string subjectId) /// public ClaimsPrincipal CreatePrincipal() { - if (SubjectId.IsMissing()) throw new ArgumentException("SubjectId is mandatory", nameof(SubjectId)); var claims = new List { new Claim(JwtClaimTypes.Subject, SubjectId) }; if (DisplayName.IsPresent()) diff --git a/identity-server/src/IdentityServer/Models/Contexts/IsActiveContext.cs b/identity-server/src/IdentityServer/Models/Contexts/IsActiveContext.cs index 087739446..d41874b31 100644 --- a/identity-server/src/IdentityServer/Models/Contexts/IsActiveContext.cs +++ b/identity-server/src/IdentityServer/Models/Contexts/IsActiveContext.cs @@ -22,7 +22,7 @@ public IsActiveContext(ClaimsPrincipal subject, Client client, string caller) { ArgumentNullException.ThrowIfNull(subject); ArgumentNullException.ThrowIfNull(client); - if (caller.IsMissing()) throw new ArgumentNullException(nameof(caller)); + ArgumentNullException.ThrowIfNullOrWhiteSpace(caller); Subject = subject; Client = client; diff --git a/identity-server/src/IdentityServer/Stores/Default/DefaultGrantStore.cs b/identity-server/src/IdentityServer/Stores/Default/DefaultGrantStore.cs index 107cd653e..98dcafcd1 100644 --- a/identity-server/src/IdentityServer/Stores/Default/DefaultGrantStore.cs +++ b/identity-server/src/IdentityServer/Stores/Default/DefaultGrantStore.cs @@ -62,7 +62,7 @@ protected DefaultGrantStore(string grantType, IHandleGenerationService handleGenerationService, ILogger logger) { - if (grantType.IsMissing()) throw new ArgumentNullException(nameof(grantType)); + ArgumentException.ThrowIfNullOrWhiteSpace(grantType); GrantType = grantType; Store = store;