Skip to content

Commit 8943e6d

Browse files
committed
Enhance parameter validation logic
Replaced `ArgumentNullException.ThrowIfNull` with `ArgumentException.ThrowIfNullOrWhiteSpace` across multiple files to ensure parameters are not null, empty, or whitespace. Updated `SwaggerExtensions.cs` and `OpenApiExtensions.cs` to use this validation for `sectionName`. In `SimpleAuthenticationExtensions.cs`, adjusted `defaultAuthenticationScheme` handling and improved validation for `JwtBearerSettings`, `ApiKeySettings`, and `BasicAuthenticationSettings` properties. These changes prevent runtime errors by ensuring meaningful content in string parameters.
1 parent c80526e commit 8943e6d

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

src/SimpleAuthentication.Swashbuckle/SwaggerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static void AddSimpleAuthentication(this SwaggerGenOptions options, IConf
7777
{
7878
ArgumentNullException.ThrowIfNull(options);
7979
ArgumentNullException.ThrowIfNull(configuration);
80-
ArgumentNullException.ThrowIfNull(sectionName);
80+
ArgumentException.ThrowIfNullOrWhiteSpace(sectionName);
8181

8282
// Adds a security definition for each authentication method that has been configured.
8383
CheckAddJwtBearer(options, configuration.GetSection($"{sectionName}:JwtBearer"));

src/SimpleAuthentication/OpenApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void AddSimpleAuthentication(this OpenApiOptions options, IConfigu
7373
{
7474
ArgumentNullException.ThrowIfNull(options);
7575
ArgumentNullException.ThrowIfNull(configuration);
76-
ArgumentNullException.ThrowIfNull(sectionName);
76+
ArgumentException.ThrowIfNullOrWhiteSpace(sectionName);
7777

7878
options.AddDocumentTransformer(new AuthenticationDocumentTransformer(configuration, sectionName, additionalSecurityRequirements));
7979
options.AddDocumentTransformer<DefaultResponseDocumentTransformer>();

src/SimpleAuthentication/SimpleAuthenticationExtensions.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ public static AuthenticationBuilder AddSimpleAuthentication(this IServiceCollect
3535
{
3636
ArgumentNullException.ThrowIfNull(services);
3737
ArgumentNullException.ThrowIfNull(configuration);
38-
ArgumentNullException.ThrowIfNull(sectionName);
38+
ArgumentException.ThrowIfNullOrWhiteSpace(sectionName);
3939

40-
var defaultAuthenticationScheme = configuration.GetValue<string>($"{sectionName}:DefaultScheme");
40+
var defaultAuthenticationScheme = configuration.GetValue<string?>($"{sectionName}:DefaultScheme");
41+
if (string.IsNullOrWhiteSpace(defaultAuthenticationScheme))
42+
{
43+
defaultAuthenticationScheme = null; // treat empty/whitespace as no value.
44+
}
4145

4246
var builder = services.AddAuthentication(options =>
4347
{
@@ -65,7 +69,7 @@ public static AuthenticationBuilder AddSimpleAuthentication(this AuthenticationB
6569
{
6670
ArgumentNullException.ThrowIfNull(builder);
6771
ArgumentNullException.ThrowIfNull(configuration);
68-
ArgumentNullException.ThrowIfNull(sectionName);
72+
ArgumentException.ThrowIfNullOrWhiteSpace(sectionName);
6973

7074
if (addAuthorizationServices)
7175
{
@@ -86,11 +90,11 @@ static void CheckAddJwtBearer(AuthenticationBuilder builder, IConfigurationSecti
8690
return;
8791
}
8892

89-
ArgumentNullException.ThrowIfNull(settings.SchemeName, nameof(JwtBearerSettings.SchemeName));
90-
ArgumentNullException.ThrowIfNull(settings.SecurityKey, nameof(JwtBearerSettings.SecurityKey));
91-
ArgumentNullException.ThrowIfNull(settings.Algorithm, nameof(JwtBearerSettings.Algorithm));
92-
ArgumentNullException.ThrowIfNull(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
93-
ArgumentNullException.ThrowIfNull(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
93+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.SchemeName, nameof(JwtBearerSettings.SchemeName));
94+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.SecurityKey, nameof(JwtBearerSettings.SecurityKey));
95+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.Algorithm, nameof(JwtBearerSettings.Algorithm));
96+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
97+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
9498

9599
builder.Services.Configure<JwtBearerSettings>(section);
96100

@@ -124,9 +128,9 @@ static void CheckAddApiKey(AuthenticationBuilder builder, IConfigurationSection
124128
return;
125129
}
126130

127-
ArgumentNullException.ThrowIfNull(settings.SchemeName, nameof(ApiKeySettings.SchemeName));
128-
ArgumentNullException.ThrowIfNull(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
129-
ArgumentNullException.ThrowIfNull(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
131+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.SchemeName, nameof(ApiKeySettings.SchemeName));
132+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
133+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
130134

131135
if (string.IsNullOrWhiteSpace(settings.HeaderName) && string.IsNullOrWhiteSpace(settings.QueryStringKey))
132136
{
@@ -135,7 +139,7 @@ static void CheckAddApiKey(AuthenticationBuilder builder, IConfigurationSection
135139

136140
if (!string.IsNullOrWhiteSpace(settings.ApiKeyValue))
137141
{
138-
ArgumentNullException.ThrowIfNull(settings.UserName, nameof(ApiKeySettings.UserName));
142+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.UserName, nameof(ApiKeySettings.UserName));
139143
}
140144

141145
if (settings.ApiKeys.Any(k => string.IsNullOrWhiteSpace(k.Value) || string.IsNullOrWhiteSpace(k.UserName)))
@@ -167,18 +171,18 @@ static void CheckAddBasicAuthentication(AuthenticationBuilder builder, IConfigur
167171
return;
168172
}
169173

170-
ArgumentNullException.ThrowIfNull(settings.SchemeName, nameof(BasicAuthenticationSettings.SchemeName));
171-
ArgumentNullException.ThrowIfNull(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
172-
ArgumentNullException.ThrowIfNull(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
174+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.SchemeName, nameof(BasicAuthenticationSettings.SchemeName));
175+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.NameClaimType, nameof(JwtBearerSettings.NameClaimType));
176+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.RoleClaimType, nameof(JwtBearerSettings.RoleClaimType));
173177

174178
if (!string.IsNullOrWhiteSpace(settings.UserName))
175179
{
176-
ArgumentNullException.ThrowIfNull(settings.Password, nameof(BasicAuthenticationSettings.Password));
180+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.Password, nameof(BasicAuthenticationSettings.Password));
177181
}
178182

179183
if (!string.IsNullOrWhiteSpace(settings.Password))
180184
{
181-
ArgumentNullException.ThrowIfNull(settings.UserName, nameof(BasicAuthenticationSettings.UserName));
185+
ArgumentException.ThrowIfNullOrWhiteSpace(settings.UserName, nameof(BasicAuthenticationSettings.UserName));
182186
}
183187

184188
if (settings.Credentials.Any(c => string.IsNullOrWhiteSpace(c.UserName) || string.IsNullOrWhiteSpace(c.Password)))

0 commit comments

Comments
 (0)