Skip to content

Commit

Permalink
Use the configured backoffice url to initialize openiddict if it is a…
Browse files Browse the repository at this point in the history
…vailable and just fallback to the one from the first request. (umbraco#16660)

Fixes umbraco#16179
  • Loading branch information
bergmania authored and matthewcare committed Jun 29, 2024
1 parent af3375e commit 3182bdb
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Security;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Middleware;

Expand All @@ -16,15 +21,40 @@ public class BackOfficeAuthorizationInitializationMiddleware : IMiddleware
private readonly UmbracoRequestPaths _umbracoRequestPaths;
private readonly IServiceProvider _serviceProvider;
private readonly IRuntimeState _runtimeState;
private readonly IOptions<GlobalSettings> _globalSettings;
private readonly IOptions<WebRoutingSettings> _webRoutingSettings;
private readonly IHostingEnvironment _hostingEnvironment;

[Obsolete("Use the non-obsolete constructor. This will be removed in Umbraco 16.")]
public BackOfficeAuthorizationInitializationMiddleware(
UmbracoRequestPaths umbracoRequestPaths,
IServiceProvider serviceProvider,
IRuntimeState runtimeState)
: this(
umbracoRequestPaths,
serviceProvider,
runtimeState,
StaticServiceProvider.Instance.GetRequiredService<IOptions<GlobalSettings>>(),
StaticServiceProvider.Instance.GetRequiredService<IOptions<WebRoutingSettings>>(),
StaticServiceProvider.Instance.GetRequiredService<IHostingEnvironment>()
)
{
}

public BackOfficeAuthorizationInitializationMiddleware(
UmbracoRequestPaths umbracoRequestPaths,
IServiceProvider serviceProvider,
IRuntimeState runtimeState,
IOptions<GlobalSettings> globalSettings,
IOptions<WebRoutingSettings> webRoutingSettings,
IHostingEnvironment hostingEnvironment)
{
_umbracoRequestPaths = umbracoRequestPaths;
_serviceProvider = serviceProvider;
_runtimeState = runtimeState;
_globalSettings = globalSettings;
_webRoutingSettings = webRoutingSettings;
_hostingEnvironment = hostingEnvironment;
}

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
Expand All @@ -47,6 +77,7 @@ private async Task InitializeBackOfficeAuthorizationOnceAsync(HttpContext contex
return;
}


if (_umbracoRequestPaths.IsBackOfficeRequest(context.Request.Path) == false)
{
return;
Expand All @@ -55,9 +86,13 @@ private async Task InitializeBackOfficeAuthorizationOnceAsync(HttpContext contex
await _firstBackOfficeRequestLocker.WaitAsync();
if (_firstBackOfficeRequest == false)
{
Uri? backOfficeUrl = string.IsNullOrWhiteSpace(_webRoutingSettings.Value.UmbracoApplicationUrl) is false
? new Uri($"{_webRoutingSettings.Value.UmbracoApplicationUrl.TrimEnd('/')}{_globalSettings.Value.GetBackOfficePath(_hostingEnvironment).EnsureStartsWith('/')}")
: null;

using IServiceScope scope = _serviceProvider.CreateScope();
IBackOfficeApplicationManager backOfficeApplicationManager = scope.ServiceProvider.GetRequiredService<IBackOfficeApplicationManager>();
await backOfficeApplicationManager.EnsureBackOfficeApplicationAsync(new Uri(context.Request.GetDisplayUrl()));
await backOfficeApplicationManager.EnsureBackOfficeApplicationAsync(backOfficeUrl ?? new Uri(context.Request.GetDisplayUrl()));
_firstBackOfficeRequest = true;
}

Expand Down

0 comments on commit 3182bdb

Please sign in to comment.