< Summary

Information
Class: ProjectTemplate.Web.Authentication.Extensions.AuthenticationServiceExtensions
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Authentication/Extensions/AuthenticationServiceExtensions.cs
Line coverage
100%
Covered lines: 71
Uncovered lines: 0
Coverable lines: 71
Total lines: 135
Line coverage: 100%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddApplicationAuthentication(...)100%11100%
AddApplicationAuthentication(...)80%1010100%
UseApplicationAuthentication(...)100%22100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Authentication/Extensions/AuthenticationServiceExtensions.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authentication;
 2using Microsoft.AspNetCore.Authentication.Cookies;
 3using Microsoft.Extensions.Options;
 4using ProjectTemplate.Web.Authentication.Claims;
 5using ProjectTemplate.Web.Authentication.Options;
 6using ProjectTemplate.Web.Authentication.Providers.GitHub;
 7using ProjectTemplate.Web.Authentication.Providers.Google;
 8using ProjectTemplate.Web.Authentication.Providers.Microsoft;
 9using ProjectTemplate.Web.Authentication.Providers.OpenIdConnect;
 10using ProjectTemplate.Web.Authentication.Providers.Saml2;
 11
 12namespace ProjectTemplate.Web.Authentication.Extensions;
 13
 14/// <summary>
 15/// Provides extension methods for registering and applying application authentication services.
 16/// </summary>
 17public 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    {
 2830        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    {
 19445        ArgumentNullException.ThrowIfNull(services);
 19446        ArgumentNullException.ThrowIfNull(configuration);
 47
 19448        services.AddTransient<IClaimsTransformation, ApplicationClaimsTransformation>();
 19449        services.AddSingleton<IValidateOptions<ApplicationAuthenticationOptions>>(
 19450            new ApplicationAuthenticationOptionsValidator(environment));
 51
 19452        services
 19453            .AddOptions<ApplicationAuthenticationOptions>()
 19454            .Bind(configuration.GetSection(ApplicationAuthenticationOptions.SectionName))
 19455            .ValidateOnStart();
 56
 19457        AuthenticationBuilder authenticationBuilder = services.AddAuthentication(options =>
 19458        {
 16259            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 16260            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 16261            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 16262            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 35663        });
 19464        services
 19465            .AddOptions<AuthenticationOptions>()
 19466            .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) =
 19467            {
 16268                ApplicationAuthenticationOptions applicationAuthenticationOptions =
 16269                    applicationAuthenticationOptionsAccessor.Value;
 19470
 16271                if (!applicationAuthenticationOptions.Enabled)
 19472                {
 473                    return;
 19474                }
 19475
 15876                options.DefaultScheme = applicationAuthenticationOptions.DefaultScheme;
 15877                options.DefaultAuthenticateScheme = applicationAuthenticationOptions.DefaultScheme;
 15878                options.DefaultChallengeScheme = applicationAuthenticationOptions.DefaultChallengeScheme;
 15879                options.DefaultSignInScheme = applicationAuthenticationOptions.DefaultSignInScheme;
 35280            });
 81
 19482        authenticationBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
 83
 19484        services
 19485            .AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
 19486            .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) =
 19487            {
 9288                ApplicationAuthenticationOptions applicationAuthenticationOptions =
 9289                    applicationAuthenticationOptionsAccessor.Value;
 19490
 9291                options.LoginPath = applicationAuthenticationOptions.Cookie.LoginPath;
 9292                options.LogoutPath = applicationAuthenticationOptions.Cookie.LogoutPath;
 9293                options.AccessDeniedPath = applicationAuthenticationOptions.Cookie.AccessDeniedPath;
 9294                options.ExpireTimeSpan = TimeSpan.FromMinutes(applicationAuthenticationOptions.Cookie.ExpireMinutes);
 9295                options.SlidingExpiration = applicationAuthenticationOptions.Cookie.SlidingExpiration;
 19496
 9297                options.Cookie.Name = ".ProjectTemplate.Web.Authentication";
 9298                options.Cookie.HttpOnly = true;
 9299                options.Cookie.SameSite = SameSiteMode.Lax;
 92100                options.Cookie.SecurePolicy = applicationAuthenticationOptions.Cookie.AllowInsecureHttp &&
 92101                    environment?.IsDevelopment() == true
 92102                        ? CookieSecurePolicy.SameAsRequest
 92103                        : CookieSecurePolicy.Always;
 286104            });
 105
 194106        ApplicationAuthenticationOptions applicationAuthenticationOptions = configuration
 194107            .GetSection(ApplicationAuthenticationOptions.SectionName)
 194108            .Get<ApplicationAuthenticationOptions>() ?? new ApplicationAuthenticationOptions();
 109
 194110        authenticationBuilder
 194111            .AddOpenIdConnectAuthentication(applicationAuthenticationOptions.Providers.OpenIdConnect)
 194112            .AddSaml2Authentication(applicationAuthenticationOptions.Providers.Saml2)
 194113            .AddMicrosoftAuthentication(applicationAuthenticationOptions.Providers.Microsoft)
 194114            .AddGoogleAuthentication(applicationAuthenticationOptions.Providers.Google)
 194115            .AddGitHubAuthentication(applicationAuthenticationOptions.Providers.GitHub);
 116
 194117        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    {
 154127        ArgumentNullException.ThrowIfNull(app);
 128
 154129        ApplicationAuthenticationOptions options = app.ApplicationServices
 154130            .GetRequiredService<IOptions<ApplicationAuthenticationOptions>>()
 154131            .Value;
 132
 140133        return !options.Enabled ? app : app.UseAuthentication();
 134    }
 135}