diff --git a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs index 36c41601..c429b282 100644 --- a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs +++ b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs @@ -1,11 +1,21 @@ using System; using System.IO; using System.Linq; +using System.Net.Http; using System.Reflection; using System.Runtime.Loader; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; using Oqtane.Infrastructure; using Oqtane.Modules; +using Oqtane.Repository; +using Oqtane.Security; using Oqtane.Services; using Oqtane.Shared; @@ -23,6 +33,190 @@ namespace Microsoft.Extensions.DependencyInjection return services; } + public static IServiceCollection AddOqtaneDbContext(this IServiceCollection services) + { + services.AddDbContext(options => { }); + services.AddDbContext(options => { }); + + services.AddIdentityCore(options => { }) + .AddEntityFrameworkStores() + .AddSignInManager() + .AddDefaultTokenProviders(); + + return services; + } + + public static IServiceCollection AddOqtaneAuthorizationPolicies(this IServiceCollection services) + { + services.AddAuthorizationCore(options => + { + options.AddPolicy(PolicyNames.ViewPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.View))); + options.AddPolicy(PolicyNames.EditPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.Edit))); + options.AddPolicy(PolicyNames.ViewModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.View))); + options.AddPolicy(PolicyNames.EditModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.Edit))); + options.AddPolicy(PolicyNames.ViewFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.View))); + options.AddPolicy(PolicyNames.EditFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Edit))); + options.AddPolicy(PolicyNames.ListFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Browse))); + }); + + return services; + } + + internal static IServiceCollection AddOqtaneScopedServices(this IServiceCollection services) + { + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + return services; + } + + internal static IServiceCollection AddOqtaneSingletonServices(this IServiceCollection services, IConfigurationRoot configurationRoot) + { + services.AddSingleton(configurationRoot); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + return services; + } + + internal static IServiceCollection AddOqtaneTransientServices(this IServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + return services; + } + + public static IServiceCollection ConfigureOqtaneCookieOptions(this IServiceCollection services) + { + services.ConfigureApplicationCookie(options => + { + options.Cookie.HttpOnly = false; + options.Events.OnRedirectToLogin = context => + { + context.Response.StatusCode = 401; + + return Task.CompletedTask; + }; + }); + + return services; + } + + public static IServiceCollection ConfigureOqtaneIdentityOptions(this IServiceCollection services) + { + services.Configure(options => + { + // Password settings + options.Password.RequireDigit = false; + options.Password.RequiredLength = 6; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + options.Password.RequireLowercase = false; + + // Lockout settings + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); + options.Lockout.MaxFailedAccessAttempts = 10; + options.Lockout.AllowedForNewUsers = true; + + // User settings + options.User.RequireUniqueEmail = false; + }); + + return services; + } + + internal static IServiceCollection TryAddHttpClientWithAuthenticationCookie(this IServiceCollection services) + { + if (!services.Any(x => x.ServiceType == typeof(HttpClient))) + { + services.AddScoped(s => + { + // creating the URI helper needs to wait until the JS Runtime is initialized, so defer it. + var navigationManager = s.GetRequiredService(); + var httpContextAccessor = s.GetRequiredService(); + var authToken = httpContextAccessor.HttpContext.Request.Cookies[".AspNetCore.Identity.Application"]; + var client = new HttpClient(new HttpClientHandler { UseCookies = false }); + if (authToken != null) + { + client.DefaultRequestHeaders.Add("Cookie", ".AspNetCore.Identity.Application=" + authToken); + } + + client.BaseAddress = new Uri(navigationManager.Uri); + + return client; + }); + } + + return services; + } + + internal static IServiceCollection TryAddSwagger(this IServiceCollection services, bool useSwagger) + { + if (useSwagger) + { + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Oqtane", Version = "v1" }); + }); + } + + return services; + } + private static IServiceCollection AddOqtaneServices(this IServiceCollection services, Runtime runtime) { if (services is null) diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index 1e744c01..f2c44365 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -1,35 +1,25 @@ using System; -using System.Collections; using System.IO; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.OpenApi.Models; using Oqtane.Extensions; using Oqtane.Infrastructure; -using Oqtane.Repository; using Oqtane.Security; -using Oqtane.Services; using Oqtane.Shared; namespace Oqtane { public class Startup { - private Runtime _runtime; - private bool _useSwagger; - private IWebHostEnvironment _env; - private string[] _supportedCultures; + private readonly Runtime _runtime; + private readonly bool _useSwagger; + private readonly IWebHostEnvironment _env; + private readonly string[] _supportedCultures; public IConfigurationRoot Configuration { get; } @@ -59,157 +49,46 @@ namespace Oqtane // Register localization services services.AddLocalization(options => options.ResourcesPath = "Resources"); - services.AddServerSideBlazor().AddCircuitOptions(options => - { - if (_env.IsDevelopment()) + services.AddServerSideBlazor() + .AddCircuitOptions(options => { - options.DetailedErrors = true; - } - }); + if (_env.IsDevelopment()) + { + options.DetailedErrors = true; + } + }); // setup HttpClient for server side in a client side compatible fashion ( with auth cookie ) - if (!services.Any(x => x.ServiceType == typeof(HttpClient))) - { - services.AddScoped(s => - { - // creating the URI helper needs to wait until the JS Runtime is initialized, so defer it. - var navigationManager = s.GetRequiredService(); - var httpContextAccessor = s.GetRequiredService(); - var authToken = httpContextAccessor.HttpContext.Request.Cookies[".AspNetCore.Identity.Application"]; - var client = new HttpClient(new HttpClientHandler {UseCookies = false}); - if (authToken != null) - { - client.DefaultRequestHeaders.Add("Cookie", ".AspNetCore.Identity.Application=" + authToken); - } - client.BaseAddress = new Uri(navigationManager.Uri); - return client; - }); - } + services.TryAddHttpClientWithAuthenticationCookie(); // register custom authorization policies - services.AddAuthorizationCore(options => - { - options.AddPolicy(PolicyNames.ViewPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.View))); - options.AddPolicy(PolicyNames.EditPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.Edit))); - options.AddPolicy(PolicyNames.ViewModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.View))); - options.AddPolicy(PolicyNames.EditModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.Edit))); - options.AddPolicy(PolicyNames.ViewFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.View))); - options.AddPolicy(PolicyNames.EditFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Edit))); - options.AddPolicy(PolicyNames.ListFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Browse))); - }); + services.AddOqtaneAuthorizationPolicies(); // register scoped core services - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddOqtaneScopedServices(); services.AddSingleton(); - services.AddDbContext(options => { }); - services.AddDbContext(options => { }); + services.AddOqtaneDbContext(); - services.AddIdentityCore(options => { }) - .AddEntityFrameworkStores() - .AddSignInManager() - .AddDefaultTokenProviders(); - - services.Configure(options => - { - // Password settings - options.Password.RequireDigit = false; - options.Password.RequiredLength = 6; - options.Password.RequireNonAlphanumeric = false; - options.Password.RequireUppercase = false; - options.Password.RequireLowercase = false; - - // Lockout settings - options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); - options.Lockout.MaxFailedAccessAttempts = 10; - options.Lockout.AllowedForNewUsers = true; - - // User settings - options.User.RequireUniqueEmail = false; - }); + services.ConfigureOqtaneIdentityOptions(); services.AddAuthentication(IdentityConstants.ApplicationScheme) .AddCookie(IdentityConstants.ApplicationScheme); - services.ConfigureApplicationCookie(options => - { - options.Cookie.HttpOnly = false; - options.Events.OnRedirectToLogin = context => - { - context.Response.StatusCode = 401; - return Task.CompletedTask; - }; - }); + services.ConfigureOqtaneCookieOptions(); // register custom claims principal factory for role claims services.AddTransient, ClaimsPrincipalFactory>(); // register singleton scoped core services - services.AddSingleton(Configuration); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); + services.AddOqtaneSingletonServices(Configuration); // install any modules or themes ( this needs to occur BEFORE the assemblies are loaded into the app domain ) InstallationManager.InstallPackages("Modules,Themes", _env.WebRootPath, _env.ContentRootPath); // register transient scoped core services - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddOqtaneTransientServices(); // load the external assemblies into the app domain, install services services.AddOqtane(_runtime, _supportedCultures); @@ -219,10 +98,7 @@ namespace Oqtane .AddOqtaneApplicationParts() // register any Controllers from custom modules .ConfigureOqtaneMvc(); // any additional configuration from IStart classes. - if (_useSwagger) - { - services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "Oqtane", Version = "v1"}); }); - } + services.TryAddSwagger(_useSwagger); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.