| | | 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 | | /// The authentication cookie name used when the cookie is always sent with the <c>Secure</c> attribute. |
| | | 21 | | /// </summary> |
| | | 22 | | public const string HostPrefixedAuthenticationCookieName = "__Host-ProjectTemplate.Web.Authentication"; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The authentication cookie name used when the Development-only insecure HTTP override is active. |
| | | 26 | | /// </summary> |
| | | 27 | | public const string AuthenticationCookieName = ".ProjectTemplate.Web.Authentication"; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Adds application authentication services based on configuration using the secure cookie policy without an |
| | | 31 | | /// environment-specific plain HTTP override. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="services">The service collection to add authentication services to.</param> |
| | | 34 | | /// <param name="configuration">The application configuration source.</param> |
| | | 35 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 36 | | public static IServiceCollection AddApplicationAuthentication( |
| | | 37 | | this IServiceCollection services, |
| | | 38 | | IConfiguration configuration) |
| | | 39 | | { |
| | 14 | 40 | | return AddApplicationAuthentication(services, configuration, environment: null); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Adds application authentication services based on configuration and the current hosting environment. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="services">The service collection to add authentication services to.</param> |
| | | 47 | | /// <param name="configuration">The application configuration source.</param> |
| | | 48 | | /// <param name="environment">The current hosting environment.</param> |
| | | 49 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 50 | | public static IServiceCollection AddApplicationAuthentication( |
| | | 51 | | this IServiceCollection services, |
| | | 52 | | IConfiguration configuration, |
| | | 53 | | IHostEnvironment? environment) |
| | | 54 | | { |
| | 129 | 55 | | ArgumentNullException.ThrowIfNull(services); |
| | 129 | 56 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 57 | | |
| | 129 | 58 | | services.AddTransient<IClaimsTransformation, ApplicationClaimsTransformation>(); |
| | 129 | 59 | | services.AddSingleton<IValidateOptions<ApplicationAuthenticationOptions>>( |
| | 129 | 60 | | new ApplicationAuthenticationOptionsValidator(environment)); |
| | | 61 | | |
| | 129 | 62 | | services |
| | 129 | 63 | | .AddOptions<ApplicationAuthenticationOptions>() |
| | 129 | 64 | | .Bind(configuration.GetSection(ApplicationAuthenticationOptions.SectionName)) |
| | 129 | 65 | | .ValidateOnStart(); |
| | | 66 | | |
| | 129 | 67 | | AuthenticationBuilder authenticationBuilder = services.AddAuthentication(options => |
| | 129 | 68 | | { |
| | 129 | 69 | | options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 129 | 70 | | options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 129 | 71 | | options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 129 | 72 | | options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 129 | 73 | | }); |
| | 129 | 74 | | services |
| | 129 | 75 | | .AddOptions<AuthenticationOptions>() |
| | 129 | 76 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 129 | 77 | | { |
| | 129 | 78 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 129 | 79 | | applicationAuthenticationOptionsAccessor.Value; |
| | 129 | 80 | | |
| | 129 | 81 | | if (!applicationAuthenticationOptions.Enabled) |
| | 129 | 82 | | { |
| | 129 | 83 | | return; |
| | 129 | 84 | | } |
| | 129 | 85 | | |
| | 129 | 86 | | options.DefaultScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 129 | 87 | | options.DefaultAuthenticateScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 129 | 88 | | options.DefaultChallengeScheme = applicationAuthenticationOptions.DefaultChallengeScheme; |
| | 129 | 89 | | options.DefaultSignInScheme = applicationAuthenticationOptions.DefaultSignInScheme; |
| | 129 | 90 | | }); |
| | | 91 | | |
| | 129 | 92 | | authenticationBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme); |
| | | 93 | | |
| | 129 | 94 | | services |
| | 129 | 95 | | .AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme) |
| | 129 | 96 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 129 | 97 | | { |
| | 129 | 98 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 129 | 99 | | applicationAuthenticationOptionsAccessor.Value; |
| | 129 | 100 | | |
| | 129 | 101 | | options.LoginPath = applicationAuthenticationOptions.Cookie.LoginPath; |
| | 129 | 102 | | options.LogoutPath = applicationAuthenticationOptions.Cookie.LogoutPath; |
| | 129 | 103 | | options.AccessDeniedPath = applicationAuthenticationOptions.Cookie.AccessDeniedPath; |
| | 129 | 104 | | options.ExpireTimeSpan = TimeSpan.FromMinutes(applicationAuthenticationOptions.Cookie.ExpireMinutes); |
| | 129 | 105 | | options.SlidingExpiration = applicationAuthenticationOptions.Cookie.SlidingExpiration; |
| | 129 | 106 | | |
| | 129 | 107 | | options.Cookie.HttpOnly = true; |
| | 129 | 108 | | options.Cookie.SameSite = SameSiteMode.Lax; |
| | 129 | 109 | | options.Cookie.SecurePolicy = applicationAuthenticationOptions.Cookie.AllowInsecureHttp && |
| | 129 | 110 | | environment?.IsDevelopment() == true |
| | 129 | 111 | | ? CookieSecurePolicy.SameAsRequest |
| | 129 | 112 | | : CookieSecurePolicy.Always; |
| | 129 | 113 | | |
| | 129 | 114 | | // Browsers accept a __Host- cookie only when it is Secure, has Path=/, and has no Domain, which binds |
| | 129 | 115 | | // the session cookie to this exact host. The prefix is used only when the cookie is always Secure, so |
| | 129 | 116 | | // the Development-only insecure HTTP override keeps working with an unprefixed name. |
| | 129 | 117 | | bool useHostPrefix = options.Cookie.SecurePolicy == CookieSecurePolicy.Always; |
| | 129 | 118 | | options.Cookie.Name = useHostPrefix |
| | 129 | 119 | | ? HostPrefixedAuthenticationCookieName |
| | 129 | 120 | | : AuthenticationCookieName; |
| | 129 | 121 | | if (useHostPrefix) |
| | 129 | 122 | | { |
| | 129 | 123 | | options.Cookie.Path = "/"; |
| | 129 | 124 | | } |
| | 129 | 125 | | }); |
| | | 126 | | |
| | 129 | 127 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = configuration |
| | 129 | 128 | | .GetSection(ApplicationAuthenticationOptions.SectionName) |
| | 129 | 129 | | .Get<ApplicationAuthenticationOptions>() ?? new ApplicationAuthenticationOptions(); |
| | | 130 | | |
| | 129 | 131 | | authenticationBuilder |
| | 129 | 132 | | .AddOpenIdConnectAuthentication(applicationAuthenticationOptions.Providers.OpenIdConnect) |
| | 129 | 133 | | .AddSaml2Authentication(applicationAuthenticationOptions.Providers.Saml2) |
| | 129 | 134 | | .AddMicrosoftAuthentication(applicationAuthenticationOptions.Providers.Microsoft) |
| | 129 | 135 | | .AddGoogleAuthentication(applicationAuthenticationOptions.Providers.Google) |
| | 129 | 136 | | .AddGitHubAuthentication(applicationAuthenticationOptions.Providers.GitHub); |
| | | 137 | | |
| | 129 | 138 | | return services; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Applies application authentication middleware when authentication is enabled. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="app">The application builder.</param> |
| | | 145 | | /// <returns>The same <see cref="IApplicationBuilder"/> instance for chaining.</returns> |
| | | 146 | | public static IApplicationBuilder UseApplicationAuthentication(this IApplicationBuilder app) |
| | | 147 | | { |
| | 107 | 148 | | ArgumentNullException.ThrowIfNull(app); |
| | | 149 | | |
| | 107 | 150 | | ApplicationAuthenticationOptions options = app.ApplicationServices |
| | 107 | 151 | | .GetRequiredService<IOptions<ApplicationAuthenticationOptions>>() |
| | 107 | 152 | | .Value; |
| | | 153 | | |
| | 100 | 154 | | return !options.Enabled ? app : app.UseAuthentication(); |
| | | 155 | | } |
| | | 156 | | } |