2023-03-26 21:53:17 +00:00
|
|
|
using AspNetCore.Authentication.ApiKey;
|
2022-08-31 13:47:10 +00:00
|
|
|
using Elsa.Common.Features;
|
2022-12-09 11:25:39 +00:00
|
|
|
using Elsa.Extensions;
|
2022-08-31 13:47:10 +00:00
|
|
|
using Elsa.Features.Abstractions;
|
|
|
|
|
using Elsa.Features.Attributes;
|
|
|
|
|
using Elsa.Features.Services;
|
2023-03-03 21:47:23 +00:00
|
|
|
using Elsa.Identity.Contracts;
|
2022-08-31 13:47:10 +00:00
|
|
|
using Elsa.Identity.Entities;
|
|
|
|
|
using Elsa.Identity.Options;
|
2023-03-26 21:53:17 +00:00
|
|
|
using Elsa.Identity.Providers;
|
2022-08-31 13:47:10 +00:00
|
|
|
using Elsa.Identity.Services;
|
2023-03-26 21:53:17 +00:00
|
|
|
using JetBrains.Annotations;
|
2022-08-31 13:47:10 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Identity.Features;
|
|
|
|
|
|
2022-11-23 10:20:35 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Provides identity feature to authenticate & authorize API requests.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
[DependsOn(typeof(SystemClockFeature))]
|
2023-03-26 21:53:17 +00:00
|
|
|
[PublicAPI]
|
2022-08-31 13:47:10 +00:00
|
|
|
public class IdentityFeature : FeatureBase
|
|
|
|
|
{
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <inheritdoc />
|
2022-08-31 13:47:10 +00:00
|
|
|
public IdentityFeature(IModule module) : base(module)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
2023-03-26 21:53:17 +00:00
|
|
|
/// Gets or sets the <see cref="IdentityOptions"/>.
|
2022-12-09 11:25:39 +00:00
|
|
|
/// </summary>
|
2023-03-26 21:53:17 +00:00
|
|
|
public Action<IdentityOptions> IdentityOptions { get; set; } = _ => { };
|
2022-12-30 12:11:29 +00:00
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
2023-03-26 21:53:17 +00:00
|
|
|
/// Gets or sets the <see cref="IdentityTokenOptions"/>.
|
2022-12-09 11:25:39 +00:00
|
|
|
/// </summary>
|
2023-03-26 21:53:17 +00:00
|
|
|
public Action<IdentityTokenOptions> TokenOptions { get; set; } = _ => { };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the <see cref="ApiKeyOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<ApiKeyOptions> ApiKeyOptions { get; set; } = options =>
|
|
|
|
|
{
|
|
|
|
|
options.Realm = "Elsa Workflows";
|
|
|
|
|
options.KeyName = "ApiKey";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that configures the <see cref="UsersOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<UsersOptions> UsersOptions { get; set; } = _ => { };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that configures the <see cref="ApplicationsOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<ApplicationsOptions> ApplicationsOptions { get; set; } = _ => { };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that configures the <see cref="RolesOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<RolesOptions> RolesOptions { get; set; } = _ => { };
|
2022-08-31 13:47:10 +00:00
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IUserStore"/>.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
public Func<IServiceProvider, IUserStore> UserStore { get; set; } = sp => sp.GetRequiredService<MemoryUserStore>();
|
2023-03-26 21:53:17 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IApplicationStore"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, IApplicationStore> ApplicationStore { get; set; } = sp => sp.GetRequiredService<MemoryApplicationStore>();
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IRoleStore"/>.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
public Func<IServiceProvider, IRoleStore> RoleStore { get; set; } = sp => sp.GetRequiredService<MemoryRoleStore>();
|
|
|
|
|
|
2023-03-26 21:53:17 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IUserProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, IUserProvider> UserProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedUserProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IApplicationProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, IApplicationProvider> ApplicationProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedApplicationProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IRoleProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, IRoleProvider> RoleProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedRoleProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseStoreBasedUserProvider() => UserProvider = sp => sp.GetRequiredService<StoreBasedUserProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseConfigurationBasedUserProvider(Action<UsersOptions> configure)
|
2022-12-09 11:25:39 +00:00
|
|
|
{
|
2023-03-26 21:53:17 +00:00
|
|
|
UserProvider = sp => sp.GetRequiredService<ConfigurationBasedUserProvider>();
|
|
|
|
|
UsersOptions += configure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="AdminUserProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseAdminUserProvider() => UserProvider = sp => sp.GetRequiredService<AdminUserProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="StoreBasedApplicationProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseStoreBasedApplicationProvider() => ApplicationProvider = sp => sp.GetRequiredService<StoreBasedApplicationProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="ConfigurationBasedApplicationProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseConfigurationBasedApplicationProvider(Action<ApplicationsOptions> configure)
|
|
|
|
|
{
|
|
|
|
|
ApplicationProvider = sp => sp.GetRequiredService<ConfigurationBasedApplicationProvider>();
|
|
|
|
|
ApplicationsOptions += configure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="StoreBasedRoleProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseStoreBasedRoleProvider() => RoleProvider = sp => sp.GetRequiredService<StoreBasedRoleProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the feature to use <see cref="ConfigurationBasedRoleProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseConfigurationBasedRoleProvider(Action<RolesOptions> configure)
|
|
|
|
|
{
|
|
|
|
|
RoleProvider = sp => sp.GetRequiredService<ConfigurationBasedRoleProvider>();
|
|
|
|
|
RolesOptions += configure;
|
2022-12-09 11:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-03-26 21:53:17 +00:00
|
|
|
public override void Configure()
|
2022-08-31 13:47:10 +00:00
|
|
|
{
|
2023-03-26 21:53:17 +00:00
|
|
|
Module.AddFastEndpointsAssembly(GetType());
|
2022-08-31 13:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <inheritdoc />
|
2022-08-31 13:47:10 +00:00
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2023-03-26 21:53:17 +00:00
|
|
|
Services.Configure(IdentityOptions);
|
|
|
|
|
Services.Configure(TokenOptions);
|
|
|
|
|
Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions);
|
|
|
|
|
Services.Configure(UsersOptions);
|
|
|
|
|
Services.Configure(ApplicationsOptions);
|
|
|
|
|
Services.Configure(RolesOptions);
|
|
|
|
|
|
|
|
|
|
// Memory stores.
|
|
|
|
|
Services
|
|
|
|
|
.AddMemoryStore<User, MemoryUserStore>()
|
|
|
|
|
.AddMemoryStore<Application, MemoryApplicationStore>()
|
|
|
|
|
.AddMemoryStore<Role, MemoryRoleStore>();
|
|
|
|
|
|
|
|
|
|
// User providers.
|
|
|
|
|
Services
|
|
|
|
|
.AddSingleton<AdminUserProvider>()
|
|
|
|
|
.AddSingleton<StoreBasedUserProvider>()
|
|
|
|
|
.AddSingleton<ConfigurationBasedUserProvider>();
|
|
|
|
|
|
|
|
|
|
// Application providers.
|
|
|
|
|
Services
|
|
|
|
|
.AddSingleton<StoreBasedApplicationProvider>()
|
|
|
|
|
.AddSingleton<ConfigurationBasedApplicationProvider>();
|
2023-01-05 12:55:08 +00:00
|
|
|
|
2023-03-26 21:53:17 +00:00
|
|
|
// Role providers.
|
|
|
|
|
Services
|
|
|
|
|
.AddSingleton<StoreBasedRoleProvider>()
|
|
|
|
|
.AddSingleton<ConfigurationBasedRoleProvider>();
|
2022-12-30 12:11:29 +00:00
|
|
|
|
2023-03-26 21:53:17 +00:00
|
|
|
// API Key.
|
|
|
|
|
Services.AddSingleton<IApiKeyProvider, DefaultApiKeyProvider>();
|
|
|
|
|
|
|
|
|
|
// Services.
|
2022-08-31 13:47:10 +00:00
|
|
|
Services
|
|
|
|
|
.AddSingleton(UserStore)
|
2023-03-26 21:53:17 +00:00
|
|
|
.AddSingleton(ApplicationStore)
|
2022-08-31 13:47:10 +00:00
|
|
|
.AddSingleton(RoleStore)
|
2023-03-26 21:53:17 +00:00
|
|
|
.AddSingleton(UserProvider)
|
|
|
|
|
.AddSingleton(ApplicationProvider)
|
|
|
|
|
.AddSingleton(RoleProvider)
|
|
|
|
|
.AddSingleton<ISecretHasher, DefaultSecretHasher>()
|
2022-08-31 13:47:10 +00:00
|
|
|
.AddSingleton<IAccessTokenIssuer, DefaultAccessTokenIssuer>()
|
2023-03-26 21:53:17 +00:00
|
|
|
.AddSingleton<IUserCredentialsValidator, DefaultUserCredentialsValidator>()
|
|
|
|
|
.AddSingleton<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>()
|
|
|
|
|
.AddSingleton<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>())
|
|
|
|
|
.AddSingleton<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>())
|
|
|
|
|
.AddSingleton<IClientIdGenerator, DefaultClientIdGenerator>()
|
|
|
|
|
.AddSingleton<ISecretGenerator, DefaultSecretGenerator>()
|
|
|
|
|
.AddSingleton<IRandomStringGenerator, DefaultRandomStringGenerator>()
|
|
|
|
|
.AddSingleton<DefaultApiKeyGeneratorAndParser>()
|
2022-08-31 13:47:10 +00:00
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
}
|