< Summary

Information
Class: Elsa.Identity.Features.IdentityFeature
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Features/IdentityFeature.cs
Line coverage
90%
Covered lines: 70
Uncovered lines: 7
Coverable lines: 77
Total lines: 205
Line coverage: 90.9%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Features/IdentityFeature.cs

#LineLine coverage
 1using AspNetCore.Authentication.ApiKey;
 2using Elsa.Common.Features;
 3using Elsa.Common.Multitenancy;
 4using Elsa.Extensions;
 5using Elsa.Features.Abstractions;
 6using Elsa.Features.Attributes;
 7using Elsa.Features.Services;
 8using Elsa.Identity.Contracts;
 9using Elsa.Identity.Entities;
 10using Elsa.Identity.Multitenancy;
 11using Elsa.Identity.Options;
 12using Elsa.Identity.Providers;
 13using Elsa.Identity.Services;
 14using JetBrains.Annotations;
 15using Microsoft.Extensions.DependencyInjection;
 16
 17namespace Elsa.Identity.Features;
 18
 19/// <summary>
 20/// Provides identity feature to authenticate &amp; authorize API requests.
 21/// </summary>
 22[DependsOn(typeof(SystemClockFeature))]
 23[PublicAPI]
 24public class IdentityFeature : FeatureBase
 25{
 26    /// <inheritdoc />
 127    public IdentityFeature(IModule module) : base(module)
 28    {
 129    }
 30
 31    /// <summary>
 32    /// Gets or sets the <see cref="IdentityTokenOptions"/>.
 33    /// </summary>
 434    public Action<IdentityTokenOptions> TokenOptions { get; set; } = _ => { };
 35
 36    /// <summary>
 37    /// Gets or sets the <see cref="ApiKeyOptions"/>.
 38    /// </summary>
 239    public Action<ApiKeyOptions> ApiKeyOptions { get; set; } = options =>
 140    {
 141        options.Realm = "Elsa Workflows";
 142        options.KeyName = "ApiKey";
 243    };
 44
 45    /// <summary>
 46    /// A delegate that configures the <see cref="UsersOptions"/>.
 47    /// </summary>
 448    public Action<UsersOptions> UsersOptions { get; set; } = _ => { };
 49
 50    /// <summary>
 51    /// A delegate that configures the <see cref="ApplicationsOptions"/>.
 52    /// </summary>
 453    public Action<ApplicationsOptions> ApplicationsOptions { get; set; } = _ => { };
 54
 55    /// <summary>
 56    /// A delegate that configures the <see cref="RolesOptions"/>.
 57    /// </summary>
 458    public Action<RolesOptions> RolesOptions { get; set; } = _ => { };
 59
 60    /// <summary>
 61    /// A delegate that creates an instance of an implementation of <see cref="IUserStore"/>.
 62    /// </summary>
 363    public Func<IServiceProvider, IUserStore> UserStore { get; set; } = sp => sp.GetRequiredService<MemoryUserStore>();
 64
 65    /// <summary>
 66    /// A delegate that creates an instance of an implementation of <see cref="IApplicationStore"/>.
 67    /// </summary>
 368    public Func<IServiceProvider, IApplicationStore> ApplicationStore { get; set; } = sp => sp.GetRequiredService<Memory
 69
 70    /// <summary>
 71    /// A delegate that creates an instance of an implementation of <see cref="IRoleStore"/>.
 72    /// </summary>
 373    public Func<IServiceProvider, IRoleStore> RoleStore { get; set; } = sp => sp.GetRequiredService<MemoryRoleStore>();
 74
 75    /// <summary>
 76    /// A delegate that creates an instance of an implementation of <see cref="IUserProvider"/>.
 77    /// </summary>
 378    public Func<IServiceProvider, IUserProvider> UserProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedUser
 79
 80    /// <summary>
 81    /// A delegate that creates an instance of an implementation of <see cref="IApplicationProvider"/>.
 82    /// </summary>
 383    public Func<IServiceProvider, IApplicationProvider> ApplicationProvider { get; set; } = sp => sp.GetRequiredService<
 84
 85    /// <summary>
 86    /// A delegate that creates an instance of an implementation of <see cref="IRoleProvider"/>.
 87    /// </summary>
 388    public Func<IServiceProvider, IRoleProvider> RoleProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedRole
 89
 90    /// <summary>
 91    /// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>.
 92    /// </summary>
 093    public void UseStoreBasedUserProvider() => UserProvider = sp => sp.GetRequiredService<StoreBasedUserProvider>();
 94
 95    /// <summary>
 96    /// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>.
 97    /// </summary>
 98    public void UseConfigurationBasedUserProvider(Action<UsersOptions> configure)
 99    {
 2100        UserProvider = sp => sp.GetRequiredService<ConfigurationBasedUserProvider>();
 1101        UsersOptions += configure;
 1102    }
 103
 104    /// <summary>
 105    /// Configures the feature to use <see cref="AdminUserProvider"/>.
 106    /// </summary>
 107    public void UseAdminUserProvider()
 108    {
 0109        UserProvider = sp => sp.GetRequiredService<AdminUserProvider>();
 0110        RoleProvider = sp => sp.GetRequiredService<AdminRoleProvider>();
 0111    }
 112
 113    /// <summary>
 114    /// Configures the feature to use <see cref="StoreBasedApplicationProvider"/>.
 115    /// </summary>
 0116    public void UseStoreBasedApplicationProvider() => ApplicationProvider = sp => sp.GetRequiredService<StoreBasedApplic
 117
 118    /// <summary>
 119    /// Configures the feature to use <see cref="ConfigurationBasedApplicationProvider"/>.
 120    /// </summary>
 121    public void UseConfigurationBasedApplicationProvider(Action<ApplicationsOptions> configure)
 122    {
 2123        ApplicationProvider = sp => sp.GetRequiredService<ConfigurationBasedApplicationProvider>();
 1124        ApplicationsOptions += configure;
 1125    }
 126
 127    /// <summary>
 128    /// Configures the feature to use <see cref="StoreBasedRoleProvider"/>.
 129    /// </summary>
 0130    public void UseStoreBasedRoleProvider() => RoleProvider = sp => sp.GetRequiredService<StoreBasedRoleProvider>();
 131
 132    /// <summary>
 133    /// Configures the feature to use <see cref="ConfigurationBasedRoleProvider"/>.
 134    /// </summary>
 135    public void UseConfigurationBasedRoleProvider(Action<RolesOptions> configure)
 136    {
 2137        RoleProvider = sp => sp.GetRequiredService<ConfigurationBasedRoleProvider>();
 1138        RolesOptions += configure;
 1139    }
 140
 141    /// <inheritdoc />
 142    public override void Configure()
 143    {
 1144        Module.AddFastEndpointsAssembly(GetType());
 1145    }
 146
 147    /// <inheritdoc />
 148    public override void Apply()
 149    {
 1150        Services.Configure(TokenOptions);
 1151        Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions);
 1152        Services.Configure(UsersOptions);
 1153        Services.Configure(ApplicationsOptions);
 1154        Services.Configure(RolesOptions);
 155
 156        // Memory stores.
 1157        Services
 1158            .AddMemoryStore<User, MemoryUserStore>()
 1159            .AddMemoryStore<Application, MemoryApplicationStore>()
 1160            .AddMemoryStore<Role, MemoryRoleStore>();
 161
 162        // User providers.
 1163        Services
 1164            .AddScoped<AdminUserProvider>()
 1165            .AddScoped<StoreBasedUserProvider>()
 1166            .AddScoped<ConfigurationBasedUserProvider>();
 167
 168        // Application providers.
 1169        Services
 1170            .AddScoped<StoreBasedApplicationProvider>()
 1171            .AddScoped<ConfigurationBasedApplicationProvider>();
 172
 173        // Role providers.
 1174        Services
 1175            .AddScoped<AdminRoleProvider>()
 1176            .AddScoped<StoreBasedRoleProvider>()
 1177            .AddScoped<ConfigurationBasedRoleProvider>();
 178
 179        // Tenant resolution strategies.
 1180        Services
 1181            .AddScoped<ITenantResolver, ClaimsTenantResolver>()
 1182            .AddScoped<ITenantResolver, CurrentUserTenantResolver>();
 183
 184        // Services.
 1185        Services
 1186            .AddScoped(UserStore)
 1187            .AddScoped(ApplicationStore)
 1188            .AddScoped(RoleStore)
 1189            .AddScoped(UserProvider)
 1190            .AddScoped(ApplicationProvider)
 1191            .AddScoped(RoleProvider)
 1192            .AddScoped<ISecretHasher, DefaultSecretHasher>()
 1193            .AddScoped<IAccessTokenIssuer, DefaultAccessTokenIssuer>()
 1194            .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>()
 1195            .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>()
 1196            .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>())
 0197            .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>())
 1198            .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>()
 1199            .AddScoped<ISecretGenerator, DefaultSecretGenerator>()
 1200            .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>()
 1201            .AddScoped<DefaultApiKeyGeneratorAndParser>()
 1202            .AddHttpContextAccessor()
 1203            ;
 1204    }
 205}