Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bunch of string concatenation with usage of StringBulder #55

Open
wants to merge 1 commit into
base: feature/Authentication
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -77,31 +78,31 @@ public static void AddApplicationConfig(this IServiceCollection services, Action

// try to detect as many configuration errors as possible instead of stopping at the first misconfigured property.
// since the happy flow is the norm, it's OK to use just a string to concatenate messages instead of a full-blown list or StringBuilder.
var configErrorsStringBuilder = new StringBuilder();

string? configErrors = null;
if (string.IsNullOrEmpty(validate.ApplicationName))
{
configErrors += "Application name is not defined in the intialization of the application config settings" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Application name is not defined in the initialization of the application config settings");
}

if (string.IsNullOrEmpty(validate.Environment.Name))
{
configErrors += "Application environment name is not defined in the intialization of the application config settings" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Application environment name is not defined in the initialization of the application config settings");
}

if (string.IsNullOrEmpty(validate.Environment.LoggingName))
{
configErrors += "Application environment logging name is not defined in the intialization of the application config settings" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Application environment logging name is not defined in the initialization of the application config settings");
}

if (string.IsNullOrEmpty(validate.Environment.TimeZone))
{
configErrors += "Application environment time zone is not defined in the intialization of the application config settings" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Application environment time zone is not defined in the initialization of the application config settings");
}

if (configErrors is not null)
if (configErrorsStringBuilder.Length > 0)
{
throw new ConfigurationException(configErrors);
throw new ConfigurationException(configErrorsStringBuilder.ToString());
}

services.Configure<ApplicationConfig>(option);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Arc4u.Configuration;
using Arc4u.OAuth2.DataProtection;
using Arc4u.OAuth2.Extensions;
Expand Down Expand Up @@ -185,63 +186,72 @@ public static AuthenticationBuilder AddOidcAuthentication(this IServiceCollectio

var settings = section.Get<OidcAuthenticationSectionOptions>() ?? throw new NullReferenceException($"No section exists with name {authenticationSectionName} in the configuration providers for OpenId Connect authentication.");

string? configErrors = null;
var configErrorsStringBuilder = new StringBuilder();
if (string.IsNullOrWhiteSpace(settings.MetadataAddress))
{
configErrors += "MetadataAddress must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("MetadataAddress must be filled!");
}

if (string.IsNullOrWhiteSpace(settings.CookieName))
{
configErrors += "We need a cookie name defined specifically for your services." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a cookie name defined specifically for your services.");
}

if (string.IsNullOrWhiteSpace(settings.OpenIdSettingsSectionPath))
{
configErrors += "We need a setting section to configure the OpenId Connect." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a setting section to configure the OpenId Connect.");
}

if (string.IsNullOrWhiteSpace(settings.OAuth2SettingsSectionPath))
{
configErrors += "We need a setting section to configure OAuth2." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a setting section to configure OAuth2.");
}

if (string.IsNullOrWhiteSpace(settings.CertificateSectionPath))
{
configErrors += "We need a setting section to specify the certificate to protect your sensitive information." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a setting section to specify the certificate to protect your sensitive information.");
}

if (string.IsNullOrWhiteSpace(settings.DataProtectionSectionPath))
{
configErrors += "We need a setting section to configure the DataProtection cache store." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a setting section to configure the DataProtection cache store.");
}

if (string.IsNullOrWhiteSpace(settings.JwtBearerEventsType))
{
configErrors += "The JwtBearerEventsType must be defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("The JwtBearerEventsType must be defined.");
}

if (string.IsNullOrWhiteSpace(settings.ClaimsIdentifierSectionPath))
{
configErrors += "We need a setting section to specify the claims used to identify a user." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("We need a setting section to specify the claims used to identify a user.");
}

if (configErrors is not null)
if (string.IsNullOrWhiteSpace(settings.CookieAuthenticationEventsType))
{
throw new ConfigurationException(configErrors);
configErrorsStringBuilder.AppendLine("The CookieAuthenticationEventsType must be defined.");
}

var jwtBearerEventsType = Type.GetType(settings.JwtBearerEventsType, false);
if (string.IsNullOrWhiteSpace(settings.OpenIdConnectEventsType))
{
configErrorsStringBuilder.AppendLine("The OpenIdConnectEventsType must be defined.");
}

if (string.IsNullOrWhiteSpace(settings.CookieAuthenticationEventsType))
if (string.IsNullOrWhiteSpace(settings.ResponseType))
{
throw new MissingFieldException("The CookieAuthenticationEventsType must be defined.");
configErrorsStringBuilder.AppendLine("A ResponseType is mandatory to define the OpenId Connect protocol.");
}
var cookieAuthenticationEventsType = Type.GetType(settings.CookieAuthenticationEventsType, true);

if (string.IsNullOrWhiteSpace(settings.OpenIdConnectEventsType))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwining the exception at the end if the setting is missing
I have moved up the check so the error is added to the error list.

Change is that MissingFieldException would not e thrown anymore.

if (configErrorsStringBuilder.Length > 0)
{
throw new MissingFieldException("The OpenIdConnectEventsType must be defined.");
throw new ConfigurationException(configErrorsStringBuilder.ToString());
}
var openIdConnectEventsType = Type.GetType(settings.OpenIdConnectEventsType, false);

var jwtBearerEventsType = Type.GetType(settings.JwtBearerEventsType, false);
var cookieAuthenticationEventsType = Type.GetType(settings.CookieAuthenticationEventsType, true);
var openIdConnectEventsType = Type.GetType(settings.OpenIdConnectEventsType, false);
var certSecurityKey = string.IsNullOrWhiteSpace(settings.CertSecurityKeyPath) ? null : new X509CertificateLoader(null).FindCertificate(configuration, settings.CertSecurityKeyPath) ?? throw new MissingFieldException($"No certificate was found based on the configuration section: {settings.CertSecurityKeyPath}.");

var cert = new X509CertificateLoader(null).FindCertificate(configuration, settings.CertificateSectionPath) ?? throw new MissingFieldException($"No certificate was found based on the configuration section: {settings.CertificateSectionPath}.");

var ticketStoreAction = CacheTicketStoreExtension.PrepareAction(configuration, settings.AuthenticationCacheTicketStorePath);

Type? cookiesConfigureOptionsType;
Expand All @@ -254,11 +264,6 @@ public static AuthenticationBuilder AddOidcAuthentication(this IServiceCollectio
cookiesConfigureOptionsType = Type.GetType(settings.CookiesConfigureOptionsType, true);
}

if (string.IsNullOrWhiteSpace(settings.ResponseType))
{
throw new MissingFieldException("A ResponseType is mandatory to define the OpenId Connect protocol.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwining the exception at the end if the setting is missing
I have moved up the check so the error is added to the error list.

Change is that MissingFieldException would not e thrown anymore.

}

void OidcAuthenticationFiller(OidcAuthenticationOptions options)
{
options.DefaultAuthority = settings.DefaultAuthority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,30 @@ public static SimpleKeyValueSettings ConfigureOAuth2Settings(this IServiceCollec

var validate = new OAuth2SettingsOption();
option(validate);
string? configErrors = null;

var configErrorsStringBuilder = new System.Text.StringBuilder();
if (string.IsNullOrWhiteSpace(validate.ProviderId))
{
configErrors += "ProviderId field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ProviderId field is not defined.");
}

if (string.IsNullOrWhiteSpace(validate.Audiences))
{
configErrors += "Audiences field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Audiences field is not defined.");
}

if (string.IsNullOrWhiteSpace(validate.AuthenticationType))
{
configErrors += "AuthenticationType field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("AuthenticationType field is not defined.");
}

if (configErrors is not null)
if (configErrorsStringBuilder.Length > 0)
{
throw new ConfigurationException(configErrors);
throw new ConfigurationException(configErrorsStringBuilder.ToString());
}

// We map this to a IKeyValuesSettings dictionary.
// The TokenProviders are based on this.

void SettingsFiller(SimpleKeyValueSettings keyOptions)
{
keyOptions.Add(TokenKeys.ProviderIdKey, validate!.ProviderId);
Expand All @@ -49,7 +48,6 @@ void SettingsFiller(SimpleKeyValueSettings keyOptions)
if (!string.IsNullOrWhiteSpace(validate.Authority))
{
keyOptions.Add(TokenKeys.AuthorityKey, validate.Authority);

}
keyOptions.Add(TokenKeys.Audiences, validate.Audiences);
if (!string.IsNullOrWhiteSpace(validate.Scopes))
Expand All @@ -58,15 +56,13 @@ void SettingsFiller(SimpleKeyValueSettings keyOptions)
}
}


services.Configure<SimpleKeyValueSettings>(sectionKey, SettingsFiller);

var settings = new SimpleKeyValueSettings();

SettingsFiller(settings);

return settings;

}

public static SimpleKeyValueSettings ConfigureOAuth2Settings(this IServiceCollection services, IConfiguration configuration, [DisallowNull] string sectionName, [DisallowNull] string sectionKey = "OAuth2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ private static OnBehalfOfSettingsOptions Validate(Action<OnBehalfOfSettingsOptio
var extract = new OnBehalfOfSettingsOptions();
options(extract);

var configErrors = string.Empty;
var configErrorsStringBuilder = new System.Text.StringBuilder();
if (string.IsNullOrWhiteSpace(extract.ClientId))
{
configErrors += "ClientId field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ClientId field is not defined.");
}

if (string.IsNullOrWhiteSpace(extract.ApplicationKey))
{
configErrors += "ApplicationKey field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ApplicationKey field is not defined.");
}

