| | | 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. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="services">The service collection to add authentication services to.</param> |
| | | 23 | | /// <param name="configuration">The application configuration source.</param> |
| | | 24 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 25 | | public static IServiceCollection AddApplicationAuthentication( |
| | | 26 | | this IServiceCollection services, |
| | | 27 | | IConfiguration configuration) |
| | | 28 | | { |
| | 176 | 29 | | ArgumentNullException.ThrowIfNull(services); |
| | 176 | 30 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 31 | | |
| | 176 | 32 | | services.AddTransient<IClaimsTransformation, ApplicationClaimsTransformation>(); |
| | 176 | 33 | | services.AddSingleton<IValidateOptions<ApplicationAuthenticationOptions>, ApplicationAuthenticationOptionsValida |
| | | 34 | | |
| | 176 | 35 | | services |
| | 176 | 36 | | .AddOptions<ApplicationAuthenticationOptions>() |
| | 176 | 37 | | .Bind(configuration.GetSection(ApplicationAuthenticationOptions.SectionName)) |
| | 176 | 38 | | .ValidateOnStart(); |
| | | 39 | | |
| | 176 | 40 | | AuthenticationBuilder authenticationBuilder = services.AddAuthentication(options => |
| | 176 | 41 | | { |
| | 152 | 42 | | options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 152 | 43 | | options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 152 | 44 | | options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 152 | 45 | | options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 328 | 46 | | }); |
| | 176 | 47 | | services |
| | 176 | 48 | | .AddOptions<AuthenticationOptions>() |
| | 176 | 49 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 176 | 50 | | { |
| | 152 | 51 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 152 | 52 | | applicationAuthenticationOptionsAccessor.Value; |
| | 176 | 53 | | |
| | 152 | 54 | | if (!applicationAuthenticationOptions.Enabled) |
| | 176 | 55 | | { |
| | 4 | 56 | | return; |
| | 176 | 57 | | } |
| | 176 | 58 | | |
| | 148 | 59 | | options.DefaultScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 148 | 60 | | options.DefaultAuthenticateScheme = applicationAuthenticationOptions.DefaultScheme; |
| | 148 | 61 | | options.DefaultChallengeScheme = applicationAuthenticationOptions.DefaultChallengeScheme; |
| | 148 | 62 | | options.DefaultSignInScheme = applicationAuthenticationOptions.DefaultSignInScheme; |
| | 324 | 63 | | }); |
| | | 64 | | |
| | 176 | 65 | | authenticationBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme); |
| | | 66 | | |
| | 176 | 67 | | services |
| | 176 | 68 | | .AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme) |
| | 176 | 69 | | .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) = |
| | 176 | 70 | | { |
| | 80 | 71 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = |
| | 80 | 72 | | applicationAuthenticationOptionsAccessor.Value; |
| | 176 | 73 | | |
| | 80 | 74 | | options.LoginPath = applicationAuthenticationOptions.Cookie.LoginPath; |
| | 80 | 75 | | options.LogoutPath = applicationAuthenticationOptions.Cookie.LogoutPath; |
| | 80 | 76 | | options.AccessDeniedPath = applicationAuthenticationOptions.Cookie.AccessDeniedPath; |
| | 80 | 77 | | options.ExpireTimeSpan = TimeSpan.FromMinutes(applicationAuthenticationOptions.Cookie.ExpireMinutes); |
| | 80 | 78 | | options.SlidingExpiration = applicationAuthenticationOptions.Cookie.SlidingExpiration; |
| | 176 | 79 | | |
| | 80 | 80 | | options.Cookie.Name = ".ProjectTemplate.Web.Authentication"; |
| | 80 | 81 | | options.Cookie.HttpOnly = true; |
| | 80 | 82 | | options.Cookie.SameSite = SameSiteMode.Lax; |
| | 80 | 83 | | options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; |
| | 256 | 84 | | }); |
| | | 85 | | |
| | 176 | 86 | | ApplicationAuthenticationOptions applicationAuthenticationOptions = configuration |
| | 176 | 87 | | .GetSection(ApplicationAuthenticationOptions.SectionName) |
| | 176 | 88 | | .Get<ApplicationAuthenticationOptions>() ?? new ApplicationAuthenticationOptions(); |
| | | 89 | | |
| | 176 | 90 | | authenticationBuilder |
| | 176 | 91 | | .AddOpenIdConnectAuthentication(applicationAuthenticationOptions.Providers.OpenIdConnect) |
| | 176 | 92 | | .AddSaml2Authentication(applicationAuthenticationOptions.Providers.Saml2) |
| | 176 | 93 | | .AddMicrosoftAuthentication(applicationAuthenticationOptions.Providers.Microsoft) |
| | 176 | 94 | | .AddGoogleAuthentication(applicationAuthenticationOptions.Providers.Google) |
| | 176 | 95 | | .AddGitHubAuthentication(applicationAuthenticationOptions.Providers.GitHub); |
| | | 96 | | |
| | 176 | 97 | | return services; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Applies application authentication middleware when authentication is enabled. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="app">The application builder.</param> |
| | | 104 | | /// <returns>The same <see cref="IApplicationBuilder"/> instance for chaining.</returns> |
| | | 105 | | public static IApplicationBuilder UseApplicationAuthentication(this IApplicationBuilder app) |
| | | 106 | | { |
| | 142 | 107 | | ArgumentNullException.ThrowIfNull(app); |
| | | 108 | | |
| | 142 | 109 | | ApplicationAuthenticationOptions options = app.ApplicationServices |
| | 142 | 110 | | .GetRequiredService<IOptions<ApplicationAuthenticationOptions>>() |
| | 142 | 111 | | .Value; |
| | | 112 | | |
| | 128 | 113 | | return !options.Enabled ? app : app.UseAuthentication(); |
| | | 114 | | } |
| | | 115 | | } |