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

Use UrlHelpers's IsSubdomainOf to check the wildcard domain. #20672

Merged
merged 2 commits into from
Oct 2, 2024
Merged
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
8 changes: 8 additions & 0 deletions framework/src/Volo.Abp.Core/Volo/Abp/Http/UrlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public static class UrlHelpers
{
private const string WildcardSubdomain = "*.";

/// <summary>
/// Check if the subdomain is a subdomain of the domain.
/// The Uri must be absolute URI and the scheme, port, and host must be the same.
/// </summary>
public static bool IsSubdomainOf(string subdomain, string domain)
{
if (Uri.TryCreate(subdomain, UriKind.Absolute, out var subdomainUri) &&
Expand All @@ -17,6 +21,10 @@ public static bool IsSubdomainOf(string subdomain, string domain)
return false;
}

/// <summary>
/// Check if the subdomain is a subdomain of the domain.
/// The Uri must be absolute URI and the scheme, port, and host must be the same.
/// </summary>
public static bool IsSubdomainOf(Uri subdomain, Uri domain)
{
return subdomain.IsAbsoluteUri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.Web;
using Volo.Abp.Uow;

namespace OpenIddict.Demo.Server;

Expand Down Expand Up @@ -116,7 +117,7 @@ public override void PreConfigureServices(ServiceConfigurationContext context)
PreConfigure<AbpOpenIddictWildcardDomainOptions>(options =>
{
options.EnableWildcardDomainSupport = true;
options.WildcardDomainsFormat.Add("https://{0}.abp.io/signin-oidc");
options.WildcardDomainsFormat.Add("https://*.abp.io");
});

PreConfigure<OpenIddictBuilder>(builder =>
Expand Down Expand Up @@ -155,8 +156,19 @@ public override void ConfigureServices(ServiceConfigurationContext context)
});
}

public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
public async override Task OnPreApplicationInitializationAsync(ApplicationInitializationContext context)
{
using var uow = context.ServiceProvider.GetRequiredService<IUnitOfWorkManager>().Begin();
{
var dbContext = await context.ServiceProvider.GetRequiredService<IDbContextProvider<ServerDbContext>>().GetDbContextAsync();
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
await dbContext.Database.MigrateAsync();
}

await uow.CompleteAsync();
}

await context.ServiceProvider
.GetRequiredService<IDataSeeder>()
.SeedAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using OpenIddict.Server;
using Volo.Abp.Text.Formatting;
using Volo.Abp.Http;

namespace Volo.Abp.OpenIddict.WildcardDomains;

Expand All @@ -29,30 +30,24 @@ protected AbpOpenIddictWildcardDomainBase(IOptions<AbpOpenIddictWildcardDomainOp

protected virtual Task<bool> CheckWildcardDomainAsync(string url)
{
Logger.LogDebug("Checking wildcard domain for url: {url}", url);

foreach (var domainFormat in WildcardDomainOptions.WildcardDomainsFormat)
if (WildcardDomainOptions.WildcardDomainsFormat.IsNullOrEmpty())
{
Logger.LogDebug("Checking wildcard domain format: {domainFormat}", domainFormat);
var extractResult = FormattedStringValueExtracter.Extract(url, domainFormat, ignoreCase: true);
if (extractResult.IsMatch)
{
Logger.LogDebug("Wildcard domain found for url: {url}", url);
return Task.FromResult(true);
}
Logger.LogDebug("No wildcard domain format configured.");
return Task.FromResult(false);
}

foreach (var domainFormat in WildcardDomainOptions.WildcardDomainsFormat)
Logger.LogDebug("Checking wildcard domain for url: {url}", url);
foreach (var domain in WildcardDomainOptions.WildcardDomainsFormat.Select(domainFormat => domainFormat.Replace("{0}", "*")))
{
Logger.LogDebug("Checking wildcard domain format: {domainFormat}", domainFormat);
if (domainFormat.Replace("{0}.", "").Equals(url, StringComparison.OrdinalIgnoreCase))
Logger.LogDebug("Checking wildcard domain format: {domain}", domain);
if (UrlHelpers.IsSubdomainOf(url, domain))
{
Logger.LogDebug("Wildcard domain found for url: {url}", url);
Logger.LogDebug("The url: {url} is a wildcard domain of: {domain}", url, domain);
return Task.FromResult(true);
}
}

Logger.LogDebug("Wildcard domain not found for url: {url}", url);
Logger.LogDebug("No wildcard domain found for url: {url}", url);
return Task.FromResult(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public class AbpOpenIddictWildcardDomainOptions
{
public bool EnableWildcardDomainSupport { get; set; }

/// <summary>
/// Wildcard domains format. For example: https://*.abp.io
/// </summary>
public HashSet<string> WildcardDomainsFormat { get; }

public AbpOpenIddictWildcardDomainOptions()
Expand Down
Loading