Skip to content

Commit 75c42f4

Browse files
authored
Added post configuration of OpenIddictServerOptions that removes the ValidateTransportSecurityRequirement iff globalsettings.usehttps is false. (#16614)
1 parent 1f52d01 commit 75c42f4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Options;
3+
using OpenIddict.Server;
4+
using OpenIddict.Server.AspNetCore;
5+
using Umbraco.Cms.Core.Configuration.Models;
6+
7+
namespace Umbraco.Cms.Api.Common.Configuration;
8+
9+
internal class PostConfigureOpenIddict : IPostConfigureOptions<OpenIddictServerOptions>
10+
{
11+
private readonly IOptions<GlobalSettings> _globalSettings;
12+
13+
public PostConfigureOpenIddict(IOptions<GlobalSettings> globalSettings)
14+
{
15+
_globalSettings = globalSettings;
16+
}
17+
18+
public void PostConfigure(string? name, OpenIddictServerOptions options)
19+
{
20+
EnsureHttpsIsNotRequiredWhenConfigAllowHttp(options);
21+
}
22+
23+
/// <summary>
24+
/// Ensures OpenIddict is configured to allow Http requrest, if and only if, the global settings are configured to allow Http.
25+
/// </summary>
26+
/// <remarks>
27+
/// The logic actually allowing http by removing the ValidateTransportSecurityRequirement Descriptor is borrowed from <see cref="OpenIddictServerBuilder.RemoveEventHandler"/>
28+
/// </remarks>
29+
private void EnsureHttpsIsNotRequiredWhenConfigAllowHttp(OpenIddictServerOptions options)
30+
{
31+
if (_globalSettings.Value.UseHttps is false)
32+
{
33+
OpenIddictServerHandlerDescriptor descriptor = OpenIddictServerAspNetCoreHandlers.ValidateTransportSecurityRequirement.Descriptor;
34+
35+
for (var index = options.Handlers.Count - 1; index >= 0; index--)
36+
{
37+
if (options.Handlers[index].ServiceDescriptor.ServiceType == descriptor.ServiceDescriptor.ServiceType)
38+
{
39+
options.Handlers.RemoveAt(index);
40+
}
41+
}
42+
}
43+
}
44+
}

src/Umbraco.Cms.Api.Common/DependencyInjection/UmbracoBuilderAuthExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.IdentityModel.Tokens;
55
using OpenIddict.Server;
66
using OpenIddict.Validation;
7+
using Umbraco.Cms.Api.Common.Configuration;
78
using Umbraco.Cms.Api.Common.Security;
89
using Umbraco.Cms.Core;
910
using Umbraco.Cms.Core.Configuration.Models;
@@ -132,5 +133,6 @@ private static void ConfigureOpenIddict(IUmbracoBuilder builder)
132133
});
133134

134135
builder.Services.AddRecurringBackgroundJob<OpenIddictCleanupJob>();
136+
builder.Services.ConfigureOptions<PostConfigureOpenIddict>();
135137
}
136138
}

0 commit comments

Comments
 (0)