< Summary

Information
Class: Elsa.Identity.ShellFeatures.DefaultAuthenticationFeature
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/ShellFeatures/DefaultAuthenticationFeature.cs
Line coverage
78%
Covered lines: 25
Uncovered lines: 7
Coverable lines: 32
Total lines: 97
Line coverage: 78.1%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ApiKeyProviderType()100%11100%
get_AdminApiKey()100%11100%
get_UseDevelopmentAdminApiKey()100%11100%
ConfigureServices(...)75%9875.86%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/ShellFeatures/DefaultAuthenticationFeature.cs

#LineLine coverage
 1using AspNetCore.Authentication.ApiKey;
 2using CShells.Features;
 3using Elsa.Extensions;
 4using Elsa.Identity.Constants;
 5using Elsa.Identity.Options;
 6using Elsa.Identity.Providers;
 7using Elsa.Platform.PackageManifest.Generator.Hints;
 8using JetBrains.Annotations;
 9using Microsoft.AspNetCore.Authentication.JwtBearer;
 10using Microsoft.AspNetCore.Authorization;
 11using Microsoft.Extensions.DependencyInjection;
 12
 13namespace Elsa.Identity.ShellFeatures;
 14
 15/// <summary>
 16/// Provides an authorization feature that configures the system with JWT bearer and API key authentication.
 17/// </summary>
 18[ManifestFeatureCategory("Identity")]
 19[ManifestFeatureCategory("Security")]
 20[ShellFeature(
 21    DisplayName = "Default Authentication",
 22    Description = "Provides JWT bearer and API key authentication",
 23    DependsOn = [typeof(IdentityFeature)])]
 24[UsedImplicitly]
 25public class DefaultAuthenticationFeature : IShellFeature
 26{
 27    private const string MultiScheme = "Jwt-or-ApiKey";
 28
 29    /// <summary>
 30    /// Gets or sets the API key provider type.
 31    /// </summary>
 1632    public Type ApiKeyProviderType { get; set; } = typeof(DefaultApiKeyProvider);
 33
 34    /// <summary>
 35    /// Gets or sets an explicit API key for <see cref="AdminApiKeyProvider"/>. Leave empty to disable the provider.
 36    /// </summary>
 37    [ManifestSetting(
 38        DisplayName = "Admin API Key",
 39        Description = "Explicit API key for the admin API key provider. Leave empty to disable built-in admin API key au
 40        Category = "Security",
 41        Secret = true,
 42        Sensitive = true,
 43        RestartRequired = true)]
 744    public string AdminApiKey { get; set; } = "";
 45
 46    /// <summary>
 47    /// Gets or sets whether the all-zero development admin API key should be enabled. Do not enable in production.
 48    /// </summary>
 49    [ManifestSetting(
 50        DisplayName = "Use Development Admin API Key",
 51        Description = "Enables the all-zero development admin API key. Do not enable in production.",
 52        Category = "Security",
 53        DefaultValue = "false",
 54        RestartRequired = true)]
 455    public bool UseDevelopmentAdminApiKey { get; set; }
 56
 57    public void ConfigureServices(IServiceCollection services)
 58    {
 359        var resolvedAdminApiKey = UseDevelopmentAdminApiKey ? AdminApiKeyProvider.DevelopmentApiKey : AdminApiKey;
 360        if (!string.IsNullOrWhiteSpace(resolvedAdminApiKey))
 261            ApiKeyProviderType = typeof(AdminApiKeyProvider);
 62
 363        services.ConfigureOptions<ConfigureJwtBearerOptions>();
 364        services.AddIdentityTokenOptionsValidation();
 365        services.Configure<AdminApiKeyOptions>(options =>
 366        {
 367            options.ApiKey = resolvedAdminApiKey;
 668        });
 69
 370        var authBuilder = services
 371            .AddAuthentication(MultiScheme)
 372            .AddPolicyScheme(MultiScheme, MultiScheme, options =>
 373            {
 074                options.ForwardDefaultSelector = context =>
 075                {
 076                    return context.Request.Headers.Authorization.Any(x => x!.Contains(ApiKeyDefaults.AuthenticationSchem
 077                        ? ApiKeyDefaults.AuthenticationScheme
 078                        : JwtBearerDefaults.AuthenticationScheme;
 079                };
 080            })
 381            .AddJwtBearer()
 382            .AddJwtBearer(IdentityAuthenticationSchemes.RefreshToken);
 83
 84        // Configure API key authorization based on provider type
 385        if (ApiKeyProviderType == typeof(AdminApiKeyProvider))
 286            authBuilder.AddApiKeyInAuthorizationHeader<AdminApiKeyProvider>();
 87        else
 188            authBuilder.AddApiKeyInAuthorizationHeader<DefaultApiKeyProvider>();
 89
 390        services.AddScoped(ApiKeyProviderType);
 591        services.AddScoped<IApiKeyProvider>(sp => (IApiKeyProvider)sp.GetRequiredService(ApiKeyProviderType));
 92
 93        // No SecurityRoot policy: it was retired in favour of endpoint permissions (ADR 0010). Authorization
 94        // services are still registered so the permission requirement handler runs.
 395        services.AddAuthorization();
 396    }
 97}