Skip to content

Commit

Permalink
fix: remove reliance on uncaught exceptions (#563)
Browse files Browse the repository at this point in the history
Thanks to @SharePointRadi
  • Loading branch information
SharePointRadi authored Jul 17, 2022
1 parent fe7c01b commit a675684
Showing 1 changed file with 27 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using Finbuckle.MultiTenant;
using Finbuckle.MultiTenant.AspNetCore;
Expand Down Expand Up @@ -103,61 +104,24 @@ public static FinbuckleMultiTenantBuilder<TTenantInfo> WithPerTenantAuthenticati
// Set per-tenant cookie options by convention.
builder.WithPerTenantOptions<CookieAuthenticationOptions>((options, tc) =>
{
var d = (dynamic)tc;
try
{
options.LoginPath = ((string)d.CookieLoginPath).Replace(Constants.TenantToken, tc.Identifier);
}
catch
{
}

try
{
options.LogoutPath = ((string)d.CookieLogoutPath).Replace(Constants.TenantToken, tc.Identifier);
}
catch
{
}

try
{
options.AccessDeniedPath =
((string)d.CookieAccessDeniedPath).Replace(Constants.TenantToken, tc.Identifier);
}
catch
var dynamicTenantInfo = (dynamic)tc;
if (dynamicTenantInfo != null)
{
options.LoginPath = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.CookieLoginPath)) ? ((string)dynamicTenantInfo.CookieLoginPath).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
options.LogoutPath = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.CookieLogoutPath)) ? ((string)dynamicTenantInfo.CookieLogoutPath).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
options.AccessDeniedPath = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.CookieAccessDeniedPath)) ? ((string)dynamicTenantInfo.CookieAccessDeniedPath).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
}
});

// Set per-tenant OpenIdConnect options by convention.
builder.WithPerTenantOptions<OpenIdConnectOptions>((options, tc) =>
{
var d = (dynamic)tc;
try
{
options.Authority =
((string)d.OpenIdConnectAuthority).Replace(Constants.TenantToken, tc.Identifier);
}
catch
{
}

try
{
options.ClientId = ((string)d.OpenIdConnectClientId).Replace(Constants.TenantToken, tc.Identifier);
}
catch
{
}

try
{
options.ClientSecret =
((string)d.OpenIdConnectClientSecret).Replace(Constants.TenantToken, tc.Identifier);
}
catch
var dynamicTenantInfo = (dynamic)tc;
if (dynamicTenantInfo != null)
{
options.Authority = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.OpenIdConnectAuthority)) ? ((string)dynamicTenantInfo.OpenIdConnectAuthority).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
options.ClientId = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.OpenIdConnectClientId)) ? ((string)dynamicTenantInfo.OpenIdConnectClientId).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
options.ClientSecret = HasPropertyWithValidValue(dynamicTenantInfo, nameof(dynamicTenantInfo.OpenIdConnectClientSecret)) ? ((string)dynamicTenantInfo.OpenIdConnectClientSecret).Replace(Constants.TenantToken, tc.Identifier) : string.Empty;
}
});

Expand All @@ -170,6 +134,22 @@ public static FinbuckleMultiTenantBuilder<TTenantInfo> WithPerTenantAuthenticati
}

return builder;

bool HasPropertyWithValidValue(dynamic entity, string propertyName)
{
var property = entity.GetType().GetProperty(propertyName);
if (property != null)
{
if (string.IsNullOrWhiteSpace(property.GetValue(entity, null)))
{
return false;
}

return true;
}

return false;
}
}

/// <summary>
Expand Down

0 comments on commit a675684

Please sign in to comment.