| | | 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 /> |
| | 3 | 27 | | public IdentityFeature(IModule module) : base(module) |
| | | 28 | | { |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the <see cref="IdentityTokenOptions"/>. |
| | | 33 | | /// </summary> |
| | 15 | 34 | | public Action<IdentityTokenOptions> TokenOptions { get; set; } = _ => { }; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the <see cref="ApiKeyOptions"/>. |
| | | 38 | | /// </summary> |
| | 6 | 39 | | public Action<ApiKeyOptions> ApiKeyOptions { get; set; } = options => |
| | 3 | 40 | | { |
| | 1 | 41 | | options.Realm = "Elsa Workflows"; |
| | 1 | 42 | | options.KeyName = "ApiKey"; |
| | 4 | 43 | | }; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// A delegate that configures the <see cref="UsersOptions"/>. |
| | | 47 | | /// </summary> |
| | 12 | 48 | | public Action<UsersOptions> UsersOptions { get; set; } = _ => { }; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// A delegate that configures the <see cref="ApplicationsOptions"/>. |
| | | 52 | | /// </summary> |
| | 12 | 53 | | public Action<ApplicationsOptions> ApplicationsOptions { get; set; } = _ => { }; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// A delegate that configures the <see cref="RolesOptions"/>. |
| | | 57 | | /// </summary> |
| | 12 | 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> |
| | 9 | 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> |
| | 9 | 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> |
| | 9 | 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> |
| | 9 | 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> |
| | 9 | 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> |
| | 9 | 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 | | { |
| | 6 | 100 | | UserProvider = sp => sp.GetRequiredService<ConfigurationBasedUserProvider>(); |
| | 3 | 101 | | UsersOptions += configure; |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Configures the feature to use <see cref="AdminUserProvider"/>. The provider denies all users unless configured. |
| | | 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="AdminUserProvider"/> with an explicit admin user. |
| | | 115 | | /// </summary> |
| | | 116 | | public void UseAdminUserProvider(Action<AdminUserProviderOptions> configure) |
| | | 117 | | { |
| | 0 | 118 | | UseAdminUserProvider(); |
| | 0 | 119 | | Services.Configure(configure); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Configures the feature to use the development admin user. Do not use in production. |
| | | 124 | | /// </summary> |
| | 0 | 125 | | public void UseDevelopmentAdminUserProvider() => UseAdminUserProvider(options => |
| | 0 | 126 | | { |
| | 0 | 127 | | options.UserName = "admin"; |
| | 0 | 128 | | options.Password = "password"; |
| | 0 | 129 | | }); |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Configures the feature to use <see cref="StoreBasedApplicationProvider"/>. |
| | | 133 | | /// </summary> |
| | 0 | 134 | | public void UseStoreBasedApplicationProvider() => ApplicationProvider = sp => sp.GetRequiredService<StoreBasedApplic |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Configures the feature to use <see cref="ConfigurationBasedApplicationProvider"/>. |
| | | 138 | | /// </summary> |
| | | 139 | | public void UseConfigurationBasedApplicationProvider(Action<ApplicationsOptions> configure) |
| | | 140 | | { |
| | 6 | 141 | | ApplicationProvider = sp => sp.GetRequiredService<ConfigurationBasedApplicationProvider>(); |
| | 3 | 142 | | ApplicationsOptions += configure; |
| | 3 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Configures the feature to use <see cref="StoreBasedRoleProvider"/>. |
| | | 147 | | /// </summary> |
| | 0 | 148 | | public void UseStoreBasedRoleProvider() => RoleProvider = sp => sp.GetRequiredService<StoreBasedRoleProvider>(); |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Configures the feature to use <see cref="ConfigurationBasedRoleProvider"/>. |
| | | 152 | | /// </summary> |
| | | 153 | | public void UseConfigurationBasedRoleProvider(Action<RolesOptions> configure) |
| | | 154 | | { |
| | 6 | 155 | | RoleProvider = sp => sp.GetRequiredService<ConfigurationBasedRoleProvider>(); |
| | 3 | 156 | | RolesOptions += configure; |
| | 3 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <inheritdoc /> |
| | | 160 | | public override void Configure() |
| | | 161 | | { |
| | 3 | 162 | | Module.AddFastEndpointsAssembly(GetType()); |
| | 3 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <inheritdoc /> |
| | | 166 | | public override void Apply() |
| | | 167 | | { |
| | 3 | 168 | | Services.Configure(TokenOptions); |
| | 3 | 169 | | Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions); |
| | 3 | 170 | | Services.Configure<AdminUserProviderOptions>(_ => { }); |
| | 3 | 171 | | Services.Configure(UsersOptions); |
| | 3 | 172 | | Services.Configure(ApplicationsOptions); |
| | 3 | 173 | | Services.Configure(RolesOptions); |
| | | 174 | | |
| | | 175 | | // Memory stores. |
| | 3 | 176 | | Services |
| | 3 | 177 | | .AddMemoryStore<User, MemoryUserStore>() |
| | 3 | 178 | | .AddMemoryStore<Application, MemoryApplicationStore>() |
| | 3 | 179 | | .AddMemoryStore<Role, MemoryRoleStore>(); |
| | | 180 | | |
| | | 181 | | // User providers. |
| | 3 | 182 | | Services |
| | 3 | 183 | | .AddScoped<AdminUserProvider>() |
| | 3 | 184 | | .AddScoped<StoreBasedUserProvider>() |
| | 3 | 185 | | .AddScoped<ConfigurationBasedUserProvider>(); |
| | | 186 | | |
| | | 187 | | // Application providers. |
| | 3 | 188 | | Services |
| | 3 | 189 | | .AddScoped<StoreBasedApplicationProvider>() |
| | 3 | 190 | | .AddScoped<ConfigurationBasedApplicationProvider>(); |
| | | 191 | | |
| | | 192 | | // Role providers. |
| | 3 | 193 | | Services |
| | 3 | 194 | | .AddScoped<AdminRoleProvider>() |
| | 3 | 195 | | .AddScoped<StoreBasedRoleProvider>() |
| | 3 | 196 | | .AddScoped<ConfigurationBasedRoleProvider>(); |
| | | 197 | | |
| | | 198 | | // Tenant resolution strategies. |
| | 3 | 199 | | Services |
| | 3 | 200 | | .AddScoped<ITenantResolver, ClaimsTenantResolver>() |
| | 3 | 201 | | .AddScoped<ITenantResolver, CurrentUserTenantResolver>(); |
| | | 202 | | |
| | | 203 | | // Services. |
| | 3 | 204 | | Services |
| | 3 | 205 | | .AddScoped(UserStore) |
| | 3 | 206 | | .AddScoped(ApplicationStore) |
| | 3 | 207 | | .AddScoped(RoleStore) |
| | 3 | 208 | | .AddScoped(UserProvider) |
| | 3 | 209 | | .AddScoped(ApplicationProvider) |
| | 3 | 210 | | .AddScoped(RoleProvider) |
| | 3 | 211 | | .AddScoped<IUserManager, UserManager>() |
| | 3 | 212 | | .AddScoped<IRoleManager, RoleManager>() |
| | 3 | 213 | | .AddScoped<IRoleAuthorizationService, RoleAuthorizationService>() |
| | 3 | 214 | | .AddScoped<ISecretHasher, DefaultSecretHasher>() |
| | 3 | 215 | | .AddScoped<IAccessTokenIssuer, DefaultAccessTokenIssuer>() |
| | 3 | 216 | | .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>() |
| | 3 | 217 | | .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>() |
| | 3 | 218 | | .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 219 | | .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 3 | 220 | | .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>() |
| | 3 | 221 | | .AddScoped<ISecretGenerator, DefaultSecretGenerator>() |
| | 3 | 222 | | .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>() |
| | 3 | 223 | | .AddScoped<DefaultApiKeyGeneratorAndParser>() |
| | 3 | 224 | | .AddHttpContextAccessor() |
| | 3 | 225 | | ; |
| | 3 | 226 | | } |
| | | 227 | | } |