2023-07-19 09:02:51 +03:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2022-11-01 11:15:31 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-09-26 09:22:45 +02:00
|
|
|
|
using Umbraco.Cms.Api.Common.DependencyInjection;
|
2024-02-06 13:02:35 +01:00
|
|
|
|
using Umbraco.Cms.Api.Management.Handlers;
|
2022-11-01 11:15:31 +01:00
|
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
2022-12-02 11:33:02 +01:00
|
|
|
|
using Umbraco.Cms.Api.Management.Middleware;
|
|
|
|
|
|
using Umbraco.Cms.Api.Management.Security;
|
2024-02-06 13:02:35 +01:00
|
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2023-05-25 10:38:44 +02:00
|
|
|
|
using Umbraco.Cms.Infrastructure.Security;
|
2023-06-28 08:40:28 +02:00
|
|
|
|
using Umbraco.Cms.Web.Common.ApplicationBuilder;
|
2022-11-01 11:15:31 +01:00
|
|
|
|
|
2022-12-02 11:33:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Api.Management.DependencyInjection;
|
2022-11-01 11:15:31 +01:00
|
|
|
|
|
|
|
|
|
|
public static class BackOfficeAuthBuilderExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IUmbracoBuilder AddBackOfficeAuthentication(this IUmbracoBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
builder
|
2023-09-26 09:22:45 +02:00
|
|
|
|
.AddAuthentication()
|
|
|
|
|
|
.AddUmbracoOpenIddict()
|
2024-02-27 12:40:30 +01:00
|
|
|
|
.AddBackOfficeLogin();
|
2024-02-06 13:02:35 +01:00
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-27 12:40:30 +01:00
|
|
|
|
public static IUmbracoBuilder AddTokenRevocation(this IUmbracoBuilder builder)
|
2024-02-06 13:02:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
builder.AddNotificationAsyncHandler<UserSavingNotification, RevokeUserAuthenticationTokensNotificationHandler>();
|
|
|
|
|
|
builder.AddNotificationAsyncHandler<UserSavedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
|
|
|
|
|
|
builder.AddNotificationAsyncHandler<UserDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
|
|
|
|
|
|
builder.AddNotificationAsyncHandler<UserGroupDeletingNotification, RevokeUserAuthenticationTokensNotificationHandler>();
|
|
|
|
|
|
builder.AddNotificationAsyncHandler<UserGroupDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
|
2022-11-01 11:15:31 +01:00
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-26 09:22:45 +02:00
|
|
|
|
private static IUmbracoBuilder AddAuthentication(this IUmbracoBuilder builder)
|
2022-11-01 11:15:31 +01:00
|
|
|
|
{
|
|
|
|
|
|
builder.Services.AddAuthentication();
|
2023-07-19 09:02:51 +03:00
|
|
|
|
builder.AddAuthorizationPolicies();
|
2022-11-01 11:15:31 +01:00
|
|
|
|
|
|
|
|
|
|
builder.Services.AddTransient<IBackOfficeApplicationManager, BackOfficeApplicationManager>();
|
|
|
|
|
|
builder.Services.AddSingleton<BackOfficeAuthorizationInitializationMiddleware>();
|
2023-06-28 08:40:28 +02:00
|
|
|
|
builder.Services.Configure<UmbracoPipelineOptions>(options => options.AddFilter(new BackofficePipelineFilter("Backoffice")));
|
2022-11-01 11:15:31 +01:00
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-19 13:32:11 +02:00
|
|
|
|
private static IUmbracoBuilder AddBackOfficeLogin(this IUmbracoBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.Services
|
|
|
|
|
|
.AddAuthentication()
|
2024-02-27 12:40:30 +01:00
|
|
|
|
// Add our custom schemes which are cookie handlers
|
|
|
|
|
|
.AddCookie(Constants.Security.BackOfficeAuthenticationType, options =>
|
2023-04-19 13:32:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
options.LoginPath = "/umbraco/login";
|
2024-02-27 12:40:30 +01:00
|
|
|
|
options.Cookie.Name = Constants.Security.BackOfficeAuthenticationType;
|
2024-01-23 18:07:21 +01:00
|
|
|
|
})
|
2024-02-27 12:40:30 +01:00
|
|
|
|
.AddCookie(Constants.Security.BackOfficeExternalAuthenticationType, o =>
|
2024-01-23 18:07:21 +01:00
|
|
|
|
{
|
2024-02-27 12:40:30 +01:00
|
|
|
|
o.Cookie.Name = Constants.Security.BackOfficeExternalAuthenticationType;
|
|
|
|
|
|
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
2024-01-23 18:07:21 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Although we don't natively support this, we add it anyways so that if end-users implement the required logic
|
|
|
|
|
|
// they don't have to worry about manually adding this scheme or modifying the sign in manager
|
2024-02-27 12:40:30 +01:00
|
|
|
|
.AddCookie(Constants.Security.BackOfficeTwoFactorAuthenticationType, options =>
|
2024-01-23 18:07:21 +01:00
|
|
|
|
{
|
2024-02-27 12:40:30 +01:00
|
|
|
|
options.Cookie.Name = Constants.Security.BackOfficeTwoFactorAuthenticationType;
|
2024-01-23 18:07:21 +01:00
|
|
|
|
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
|
|
|
|
|
})
|
2024-02-27 12:40:30 +01:00
|
|
|
|
.AddCookie(Constants.Security.BackOfficeTwoFactorRememberMeAuthenticationType, o =>
|
2024-01-23 18:07:21 +01:00
|
|
|
|
{
|
2024-02-27 12:40:30 +01:00
|
|
|
|
o.Cookie.Name = Constants.Security.BackOfficeTwoFactorRememberMeAuthenticationType;
|
|
|
|
|
|
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
2023-04-19 13:32:11 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
2022-11-01 11:15:31 +01:00
|
|
|
|
}
|
2023-06-28 08:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
internal class BackofficePipelineFilter : UmbracoPipelineFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
public BackofficePipelineFilter(string name)
|
|
|
|
|
|
: base(name)
|
|
|
|
|
|
=> PrePipeline = builder => builder.UseMiddleware<BackOfficeAuthorizationInitializationMiddleware>();
|
|
|
|
|
|
}
|