factor out auth constants, remove TAlias is Alias is not an extensible type, improve SiteOptions cache clearing, improve principal validation, localization improvements
This commit is contained in:
		| @ -7,6 +7,8 @@ using System.Net.Http; | ||||
| using System.Reflection; | ||||
| using System.Runtime.Loader; | ||||
| using System.Threading.Tasks; | ||||
| using Microsoft.AspNetCore.Authentication.OAuth; | ||||
| using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||||
| using Microsoft.AspNetCore.Components; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.AspNetCore.Identity; | ||||
| @ -15,7 +17,6 @@ using Microsoft.Extensions.Hosting; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Microsoft.OpenApi.Models; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Models; | ||||
| using Oqtane.Modules; | ||||
| using Oqtane.Repository; | ||||
| using Oqtane.Security; | ||||
| @ -59,10 +60,9 @@ namespace Microsoft.Extensions.DependencyInjection | ||||
|             return services; | ||||
|         } | ||||
|  | ||||
|         public static OqtaneSiteOptionsBuilder<T> AddOqtaneSiteOptions<T>(this IServiceCollection services) | ||||
|             where T : class, IAlias, new() | ||||
|         public static OqtaneSiteOptionsBuilder AddOqtaneSiteOptions(this IServiceCollection services) | ||||
|         { | ||||
|             return new OqtaneSiteOptionsBuilder<T>(services); | ||||
|             return new OqtaneSiteOptionsBuilder(services); | ||||
|         } | ||||
|  | ||||
|         internal static IServiceCollection AddOqtaneSingletonServices(this IServiceCollection services) | ||||
| @ -144,6 +144,15 @@ namespace Microsoft.Extensions.DependencyInjection | ||||
|             return services; | ||||
|         } | ||||
|  | ||||
|         public static IServiceCollection ConfigureOqtaneAuthenticationOptions(this IServiceCollection services, IConfigurationRoot Configuration) | ||||
|         { | ||||
|             // settings defined in appsettings | ||||
|             services.Configure<OAuthOptions>(Configuration); | ||||
|             services.Configure<OpenIdConnectOptions>(Configuration); | ||||
|  | ||||
|             return services; | ||||
|         } | ||||
|  | ||||
|         public static IServiceCollection ConfigureOqtaneIdentityOptions(this IServiceCollection services, IConfigurationRoot Configuration) | ||||
|         { | ||||
|             // default settings | ||||
|  | ||||
| @ -11,7 +11,6 @@ using System.Security.Claims; | ||||
| using System.Threading.Tasks; | ||||
| using Microsoft.AspNetCore.Identity; | ||||
| using Oqtane.Repository; | ||||
| using System.IO; | ||||
| using System.Collections.Generic; | ||||
| using Oqtane.Security; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| @ -24,30 +23,19 @@ namespace Oqtane.Extensions | ||||
| { | ||||
|     public static class OqtaneSiteAuthenticationBuilderExtensions | ||||
|     { | ||||
|         public static OqtaneSiteOptionsBuilder<TAlias> WithSiteAuthentication<TAlias>( | ||||
|             this OqtaneSiteOptionsBuilder<TAlias> builder) | ||||
|             where TAlias : class, IAlias, new() | ||||
|         { | ||||
|             builder.WithSiteAuthenticationOptions(); | ||||
|  | ||||
|             return builder; | ||||
|         } | ||||
|  | ||||
|         public static OqtaneSiteOptionsBuilder<TAlias> WithSiteAuthenticationOptions<TAlias>( | ||||
|             this OqtaneSiteOptionsBuilder<TAlias> builder) | ||||
|             where TAlias : class, IAlias, new() | ||||
|         public static OqtaneSiteOptionsBuilder WithSiteAuthentication(this OqtaneSiteOptionsBuilder builder) | ||||
|         { | ||||
|             // site OpenIdConnect options | ||||
|             builder.AddSiteOptions<OpenIdConnectOptions>((options, alias) => | ||||
|             { | ||||
|                 if (alias.SiteSettings.GetValue("ExternalLogin:ProviderType", "") == "oidc") | ||||
|                 if (alias.SiteSettings.GetValue("ExternalLogin:ProviderType", "") == AuthenticationProviderTypes.OpenIDConnect) | ||||
|                 { | ||||
|                     // default options | ||||
|                     options.SignInScheme = Constants.AuthenticationScheme; // identity cookie | ||||
|                     options.RequireHttpsMetadata = true; | ||||
|                     options.SaveTokens = true; | ||||
|                     options.GetClaimsFromUserInfoEndpoint = true; | ||||
|                     options.CallbackPath = string.IsNullOrEmpty(alias.Path) ? "/signin-oidc" : "/" + alias.Path + "/signin-oidc"; | ||||
|                     options.CallbackPath = string.IsNullOrEmpty(alias.Path) ? "/signin-" + AuthenticationProviderTypes.OpenIDConnect : "/" + alias.Path + "/signin-" + AuthenticationProviderTypes.OpenIDConnect; | ||||
|                     options.ResponseType = OpenIdConnectResponseType.Code; // authorization code flow | ||||
|                     options.ResponseMode = OpenIdConnectResponseMode.FormPost; // recommended as most secure | ||||
|  | ||||
| @ -77,11 +65,11 @@ namespace Oqtane.Extensions | ||||
|             // site OAuth2.0 options | ||||
|             builder.AddSiteOptions<OAuthOptions>((options, alias) => | ||||
|             { | ||||
|                 if (alias.SiteSettings.GetValue("ExternalLogin:ProviderType", "") == "oauth2") | ||||
|                 if (alias.SiteSettings.GetValue("ExternalLogin:ProviderType", "") == AuthenticationProviderTypes.OAuth2) | ||||
|                 { | ||||
|                     // default options | ||||
|                     options.SignInScheme = Constants.AuthenticationScheme; // identity cookie | ||||
|                     options.CallbackPath = string.IsNullOrEmpty(alias.Path) ? "/signin-oauth2" : "/" + alias.Path + "/signin-oauth2"; | ||||
|                     options.CallbackPath = string.IsNullOrEmpty(alias.Path) ? "/signin-" + AuthenticationProviderTypes.OAuth2 : "/" + alias.Path + "/signin-" + AuthenticationProviderTypes.OAuth2; | ||||
|                     options.SaveTokens = true; | ||||
|  | ||||
|                     // site options | ||||
|  | ||||
| @ -7,9 +7,7 @@ namespace Oqtane.Extensions | ||||
| { | ||||
|     public static class OqtaneSiteIdentityBuilderExtensions | ||||
|     { | ||||
|         public static OqtaneSiteOptionsBuilder<TAlias> WithSiteIdentity<TAlias>( | ||||
|             this OqtaneSiteOptionsBuilder<TAlias> builder) | ||||
|             where TAlias : class, IAlias, new() | ||||
|         public static OqtaneSiteOptionsBuilder WithSiteIdentity(this OqtaneSiteOptionsBuilder builder) | ||||
|         { | ||||
|             // site identity options | ||||
|             builder.AddSiteOptions<IdentityOptions>((options, alias) => | ||||
|  | ||||
| @ -6,7 +6,7 @@ using Oqtane.Models; | ||||
|  | ||||
| namespace Microsoft.Extensions.DependencyInjection | ||||
| { | ||||
|     public partial class OqtaneSiteOptionsBuilder<TSiteOptions> where TSiteOptions : class, IAlias, new() | ||||
|     public partial class OqtaneSiteOptionsBuilder | ||||
|     { | ||||
|         public IServiceCollection Services { get; set; } | ||||
|  | ||||
| @ -15,13 +15,12 @@ namespace Microsoft.Extensions.DependencyInjection | ||||
|             Services = services; | ||||
|         } | ||||
|  | ||||
|         public OqtaneSiteOptionsBuilder<TSiteOptions> AddSiteOptions<TOptions>( | ||||
|             Action<TOptions, TSiteOptions> siteOptions) where TOptions : class, new() | ||||
|         public OqtaneSiteOptionsBuilder AddSiteOptions<TOptions>( | ||||
|             Action<TOptions, Alias> alias) where TOptions : class, new() | ||||
|         { | ||||
|             Services.TryAddSingleton<IOptionsMonitorCache<TOptions>, SiteOptionsCache<TOptions, TSiteOptions>>(); | ||||
|             Services.AddSingleton<ISiteOptions<TOptions, TSiteOptions>, SiteOptions<TOptions, TSiteOptions>> | ||||
|                 (sp => new SiteOptions<TOptions, TSiteOptions>(siteOptions)); | ||||
|             Services.TryAddTransient<IOptionsFactory<TOptions>, SiteOptionsFactory<TOptions, TSiteOptions>>(); | ||||
|             Services.TryAddSingleton<IOptionsMonitorCache<TOptions>, SiteOptionsCache<TOptions>>(); | ||||
|             Services.AddSingleton<ISiteOptions<TOptions>, SiteOptions<TOptions>> (sp => new SiteOptions<TOptions>(alias)); | ||||
|             Services.TryAddTransient<IOptionsFactory<TOptions>, SiteOptionsFactory<TOptions>>(); | ||||
|             Services.TryAddScoped<IOptionsSnapshot<TOptions>>(sp => BuildOptionsManager<TOptions>(sp)); | ||||
|             Services.TryAddSingleton<IOptions<TOptions>>(sp => BuildOptionsManager<TOptions>(sp)); | ||||
|  | ||||
| @ -31,7 +30,7 @@ namespace Microsoft.Extensions.DependencyInjection | ||||
|         private static SiteOptionsManager<TOptions> BuildOptionsManager<TOptions>(IServiceProvider sp) | ||||
|             where TOptions : class, new() | ||||
|         { | ||||
|             var cache = ActivatorUtilities.CreateInstance(sp, typeof(SiteOptionsCache<TOptions, TSiteOptions>)); | ||||
|             var cache = ActivatorUtilities.CreateInstance(sp, typeof(SiteOptionsCache<TOptions>)); | ||||
|             return (SiteOptionsManager<TOptions>)ActivatorUtilities.CreateInstance(sp, typeof(SiteOptionsManager<TOptions>), new[] { cache }); | ||||
|         } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Shaun Walker
					Shaun Walker