| | | 1 | | using Microsoft.AspNetCore.Authentication; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using ProjectTemplate.Web.Authentication.Claims; |
| | | 5 | | using ProjectTemplate.Web.Authentication.Options; |
| | | 6 | | using ProjectTemplate.Web.Authentication.Providers.GitHub; |
| | | 7 | | using ProjectTemplate.Web.Authentication.Providers.Google; |
| | | 8 | | using ProjectTemplate.Web.Authentication.Providers.Microsoft; |
| | | 9 | | using ProjectTemplate.Web.Authentication.Providers.OpenIdConnect; |
| | | 10 | | using ProjectTemplate.Web.Authentication.Providers.Saml2; |
| | | 11 | | |
| | | 12 | | namespace ProjectTemplate.Web.Authentication.Extensions; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides extension methods for registering and applying application authentication services. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class AuthenticationServiceExtensions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Adds application authentication services based on configuration using the secure cookie policy without an |
| | | 21 | | /// environment-specific plain HTTP override. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="services">The service collection to add authentication services to.</param> |
| | | 24 | | /// <param name="configuration">The application configuration source.</param> |
| | | 25 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 26 | | public static IServiceCollection AddApplicationAuthentication( |
| | | 27 | | this IServiceCollection services, |
| | | 28 | | IConfiguration configuration) |
| | | 29 | | { |
| | 28 | 30 | | return AddApplicationAuthentication(services, configuration, environment: null); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Adds application authentication services based on configuration and the current hosting environment. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="services">The service collection to add authentication services to.</param> |
| | | 37 | | /// <param name="configuration">The application configuration source.</param> |
| | | 38 | | /// <param name="environment">The current hosting environment.</param> |
| | | 39 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 40 | | public static IServiceCollection AddApplicationAuthentication( |
| | | 41 | | this IServiceCollection services, |
| | | 42 | | IConfiguration configuration, |
| | | 43 | | IHostEnvironment? environment) |
| | | 44 | | { |
| | 194 | 45 | | ArgumentNullException.ThrowIfNull(services); |
| | 194 | 46 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 47 | | |
| | 194 | 48 | | services.AddTransient<IClaimsTransformation, ApplicationClaimsTransformation>(); |
| | 194 | 49 | | services.AddSingleton<IValidateOptions<ApplicationAuthenticationOptions>>( |
| | 194 | 50 | | new ApplicationAuthenticationOptionsValidator(environment)); |
| | | 51 | | |
| | 194 | 52 | | services |
| | 194 | 53 | | .AddOptions<ApplicationAuthenticationOptions>() |
| | 194 | 54 | | .Bind(configuration.GetSection(ApplicationAuthenticationOptions.SectionName)) |
| | 194 | 55 | | .ValidateOnStart(); |
| | | 56 | | |
| | 194 | 57 | | AuthenticationBuilder authenticationBuilder = services.AddAuthentication(options => |
| | 194 | 58 | | { |
| | 162 | 59 | | options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 162 | 60 | | options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 162 | 61 | | options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 162 | 62 | | options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 356 | 63 | | }); |
| | 194 | 64 | | services |
| | 194 | 65 | | .AddOptions<AuthenticationOptions>() |
| | 194 | 66 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 194 | 67 | | { |
| | 162 | 68 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 162 | 69 | | applicationAuthenticationOptionsAccessor.Value; |
| | 194 | 70 | | |
| | 162 | 71 | | if (!applicationAuthenticationOptions.Enabled) |
| | 194 | 72 | | { |
| | 4 | 73 | | return; |
| | 194 | 74 | | } |
| | 194 | 75 | | |
| | 158 | 76 | | options.DefaultScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 158 | 77 | | options.DefaultAuthenticateScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 158 | 78 | | options.DefaultChallengeScheme = applicationAuthenticationOptions.DefaultChallengeScheme; |
| | 158 | 79 | | options.DefaultSignInScheme = applicationAuthenticationOptions.DefaultSignInScheme; |
| | 352 | 80 | | }); |
| | | 81 | | |
| | 194 | 82 | | authenticationBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme); |
| | | 83 | | |
| | 194 | 84 | | services |
| | 194 | 85 | | .AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme) |
| | 194 | 86 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 194 | 87 | | { |
| | 92 | 88 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 92 | 89 | | applicationAuthenticationOptionsAccessor.Value; |
| | 194 | 90 | | |
| | 92 | 91 | | options.LoginPath = applicationAuthenticationOptions.Cookie.LoginPath; |
| | 92 | 92 | | options.LogoutPath = applicationAuthenticationOptions.Cookie.LogoutPath; |
| | 92 | 93 | | options.AccessDeniedPath = applicationAuthenticationOptions.Cookie.AccessDeniedPath; |
| | 92 | 94 | | options.ExpireTimeSpan = TimeSpan.FromMinutes(applicationAuthenticationOptions.Cookie.ExpireMinutes); |
| | 92 | 95 | | options.SlidingExpiration = applicationAuthenticationOptions.Cookie.SlidingExpiration; |
| | 194 | 96 | | |
| | 92 | 97 | | options.Cookie.Name = ".ProjectTemplate.Web.Authentication"; |
| | 92 | 98 | | options.Cookie.HttpOnly = true; |
| | 92 | 99 | | options.Cookie.SameSite = SameSiteMode.Lax; |
| | 92 | 100 | | options.Cookie.SecurePolicy = applicationAuthenticationOptions.Cookie.AllowInsecureHttp && |
| | 92 | 101 | | environment?.IsDevelopment() == true |
| | 92 | 102 | | ? CookieSecurePolicy.SameAsRequest |
| | 92 | 103 | | : CookieSecurePolicy.Always; |
| | 286 | 104 | | }); |
| | | 105 | | |
| | 194 | 106 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = configuration |
| | 194 | 107 | | .GetSection(ApplicationAuthenticationOptions.SectionName) |
| | 194 | 108 | | .Get<ApplicationAuthenticationOptions>() ?? new ApplicationAuthenticationOptions(); |
| | | 109 | | |
| | 194 | 110 | | authenticationBuilder |
| | 194 | 111 | | .AddOpenIdConnectAuthentication(applicationAuthenticationOptions.Providers.OpenIdConnect) |
| | 194 | 112 | | .AddSaml2Authentication(applicationAuthenticationOptions.Providers.Saml2) |
| | 194 | 113 | | .AddMicrosoftAuthentication(applicationAuthenticationOptions.Providers.Microsoft) |
| | 194 | 114 | | .AddGoogleAuthentication(applicationAuthenticationOptions.Providers.Google) |
| | 194 | 115 | | .AddGitHubAuthentication(applicationAuthenticationOptions.Providers.GitHub); |
| | | 116 | | |
| | 194 | 117 | | return services; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Applies application authentication middleware when authentication is enabled. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="app">The application builder.</param> |
| | | 124 | | /// <returns>The same <see cref="IApplicationBuilder"/> instance for chaining.</returns> |
| | | 125 | | public static IApplicationBuilder UseApplicationAuthentication(this IApplicationBuilder app) |
| | | 126 | | { |
| | 154 | 127 | | ArgumentNullException.ThrowIfNull(app); |
| | | 128 | | |
| | 154 | 129 | | ApplicationAuthenticationOptions options = app.ApplicationServices |
| | 154 | 130 | | .GetRequiredService<IOptions<ApplicationAuthenticationOptions>>() |
| | 154 | 131 | | .Value; |
| | | 132 | | |
| | 140 | 133 | | return !options.Enabled ? app : app.UseAuthentication(); |
| | | 134 | | } |
| | | 135 | | } |