| | | 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.HostedServices; |
| | | 12 | | using Elsa.Identity.Options; |
| | | 13 | | using Elsa.Identity.Providers; |
| | | 14 | | using Elsa.Identity.Services; |
| | | 15 | | using JetBrains.Annotations; |
| | | 16 | | using Microsoft.Extensions.DependencyInjection; |
| | | 17 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 18 | | |
| | | 19 | | namespace Elsa.Identity.Features; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Provides identity feature to authenticate & authorize API requests. |
| | | 23 | | /// </summary> |
| | | 24 | | [DependsOn(typeof(SystemClockFeature))] |
| | | 25 | | [PublicAPI] |
| | | 26 | | public class IdentityFeature : FeatureBase |
| | | 27 | | { |
| | | 28 | | /// <inheritdoc /> |
| | 5 | 29 | | public IdentityFeature(IModule module) : base(module) |
| | | 30 | | { |
| | 5 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the <see cref="IdentityTokenOptions"/>. |
| | | 35 | | /// </summary> |
| | 19 | 36 | | public Action<IdentityTokenOptions> TokenOptions { get; set; } = _ => { }; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the <see cref="ApiKeyOptions"/>. |
| | | 40 | | /// </summary> |
| | 10 | 41 | | public Action<ApiKeyOptions> ApiKeyOptions { get; set; } = options => |
| | 5 | 42 | | { |
| | 1 | 43 | | options.Realm = "Elsa Workflows"; |
| | 1 | 44 | | options.KeyName = "ApiKey"; |
| | 6 | 45 | | }; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A delegate that configures the <see cref="UsersOptions"/>. |
| | | 49 | | /// </summary> |
| | 16 | 50 | | public Action<UsersOptions> UsersOptions { get; set; } = _ => { }; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// A delegate that configures the <see cref="ApplicationsOptions"/>. |
| | | 54 | | /// </summary> |
| | 16 | 55 | | public Action<ApplicationsOptions> ApplicationsOptions { get; set; } = _ => { }; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// A delegate that configures the <see cref="RolesOptions"/>. |
| | | 59 | | /// </summary> |
| | 19 | 60 | | public Action<RolesOptions> RolesOptions { get; set; } = _ => { }; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// A delegate that creates an instance of an implementation of <see cref="IUserStore"/>. |
| | | 64 | | /// </summary> |
| | 14 | 65 | | public Func<IServiceProvider, IUserStore> UserStore { get; set; } = sp => sp.GetRequiredService<MemoryUserStore>(); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// A delegate that creates an instance of an implementation of <see cref="IApplicationStore"/>. |
| | | 69 | | /// </summary> |
| | 13 | 70 | | public Func<IServiceProvider, IApplicationStore> ApplicationStore { get; set; } = sp => sp.GetRequiredService<Memory |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// A delegate that creates an instance of an implementation of <see cref="IRoleStore"/>. |
| | | 74 | | /// </summary> |
| | 14 | 75 | | public Func<IServiceProvider, IRoleStore> RoleStore { get; set; } = sp => sp.GetRequiredService<MemoryRoleStore>(); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// A delegate that creates an instance of an implementation of <see cref="IUserProvider"/>. |
| | | 79 | | /// </summary> |
| | 13 | 80 | | public Func<IServiceProvider, IUserProvider> UserProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedUser |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// A delegate that creates an instance of an implementation of <see cref="IApplicationProvider"/>. |
| | | 84 | | /// </summary> |
| | 13 | 85 | | public Func<IServiceProvider, IApplicationProvider> ApplicationProvider { get; set; } = sp => sp.GetRequiredService< |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// A delegate that creates an instance of an implementation of <see cref="IRoleProvider"/>. |
| | | 89 | | /// </summary> |
| | 14 | 90 | | public Func<IServiceProvider, IRoleProvider> RoleProvider { get; set; } = sp => sp.GetRequiredService<StoreBasedRole |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>. |
| | | 94 | | /// </summary> |
| | 0 | 95 | | public void UseStoreBasedUserProvider() => UserProvider = sp => sp.GetRequiredService<StoreBasedUserProvider>(); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Configures the feature to use <see cref="ConfigurationBasedUserProvider"/>. |
| | | 99 | | /// </summary> |
| | | 100 | | public void UseConfigurationBasedUserProvider(Action<UsersOptions> configure) |
| | | 101 | | { |
| | 6 | 102 | | UserProvider = sp => sp.GetRequiredService<ConfigurationBasedUserProvider>(); |
| | 3 | 103 | | UsersOptions += configure; |
| | 3 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Configures the feature to use <see cref="AdminUserProvider"/>. The provider denies all users unless configured. |
| | | 108 | | /// </summary> |
| | | 109 | | public void UseAdminUserProvider() |
| | | 110 | | { |
| | 0 | 111 | | UserProvider = sp => sp.GetRequiredService<AdminUserProvider>(); |
| | 0 | 112 | | RoleProvider = sp => sp.GetRequiredService<AdminRoleProvider>(); |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Configures the feature to use <see cref="AdminUserProvider"/> with an explicit admin user. |
| | | 117 | | /// </summary> |
| | | 118 | | public void UseAdminUserProvider(Action<AdminUserProviderOptions> configure) |
| | | 119 | | { |
| | 0 | 120 | | UseAdminUserProvider(); |
| | 0 | 121 | | Services.Configure(configure); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Configures the feature to use the development admin user. Do not use in production. |
| | | 126 | | /// </summary> |
| | 0 | 127 | | public void UseDevelopmentAdminUserProvider() => UseAdminUserProvider(options => |
| | 0 | 128 | | { |
| | 0 | 129 | | options.UserName = "admin"; |
| | 0 | 130 | | options.Password = "password"; |
| | 0 | 131 | | }); |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Configures the feature to use <see cref="StoreBasedApplicationProvider"/>. |
| | | 135 | | /// </summary> |
| | 0 | 136 | | public void UseStoreBasedApplicationProvider() => ApplicationProvider = sp => sp.GetRequiredService<StoreBasedApplic |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Configures the feature to use <see cref="ConfigurationBasedApplicationProvider"/>. |
| | | 140 | | /// </summary> |
| | | 141 | | public void UseConfigurationBasedApplicationProvider(Action<ApplicationsOptions> configure) |
| | | 142 | | { |
| | 6 | 143 | | ApplicationProvider = sp => sp.GetRequiredService<ConfigurationBasedApplicationProvider>(); |
| | 3 | 144 | | ApplicationsOptions += configure; |
| | 3 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Configures the feature to use <see cref="StoreBasedRoleProvider"/>. |
| | | 149 | | /// </summary> |
| | 0 | 150 | | public void UseStoreBasedRoleProvider() => RoleProvider = sp => sp.GetRequiredService<StoreBasedRoleProvider>(); |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Configures the feature to use <see cref="ConfigurationBasedRoleProvider"/>. |
| | | 154 | | /// </summary> |
| | | 155 | | public void UseConfigurationBasedRoleProvider(Action<RolesOptions> configure) |
| | | 156 | | { |
| | 9 | 157 | | RoleProvider = sp => sp.GetRequiredService<ConfigurationBasedRoleProvider>(); |
| | 3 | 158 | | RolesOptions += configure; |
| | 3 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc /> |
| | | 162 | | public override void Configure() |
| | | 163 | | { |
| | 3 | 164 | | Module.AddFastEndpointsAssembly(GetType()); |
| | 3 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc /> |
| | | 168 | | public override void Apply() |
| | | 169 | | { |
| | 5 | 170 | | Services.Configure(TokenOptions); |
| | 5 | 171 | | Services.Configure(ApiKeyDefaults.AuthenticationScheme, ApiKeyOptions); |
| | 5 | 172 | | Services.Configure<AdminUserProviderOptions>(_ => { }); |
| | 5 | 173 | | Services.Configure(UsersOptions); |
| | 5 | 174 | | Services.Configure(ApplicationsOptions); |
| | 5 | 175 | | Services.Configure(RolesOptions); |
| | | 176 | | |
| | | 177 | | // The identity stores are tenant-scoped, so a host that never enables multitenancy still needs an |
| | | 178 | | // accessor. TryAdd leaves an existing registration -- notably MultitenancyFeature's -- untouched. |
| | 5 | 179 | | Services.TryAddSingleton<ITenantAccessor, DefaultTenantAccessor>(); |
| | 5 | 180 | | Services.AddMemoryCache(); |
| | 5 | 181 | | Services.Configure<PermissionStampOptions>(_ => { }); |
| | 5 | 182 | | Services.AddHostedService<StoredPermissionValidator>(); |
| | 5 | 183 | | Services.AddHostedService<IdentityBootstrapDiagnostic>(); |
| | | 184 | | |
| | | 185 | | // Memory stores. |
| | 5 | 186 | | Services |
| | 5 | 187 | | .AddMemoryStore<User, MemoryUserStore>() |
| | 5 | 188 | | .AddMemoryStore<Application, MemoryApplicationStore>() |
| | 5 | 189 | | .AddMemoryStore<Role, MemoryRoleStore>(); |
| | | 190 | | |
| | | 191 | | // User providers. |
| | 5 | 192 | | Services |
| | 5 | 193 | | .AddScoped<AdminUserProvider>() |
| | 5 | 194 | | .AddScoped<StoreBasedUserProvider>() |
| | 5 | 195 | | .AddScoped<ConfigurationBasedUserProvider>(); |
| | | 196 | | |
| | | 197 | | // Application providers. |
| | 5 | 198 | | Services |
| | 5 | 199 | | .AddScoped<StoreBasedApplicationProvider>() |
| | 5 | 200 | | .AddScoped<ConfigurationBasedApplicationProvider>(); |
| | | 201 | | |
| | | 202 | | // Role providers. |
| | 5 | 203 | | Services |
| | 5 | 204 | | .AddScoped<AdminRoleProvider>() |
| | 5 | 205 | | .AddScoped<StoreBasedRoleProvider>() |
| | 5 | 206 | | .AddScoped<ConfigurationBasedRoleProvider>(); |
| | | 207 | | |
| | | 208 | | // Tenant resolution strategies. |
| | 5 | 209 | | Services |
| | 5 | 210 | | .AddScoped<ITenantResolver, ClaimsTenantResolver>() |
| | 5 | 211 | | .AddScoped<ITenantResolver, CurrentUserTenantResolver>(); |
| | | 212 | | |
| | | 213 | | // Services. |
| | 5 | 214 | | Services |
| | 5 | 215 | | .AddScoped(UserStore) |
| | 5 | 216 | | .AddScoped(ApplicationStore) |
| | 5 | 217 | | .AddScoped(RoleStore) |
| | 5 | 218 | | .AddScoped(UserProvider) |
| | 5 | 219 | | .AddScoped(ApplicationProvider) |
| | 5 | 220 | | .AddScoped(RoleProvider) |
| | 5 | 221 | | .AddScoped<IUserManager, UserManager>() |
| | 5 | 222 | | .AddScoped<IRoleManager, RoleManager>() |
| | 5 | 223 | | .AddScoped<IRoleAuthorizationService, RoleAuthorizationService>() |
| | 5 | 224 | | .AddScoped<RoleSecurityNotifier>() |
| | 5 | 225 | | .AddScoped<IPermissionStampCalculator, PermissionStampCalculator>() |
| | 5 | 226 | | .AddScoped<PermissionStampValidator>() |
| | 5 | 227 | | .AddScoped<IRoleDeletionCoordinator, RoleDeletionCoordinator>() |
| | 5 | 228 | | .AddScoped<IUserDeletionCoordinator, UserDeletionCoordinator>() |
| | 5 | 229 | | .AddScoped<ISecretHasher, DefaultSecretHasher>() |
| | 5 | 230 | | .AddScoped<IElsaTokenService, DefaultElsaTokenService>() |
| | 4 | 231 | | .AddScoped<IAccessTokenIssuer>(sp => ActivatorUtilities.CreateInstance<DefaultAccessTokenIssuer>(sp)) |
| | 5 | 232 | | .AddScoped<IIdentityRefreshTokenService, DefaultIdentityRefreshTokenService>() |
| | 5 | 233 | | .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>() |
| | 5 | 234 | | .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>() |
| | 3 | 235 | | .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 236 | | .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 5 | 237 | | .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>() |
| | 5 | 238 | | .AddScoped<ISecretGenerator, DefaultSecretGenerator>() |
| | 5 | 239 | | .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>() |
| | 5 | 240 | | .AddScoped<DefaultApiKeyGeneratorAndParser>() |
| | 5 | 241 | | .AddHttpContextAccessor() |
| | 5 | 242 | | ; |
| | 5 | 243 | | } |
| | | 244 | | } |