| | | 1 | | using AspNetCore.Authentication.ApiKey; |
| | | 2 | | using CShells.Configuration; |
| | | 3 | | using CShells.FastEndpoints.Features; |
| | | 4 | | using CShells.Features; |
| | | 5 | | using Elsa.Common.Multitenancy; |
| | | 6 | | using Elsa.Common.ShellFeatures; |
| | | 7 | | using Elsa.Extensions; |
| | | 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 Elsa.Platform.PackageManifest.Generator.Hints; |
| | | 15 | | using JetBrains.Annotations; |
| | | 16 | | using Microsoft.Extensions.Configuration; |
| | | 17 | | using Microsoft.Extensions.DependencyInjection; |
| | | 18 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 19 | | |
| | | 20 | | namespace Elsa.Identity.ShellFeatures; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides identity feature to authenticate & authorize API requests. |
| | | 24 | | /// </summary> |
| | | 25 | | [ManifestFeatureCategory("Identity")] |
| | | 26 | | [ManifestFeatureCategory("Security")] |
| | | 27 | | [ShellFeature( |
| | | 28 | | DisplayName = "Identity", |
| | | 29 | | Description = "Provides identity management, authentication and authorization capabilities", |
| | | 30 | | DependsOn = [typeof(SystemClockFeature)])] |
| | | 31 | | [UsedImplicitly] |
| | | 32 | | public class IdentityFeature : IFastEndpointsShellFeature |
| | | 33 | | { |
| | | 34 | | public void ConfigureServices(IServiceCollection services) |
| | | 35 | | { |
| | 2 | 36 | | services.AddOptions<IdentityTokenOptions>().Configure<ShellConfiguration>((options, config) => config.GetSection |
| | 2 | 37 | | services.Configure<ApiKeyOptions>(ApiKeyDefaults.AuthenticationScheme, options => |
| | 2 | 38 | | { |
| | 0 | 39 | | options.Realm = "Elsa Workflows"; |
| | 0 | 40 | | options.KeyName = "ApiKey"; |
| | 2 | 41 | | }); |
| | 2 | 42 | | services.Configure<AdminUserProviderOptions>(_ => { }); |
| | 2 | 43 | | services.Configure<UsersOptions>(_ => { }); |
| | 2 | 44 | | services.Configure<ApplicationsOptions>(_ => { }); |
| | 2 | 45 | | services.Configure<RolesOptions>(_ => { }); |
| | | 46 | | |
| | | 47 | | // The identity stores are tenant-scoped, so a host that never enables multitenancy still needs an |
| | | 48 | | // accessor. TryAdd leaves an existing registration -- notably MultitenancyFeature's -- untouched. |
| | 2 | 49 | | services.TryAddSingleton<ITenantAccessor, DefaultTenantAccessor>(); |
| | 2 | 50 | | services.AddHostedService<HostedServices.StoredPermissionValidator>(); |
| | 2 | 51 | | services.AddHostedService<HostedServices.IdentityBootstrapDiagnostic>(); |
| | 2 | 52 | | services.AddScoped<Services.RoleSecurityNotifier>(); |
| | 2 | 53 | | services.AddMemoryCache(); |
| | 2 | 54 | | services.Configure<PermissionStampOptions>(_ => { }); |
| | 2 | 55 | | services.AddScoped<Services.IPermissionStampCalculator, Services.PermissionStampCalculator>(); |
| | 2 | 56 | | services.AddScoped<Services.PermissionStampValidator>(); |
| | | 57 | | |
| | | 58 | | // Memory stores. |
| | 2 | 59 | | services |
| | 2 | 60 | | .AddMemoryStore<User, MemoryUserStore>() |
| | 2 | 61 | | .AddMemoryStore<Application, MemoryApplicationStore>() |
| | 2 | 62 | | .AddMemoryStore<Role, MemoryRoleStore>(); |
| | | 63 | | |
| | | 64 | | // User providers. |
| | 2 | 65 | | services |
| | 2 | 66 | | .AddScoped<AdminUserProvider>() |
| | 2 | 67 | | .AddScoped<StoreBasedUserProvider>() |
| | 2 | 68 | | .AddScoped<ConfigurationBasedUserProvider>(); |
| | | 69 | | |
| | | 70 | | // Application providers. |
| | 2 | 71 | | services |
| | 2 | 72 | | .AddScoped<StoreBasedApplicationProvider>() |
| | 2 | 73 | | .AddScoped<ConfigurationBasedApplicationProvider>(); |
| | | 74 | | |
| | | 75 | | // Role providers. |
| | 2 | 76 | | services |
| | 2 | 77 | | .AddScoped<AdminRoleProvider>() |
| | 2 | 78 | | .AddScoped<StoreBasedRoleProvider>() |
| | 2 | 79 | | .AddScoped<ConfigurationBasedRoleProvider>(); |
| | | 80 | | |
| | | 81 | | // Tenant resolution strategies. |
| | 2 | 82 | | services |
| | 2 | 83 | | .AddScoped<ITenantResolver, ClaimsTenantResolver>() |
| | 2 | 84 | | .AddScoped<ITenantResolver, CurrentUserTenantResolver>(); |
| | | 85 | | |
| | | 86 | | // Services. |
| | 2 | 87 | | services |
| | 2 | 88 | | .AddScoped<IUserManager, UserManager>() |
| | 2 | 89 | | .AddScoped<IRoleManager, RoleManager>() |
| | 2 | 90 | | .AddScoped<IRoleAuthorizationService, RoleAuthorizationService>() |
| | 2 | 91 | | .AddScoped<IRoleDeletionCoordinator, RoleDeletionCoordinator>() |
| | 2 | 92 | | .AddScoped<IUserDeletionCoordinator, UserDeletionCoordinator>() |
| | 2 | 93 | | .AddScoped<ISecretHasher, DefaultSecretHasher>() |
| | 2 | 94 | | .AddScoped<IElsaTokenService, DefaultElsaTokenService>() |
| | 1 | 95 | | .AddScoped<IAccessTokenIssuer>(sp => ActivatorUtilities.CreateInstance<DefaultAccessTokenIssuer>(sp)) |
| | 2 | 96 | | .AddScoped<IIdentityRefreshTokenService, DefaultIdentityRefreshTokenService>() |
| | 2 | 97 | | .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>() |
| | 2 | 98 | | .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>() |
| | 0 | 99 | | .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 100 | | .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 2 | 101 | | .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>() |
| | 2 | 102 | | .AddScoped<ISecretGenerator, DefaultSecretGenerator>() |
| | 2 | 103 | | .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>() |
| | 2 | 104 | | .AddScoped<DefaultApiKeyGeneratorAndParser>() |
| | 2 | 105 | | .AddHttpContextAccessor() |
| | 2 | 106 | | ; |
| | | 107 | | |
| | | 108 | | // Overridable services. |
| | 2 | 109 | | services |
| | 2 | 110 | | .AddScoped<IUserStore, MemoryUserStore>() |
| | 2 | 111 | | .AddScoped<IApplicationStore, MemoryApplicationStore>() |
| | 2 | 112 | | .AddScoped<IRoleStore, MemoryRoleStore>() |
| | 2 | 113 | | .AddScoped<IUserProvider, StoreBasedUserProvider>() |
| | 2 | 114 | | .AddScoped<IApplicationProvider, StoreBasedApplicationProvider>() |
| | 2 | 115 | | .AddScoped<IRoleProvider, StoreBasedRoleProvider>(); |
| | 2 | 116 | | } |
| | | 117 | | } |