< 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
73%
Covered lines: 31
Uncovered lines: 11
Coverable lines: 42
Total lines: 109
Line coverage: 73.8%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
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%
get_EnableLocalHostPermissionGrant()100%210%
ConfigureServices(...)60%121073.68%

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.Options;
 8using Elsa.PackageManifest.Generator.Hints;
 9using Elsa.Requirements;
 10using JetBrains.Annotations;
 11using Microsoft.AspNetCore.Authentication.JwtBearer;
 12using Microsoft.AspNetCore.Authorization;
 13using Microsoft.Extensions.DependencyInjection;
 14
 15namespace Elsa.Identity.ShellFeatures;
 16
 17/// <summary>
 18/// Provides an authorization feature that configures the system with JWT bearer and API key authentication.
 19/// </summary>
 20[ShellFeature(
 21    DisplayName = "Default Authentication",
 22    Description = "Provides JWT bearer and API key authentication",
 23    DependsOn = ["Identity"])]
 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    /// <summary>
 58    /// Gets or sets whether localhost requests may satisfy the security-root permission requirement without other crede
 59    /// </summary>
 060    public bool EnableLocalHostPermissionGrant { get; set; }
 61
 62    public void ConfigureServices(IServiceCollection services)
 63    {
 364        var resolvedAdminApiKey = UseDevelopmentAdminApiKey ? AdminApiKeyProvider.DevelopmentApiKey : AdminApiKey;
 365        if (!string.IsNullOrWhiteSpace(resolvedAdminApiKey))
 266            ApiKeyProviderType = typeof(AdminApiKeyProvider);
 67
 368        services.ConfigureOptions<ConfigureJwtBearerOptions>();
 369        services.AddIdentityTokenOptionsValidation();
 370        services.Configure<LocalHostPermissionRequirementOptions>(options => options.EnableLocalHostPermissionGrant = En
 371        services.Configure<AdminApiKeyOptions>(options =>
 372        {
 373            options.ApiKey = resolvedAdminApiKey;
 674        });
 75
 376        var authBuilder = services
 377            .AddAuthentication(MultiScheme)
 378            .AddPolicyScheme(MultiScheme, MultiScheme, options =>
 379            {
 080                options.ForwardDefaultSelector = context =>
 081                {
 082                    return context.Request.Headers.Authorization.Any(x => x!.Contains(ApiKeyDefaults.AuthenticationSchem
 083                        ? ApiKeyDefaults.AuthenticationScheme
 084                        : JwtBearerDefaults.AuthenticationScheme;
 085                };
 086            })
 387            .AddJwtBearer()
 388            .AddJwtBearer(IdentityAuthenticationSchemes.RefreshToken);
 89
 90        // Configure API key authorization based on provider type
 391        if (ApiKeyProviderType == typeof(AdminApiKeyProvider))
 292            authBuilder.AddApiKeyInAuthorizationHeader<AdminApiKeyProvider>();
 93        else
 194            authBuilder.AddApiKeyInAuthorizationHeader<DefaultApiKeyProvider>();
 95
 396        services.AddScoped<IAuthorizationHandler, LocalHostRequirementHandler>();
 397        services.AddScoped<IAuthorizationHandler, LocalHostPermissionRequirementHandler>();
 398        services.AddScoped(ApiKeyProviderType);
 599        services.AddScoped<IApiKeyProvider>(sp => (IApiKeyProvider)sp.GetRequiredService(ApiKeyProviderType));
 100
 3101        services.AddAuthorization(options =>
 3102        {
 0103            if (EnableLocalHostPermissionGrant)
 0104                options.AddPolicy(IdentityPolicyNames.SecurityRoot, policy => policy.AddRequirements(new LocalHostPermis
 3105            else
 0106                options.AddPolicy(IdentityPolicyNames.SecurityRoot, policy => policy.RequireAuthenticatedUser());
 3107        });
 3108    }
 109}