-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathBackOfficeAuthBuilderExtensions.cs
More file actions
90 lines (77 loc) · 4.07 KB
/
BackOfficeAuthBuilderExtensions.cs
File metadata and controls
90 lines (77 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.DependencyInjection;
using Umbraco.Cms.Api.Management.Configuration;
using Umbraco.Cms.Api.Management.Handlers;
using Umbraco.Cms.Api.Management.Middleware;
using Umbraco.Cms.Api.Management.Security;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Infrastructure.Security;
using Umbraco.Cms.Web.Common.ApplicationBuilder;
namespace Umbraco.Cms.Api.Management.DependencyInjection;
public static class BackOfficeAuthBuilderExtensions
{
public static IUmbracoBuilder AddBackOfficeAuthentication(this IUmbracoBuilder builder)
{
builder
.AddAuthentication()
.AddUmbracoOpenIddict()
.AddBackOfficeLogin();
return builder;
}
public static IUmbracoBuilder AddTokenRevocation(this IUmbracoBuilder builder)
{
builder.AddNotificationAsyncHandler<UserSavingNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserSavedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserGroupDeletingNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserGroupDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserLoginSuccessNotification, RevokeUserAuthenticationTokensNotificationHandler>();
return builder;
}
private static IUmbracoBuilder AddAuthentication(this IUmbracoBuilder builder)
{
builder.Services.AddAuthentication();
builder.AddAuthorizationPolicies();
builder.Services.AddTransient<IBackOfficeApplicationManager, BackOfficeApplicationManager>();
builder.Services.AddSingleton<BackOfficeAuthorizationInitializationMiddleware>();
builder.Services.Configure<UmbracoPipelineOptions>(options => options.AddFilter(new BackofficePipelineFilter("Backoffice")));
return builder;
}
private static IUmbracoBuilder AddBackOfficeLogin(this IUmbracoBuilder builder)
{
builder.Services
.AddAuthentication()
// Add our custom schemes which are cookie handlers
.AddCookie(Constants.Security.BackOfficeAuthenticationType)
.AddCookie(Constants.Security.BackOfficeExternalAuthenticationType, o =>
{
o.Cookie.Name = Constants.Security.BackOfficeExternalAuthenticationType;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
})
// 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
.AddCookie(Constants.Security.BackOfficeTwoFactorAuthenticationType, options =>
{
options.Cookie.Name = Constants.Security.BackOfficeTwoFactorAuthenticationType;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
})
.AddCookie(Constants.Security.BackOfficeTwoFactorRememberMeAuthenticationType, o =>
{
o.Cookie.Name = Constants.Security.BackOfficeTwoFactorRememberMeAuthenticationType;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
builder.Services.AddScoped<BackOfficeSecurityStampValidator>();
builder.Services.ConfigureOptions<ConfigureBackOfficeCookieOptions>();
builder.Services.ConfigureOptions<ConfigureBackOfficeSecurityStampValidatorOptions>();
return builder;
}
}
internal class BackofficePipelineFilter : UmbracoPipelineFilter
{
public BackofficePipelineFilter(string name)
: base(name)
=> PrePipeline = builder => builder.UseMiddleware<BackOfficeAuthorizationInitializationMiddleware>();
}