< 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: 66
Uncovered lines: 0
Coverable lines: 66
Total lines: 115
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
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(...)75%44100%
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.
 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    {
 17629        ArgumentNullException.ThrowIfNull(services);
 17630        ArgumentNullException.ThrowIfNull(configuration);
 31
 17632        services.AddTransient<IClaimsTransformation, ApplicationClaimsTransformation>();
 17633        services.AddSingleton<IValidateOptions<ApplicationAuthenticationOptions>, ApplicationAuthenticationOptionsValida
 34
 17635        services
 17636            .AddOptions<ApplicationAuthenticationOptions>()
 17637            .Bind(configuration.GetSection(ApplicationAuthenticationOptions.SectionName))
 17638            .ValidateOnStart();
 39
 17640        AuthenticationBuilder authenticationBuilder = services.AddAuthentication(options =>
 17641        {
 15242            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 15243            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 15244            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 15245            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 32846        });
 17647        services
 17648            .AddOptions<AuthenticationOptions>()
 17649            .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) =
 17650            {
 15251                ApplicationAuthenticationOptions applicationAuthenticationOptions =
 15252                    applicationAuthenticationOptionsAccessor.Value;
 17653
 15254                if (!applicationAuthenticationOptions.Enabled)
 17655                {
 456                    return;
 17657                }
 17658
 14859                options.DefaultScheme = applicationAuthenticationOptions.DefaultScheme;
 14860                options.DefaultAuthenticateScheme = applicationAuthenticationOptions.DefaultScheme;
 14861                options.DefaultChallengeScheme = applicationAuthenticationOptions.DefaultChallengeScheme;
 14862                options.DefaultSignInScheme = applicationAuthenticationOptions.DefaultSignInScheme;
 32463            });
 64
 17665        authenticationBuilder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
 66
 17667        services
 17668            .AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
 17669            .Configure<IOptions<ApplicationAuthenticationOptions>>((options, applicationAuthenticationOptionsAccessor) =
 17670            {
 8071                ApplicationAuthenticationOptions applicationAuthenticationOptions =
 8072                    applicationAuthenticationOptionsAccessor.Value;
 17673
 8074                options.LoginPath = applicationAuthenticationOptions.Cookie.LoginPath;
 8075                options.LogoutPath = applicationAuthenticationOptions.Cookie.LogoutPath;
 8076                options.AccessDeniedPath = applicationAuthenticationOptions.Cookie.AccessDeniedPath;
 8077                options.ExpireTimeSpan = TimeSpan.FromMinutes(applicationAuthenticationOptions.Cookie.ExpireMinutes);
 8078                options.SlidingExpiration = applicationAuthenticationOptions.Cookie.SlidingExpiration;
 17679
 8080                options.Cookie.Name = ".ProjectTemplate.Web.Authentication";
 8081                options.Cookie.HttpOnly = true;
 8082                options.Cookie.SameSite = SameSiteMode.Lax;
 8083                options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
 25684            });
 85
 17686        ApplicationAuthenticationOptions applicationAuthenticationOptions = configuration
 17687            .GetSection(ApplicationAuthenticationOptions.SectionName)
 17688            .Get<ApplicationAuthenticationOptions>() ?? new ApplicationAuthenticationOptions();
 89
 17690        authenticationBuilder
 17691            .AddOpenIdConnectAuthentication(applicationAuthenticationOptions.Providers.OpenIdConnect)
 17692            .AddSaml2Authentication(applicationAuthenticationOptions.Providers.Saml2)
 17693            .AddMicrosoftAuthentication(applicationAuthenticationOptions.Providers.Microsoft)
 17694            .AddGoogleAuthentication(applicationAuthenticationOptions.Providers.Google)
 17695            .AddGitHubAuthentication(applicationAuthenticationOptions.Providers.GitHub);
 96
 17697        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    {
 142107        ArgumentNullException.ThrowIfNull(app);
 108
 142109        ApplicationAuthenticationOptions options = app.ApplicationServices
 142110            .GetRequiredService<IOptions<ApplicationAuthenticationOptions>>()
 142111            .Value;
 112
 128113        return !options.Enabled ? app : app.UseAuthentication();
 114    }
 115}