| | | 1 | | using AspNetCore.Authentication.ApiKey; |
| | | 2 | | using Elsa.Common.Features; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Features.Abstractions; |
| | | 6 | | using Elsa.Features.Attributes; |
| | | 7 | | using Elsa.Features.Services; |
| | | 8 | | using Elsa.Identity.Contracts; |
| | | 9 | | using Elsa.Identity.Entities; |
| | | 10 | | using Elsa.Identity.Multitenancy; |
| | | 11 | | using Elsa.Identity.Options; |
| | | 12 | | using Elsa.Identity.Providers; |
| | | 13 | | using Elsa.Identity.Services; |
| | | 14 | | using JetBrains.Annotations; |
| | | 15 | | using Microsoft.Extensions.DependencyInjection; |
| | | 16 | | |
| | | 17 | | namespace Elsa.Identity.Features; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Provides identity feature to authenticate & authorize API requests. |
| | | 21 | | /// </summary> |
| | | 22 | | [DependsOn(typeof(SystemClockFeature))] |
| | | 23 | | [PublicAPI] |
| | | 24 | | public class IdentityFeature : FeatureBase |
| | | 25 | | { |
| | | 26 | | /// <inheritdoc /> |
| | 1 | 27 | | public IdentityFeature(IModule module) : base(module) |
| | | 28 | | { |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the <see cref="IdentityTokenOptions"/>. |
| | | 33 | | /// </summary> |
| | 4 | 34 | | public Action<IdentityTokenOptions> TokenOptions { get; set; } = _ => { }; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the <see cref="ApiKeyOptions"/>. |
| | | 38 | | /// </summary> |
| | 2 | 39 | | public Action<ApiKeyOptions> ApiKeyOptions { get; set; } = options => |
| | 1 | 40 | | { |
| | 1 | 41 | | options.Realm = "Elsa Workflows"; |
| | 1 | 42 | | options.KeyName = "ApiKey"; |
| | 2 | 43 | | }; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// A delegate that configures the <see cref="UsersOptions"/>. |
| | | 47 | | /// </summary> |
| | 4 | 48 | | public Action<UsersOptions> UsersOptions { get; set; } = _ => { }; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// A delegate that configures the <see cref="ApplicationsOptions"/>. |
| | | 52 | | /// </summary> |
| | 4 | 53 | | public Action<ApplicationsOptions> ApplicationsOptions { get; set; } = _ => { }; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// A delegate that configures the <see cref="RolesOptions"/>. |
| | | 57 | | /// </summary> |
| | 4 | 58 | | 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> |
| | 3 | 63 | | 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> |
| | 3 | 68 | | 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> |
| | 3 | 73 | | 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> |
| | 3 | 78 | | 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> |
| | 3 | 83 | | 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> |
| | 3 | 88 | | 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> |
| | 0 | 93 | | 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 | | { |
| | 2 | 100 | | UserProvider = sp => sp.GetRequiredService<ConfigurationBasedUserProvider>(); |
| | 1 | 101 | | UsersOptions += configure; |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Configures the feature to use <see cref="AdminUserProvider"/>. |
| | | 106 | | /// </summary> |
| | | 107 | | public void UseAdminUserProvider() |
| | | 108 | | { |
| | 0 | 109 | | UserProvider = sp => sp.GetRequiredService<AdminUserProvider>(); |
| | 0 | 110 | | RoleProvider = sp => sp.GetRequiredService<AdminRoleProvider>(); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Configures the feature to use <see cref="StoreBasedApplicationProvider"/>. |
| | | 115 | | /// </summary> |
| | 0 | 116 | | 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 | | { |
| | 2 | 123 | | ApplicationProvider = sp => sp.GetRequiredService<ConfigurationBasedApplicationProvider>(); |
| | 1 | 124 | | ApplicationsOptions += configure; |
| | 1 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Configures the feature to use <see cref="StoreBasedRoleProvider"/>. |
| | | 129 | | /// </summary> |
| | 0 | 130 | | 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 | | { |
| | 2 | 137 | | RoleProvider = sp => sp.GetRequiredService<ConfigurationBasedRoleProvider>(); |
| | 1 | 138 | | RolesOptions += configure; |
| | 1 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc /> |
| | | 142 | | public override void Configure() |
| | | 143 | | { |
| | 1 | 144 | | Module.AddFastEndpointsAssembly(GetType()); |
| | 1 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc /> |
| | | 148 | | public override void Apply() |
| | | 149 | | { |
| | 1 | 150 | | Services.Configure(TokenOptions); |
| | 1 | 151 | | Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions); |
| | 1 | 152 | | Services.Configure(UsersOptions); |
| | 1 | 153 | | Services.Configure(ApplicationsOptions); |
| | 1 | 154 | | Services.Configure(RolesOptions); |
| | | 155 | | |
| | | 156 | | // Memory stores. |
| | 1 | 157 | | Services |
| | 1 | 158 | | .AddMemoryStore<User, MemoryUserStore>() |
| | 1 | 159 | | .AddMemoryStore<Application, MemoryApplicationStore>() |
| | 1 | 160 | | .AddMemoryStore<Role, MemoryRoleStore>(); |
| | | 161 | | |
| | | 162 | | // User providers. |
| | 1 | 163 | | Services |
| | 1 | 164 | | .AddScoped<AdminUserProvider>() |
| | 1 | 165 | | .AddScoped<StoreBasedUserProvider>() |
| | 1 | 166 | | .AddScoped<ConfigurationBasedUserProvider>(); |
| | | 167 | | |
| | | 168 | | // Application providers. |
| | 1 | 169 | | Services |
| | 1 | 170 | | .AddScoped<StoreBasedApplicationProvider>() |
| | 1 | 171 | | .AddScoped<ConfigurationBasedApplicationProvider>(); |
| | | 172 | | |
| | | 173 | | // Role providers. |
| | 1 | 174 | | Services |
| | 1 | 175 | | .AddScoped<AdminRoleProvider>() |
| | 1 | 176 | | .AddScoped<StoreBasedRoleProvider>() |
| | 1 | 177 | | .AddScoped<ConfigurationBasedRoleProvider>(); |
| | | 178 | | |
| | | 179 | | // Tenant resolution strategies. |
| | 1 | 180 | | Services |
| | 1 | 181 | | .AddScoped<ITenantResolver, ClaimsTenantResolver>() |
| | 1 | 182 | | .AddScoped<ITenantResolver, CurrentUserTenantResolver>(); |
| | | 183 | | |
| | | 184 | | // Services. |
| | 1 | 185 | | Services |
| | 1 | 186 | | .AddScoped(UserStore) |
| | 1 | 187 | | .AddScoped(ApplicationStore) |
| | 1 | 188 | | .AddScoped(RoleStore) |
| | 1 | 189 | | .AddScoped(UserProvider) |
| | 1 | 190 | | .AddScoped(ApplicationProvider) |
| | 1 | 191 | | .AddScoped(RoleProvider) |
| | 1 | 192 | | .AddScoped<ISecretHasher, DefaultSecretHasher>() |
| | 1 | 193 | | .AddScoped<IAccessTokenIssuer, DefaultAccessTokenIssuer>() |
| | 1 | 194 | | .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>() |
| | 1 | 195 | | .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>() |
| | 1 | 196 | | .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 197 | | .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 1 | 198 | | .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>() |
| | 1 | 199 | | .AddScoped<ISecretGenerator, DefaultSecretGenerator>() |
| | 1 | 200 | | .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>() |
| | 1 | 201 | | .AddScoped<DefaultApiKeyGeneratorAndParser>() |
| | 1 | 202 | | .AddHttpContextAccessor() |
| | 1 | 203 | | ; |
| | 1 | 204 | | } |
| | | 205 | | } |