if (string.IsNullOrWhiteSpace(extract.Scope))
{
configErrors += "Scope field is not defined." + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Scope field is not defined.");
}

if (!string.IsNullOrWhiteSpace(configErrors))
if (configErrorsStringBuilder.Length > 0)
{
throw new ConfigurationException(configErrors);
throw new ConfigurationException(configErrorsStringBuilder.ToString());
}

return extract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,26 @@ private static Action<SimpleKeyValueSettings> BuildRemoteSecretsSettings(RemoteS
{
// Check the settings!
// options mandatory fields!
string? configErrors = null;
var configErrorsStringBuilder = new System.Text.StringBuilder();

if (string.IsNullOrWhiteSpace(options.HeaderKey))
{
configErrors += "HeaaderKey in Remote Secret settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("HeaderKey in Remote Secret settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.ClientSecret))
{
configErrors += "ClientSecret in Remote Secret settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ClientSecret in Remote Secret settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.ProviderId))
{
configErrors += "ProviderId in Remote Secret settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ProviderId in Remote Secret settings must be filled!");
}

if (configErrors is not null)
if (configErrorsStringBuilder.Length > 0)
{
throw new ConfigurationException(configErrors);
throw new ConfigurationException(configErrorsStringBuilder.ToString());
}

// We map this to a IKeyValuesSettings dictionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,67 +57,72 @@ private static Action<SimpleKeyValueSettings> BuildBasicSettings(Action<SecretBa
}
private static Action<SimpleKeyValueSettings> BuildBasicSettings(SecretBasicSettingsOptions options)
{
var configErrorsStringBuilder = new System.Text.StringBuilder();

// Check the settings!
// options mandatory fields!
string? configErrors = null;

if (string.IsNullOrWhiteSpace(options.ClientId))
{
configErrors += "ClientId in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ClientId in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.Authority))
{
configErrors += "Authority in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Authority in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.Audience))
{
configErrors += "Audience in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Audience in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.AuthenticationType))
{
configErrors += "AuthenticationType in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("AuthenticationType in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.ProviderId))
{
configErrors += "ProviderId in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("ProviderId in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.Scope))
{
configErrors += "Scope in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("Scope in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.BasicProviderId))
{
configErrors += "BasicProviderId in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine(
"BasicProviderId in Secret Basic settings must be filled!");
}

