Simplified how we disable the TransportSecurityRequirement in OpenIddict (#16629)

This commit is contained in:
Bjarke Berg
2024-06-20 10:39:24 +02:00
committed by GitHub
parent 75c42f4ea4
commit f717a5d0b7
3 changed files with 16 additions and 45 deletions

View File

@@ -0,0 +1,15 @@
using Microsoft.Extensions.Options;
using OpenIddict.Server.AspNetCore;
using Umbraco.Cms.Core.Configuration.Models;
namespace Umbraco.Cms.Api.Common.Configuration;
internal class ConfigureOpenIddict : IConfigureOptions<OpenIddictServerAspNetCoreOptions>
{
private readonly IOptions<GlobalSettings> _globalSettings;
public ConfigureOpenIddict(IOptions<GlobalSettings> globalSettings) => _globalSettings = globalSettings;
public void Configure(OpenIddictServerAspNetCoreOptions options)
=> options.DisableTransportSecurityRequirement = _globalSettings.Value.UseHttps is false;
}

View File

@@ -1,44 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using OpenIddict.Server.AspNetCore;
using Umbraco.Cms.Core.Configuration.Models;
namespace Umbraco.Cms.Api.Common.Configuration;
internal class PostConfigureOpenIddict : IPostConfigureOptions<OpenIddictServerOptions>
{
private readonly IOptions<GlobalSettings> _globalSettings;
public PostConfigureOpenIddict(IOptions<GlobalSettings> globalSettings)
{
_globalSettings = globalSettings;
}
public void PostConfigure(string? name, OpenIddictServerOptions options)
{
EnsureHttpsIsNotRequiredWhenConfigAllowHttp(options);
}
/// <summary>
/// Ensures OpenIddict is configured to allow Http requrest, if and only if, the global settings are configured to allow Http.
/// </summary>
/// <remarks>
/// The logic actually allowing http by removing the ValidateTransportSecurityRequirement Descriptor is borrowed from <see cref="OpenIddictServerBuilder.RemoveEventHandler"/>
/// </remarks>
private void EnsureHttpsIsNotRequiredWhenConfigAllowHttp(OpenIddictServerOptions options)
{
if (_globalSettings.Value.UseHttps is false)
{
OpenIddictServerHandlerDescriptor descriptor = OpenIddictServerAspNetCoreHandlers.ValidateTransportSecurityRequirement.Descriptor;
for (var index = options.Handlers.Count - 1; index >= 0; index--)
{
if (options.Handlers[index].ServiceDescriptor.ServiceType == descriptor.ServiceDescriptor.ServiceType)
{
options.Handlers.RemoveAt(index);
}
}
}
}
}

View File

@@ -133,6 +133,6 @@ public static class UmbracoBuilderAuthExtensions
});
builder.Services.AddRecurringBackgroundJob<OpenIddictCleanupJob>();
builder.Services.ConfigureOptions<PostConfigureOpenIddict>();
builder.Services.ConfigureOptions<ConfigureOpenIddict>();
}
}