using AspNetCore.Authentication.ApiKey;
using Elsa.Common.Features;
using Elsa.Common.Multitenancy;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Identity.Contracts;
using Elsa.Identity.Entities;
using Elsa.Identity.Multitenancy;
using Elsa.Identity.Options;
using Elsa.Identity.Providers;
using Elsa.Identity.Services;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Identity.Features;
///
/// Provides identity feature to authenticate & authorize API requests.
///
[DependsOn(typeof(SystemClockFeature))]
[PublicAPI]
public class IdentityFeature : FeatureBase
{
///
public IdentityFeature(IModule module) : base(module)
{
}
///
/// Gets or sets the .
///
public Action TokenOptions { get; set; } = _ => { };
///
/// Gets or sets the .
///
public Action ApiKeyOptions { get; set; } = options =>
{
options.Realm = "Elsa Workflows";
options.KeyName = "ApiKey";
};
///
/// A delegate that configures the .
///
public Action UsersOptions { get; set; } = _ => { };
///
/// A delegate that configures the .
///
public Action ApplicationsOptions { get; set; } = _ => { };
///
/// A delegate that configures the .
///
public Action RolesOptions { get; set; } = _ => { };
///
/// A delegate that creates an instance of an implementation of .
///
public Func UserStore { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func ApplicationStore { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func RoleStore { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func UserProvider { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func ApplicationProvider { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func RoleProvider { get; set; } = sp => sp.GetRequiredService();
///
/// Configures the feature to use .
///
public void UseStoreBasedUserProvider() => UserProvider = sp => sp.GetRequiredService();
///
/// Configures the feature to use .
///
public void UseConfigurationBasedUserProvider(Action configure)
{
UserProvider = sp => sp.GetRequiredService();
UsersOptions += configure;
}
///
/// Configures the feature to use .
///
public void UseAdminUserProvider()
{
UserProvider = sp => sp.GetRequiredService();
RoleProvider = sp => sp.GetRequiredService();
}
///
/// Configures the feature to use .
///
public void UseStoreBasedApplicationProvider() => ApplicationProvider = sp => sp.GetRequiredService();
///
/// Configures the feature to use .
///
public void UseConfigurationBasedApplicationProvider(Action configure)
{
ApplicationProvider = sp => sp.GetRequiredService();
ApplicationsOptions += configure;
}
///
/// Configures the feature to use .
///
public void UseStoreBasedRoleProvider() => RoleProvider = sp => sp.GetRequiredService();
///
/// Configures the feature to use .
///
public void UseConfigurationBasedRoleProvider(Action configure)
{
RoleProvider = sp => sp.GetRequiredService();
RolesOptions += configure;
}
///
public override void Configure()
{
Module.AddFastEndpointsAssembly(GetType());
}
///
public override void Apply()
{
Services.Configure(TokenOptions);
Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions);
Services.Configure(UsersOptions);
Services.Configure(ApplicationsOptions);
Services.Configure(RolesOptions);
// Memory stores.
Services
.AddMemoryStore()
.AddMemoryStore()
.AddMemoryStore();
// User providers.
Services
.AddScoped()
.AddScoped()
.AddScoped();
// Application providers.
Services
.AddScoped()
.AddScoped();
// Role providers.
Services
.AddScoped()
.AddScoped()
.AddScoped();
// Tenant resolution strategies.
Services
.AddScoped()
.AddScoped();
// Services.
Services
.AddScoped(UserStore)
.AddScoped(ApplicationStore)
.AddScoped(RoleStore)
.AddScoped(UserProvider)
.AddScoped(ApplicationProvider)
.AddScoped(RoleProvider)
.AddScoped()
.AddScoped()
.AddScoped()
.AddScoped()
.AddScoped(sp => sp.GetRequiredService())
.AddScoped(sp => sp.GetRequiredService())
.AddScoped()
.AddScoped()
.AddScoped()
.AddScoped()
;
}
}