if (string.IsNullOrWhiteSpace(options.User) && string.IsNullOrWhiteSpace(options.Password) && string.IsNullOrWhiteSpace(options.Credential))
if (string.IsNullOrWhiteSpace(options.User) &&
string.IsNullOrWhiteSpace(options.Password) &&
string.IsNullOrWhiteSpace(options.Credential))
{
configErrors += "User/Password or Credential in Secret Basic settings must be filled!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("User/Password or Credential in Secret Basic settings must be filled!");
}

if (!string.IsNullOrWhiteSpace(options.Password) && !string.IsNullOrWhiteSpace(options.Credential))
{
configErrors += "Password and Credential in Secret Basic settings cannot be filled at the same time!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine(
"Password and Credential in Secret Basic settings cannot be filled at the same time!");
}

if (string.IsNullOrWhiteSpace(options.User) && !string.IsNullOrWhiteSpace(options.Password) && string.IsNullOrWhiteSpace(options.Credential))
{
configErrors += "User in Secret Basic settings must be filled when password is used!" + System.Environment.NewLine;
configErrorsStringBuilder.AppendLine("User in Secret Basic settings must be filled when password is used!");
}

if (configErrors is not null)
if (configErrorsStringBuilder.Length > 0)
{
throw new ConfigurationException(configErrors);
throw new ApplicationException(configErrorsStringBuilder.ToString());
}

// We map this to a IKeyValuesSettings dictionary.
// The TokenProviders are based on this.

void Settings(SimpleKeyValueSettings settings)
{
settings.Add(TokenKeys.ProviderIdKey, options!.ProviderId);
Expand Down
Loading