| | | 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.Extensions; |
| | | 7 | | using Elsa.Identity.Contracts; |
| | | 8 | | using Elsa.Identity.Entities; |
| | | 9 | | using Elsa.Identity.Multitenancy; |
| | | 10 | | using Elsa.Identity.Options; |
| | | 11 | | using Elsa.Identity.Providers; |
| | | 12 | | using Elsa.Identity.Services; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Microsoft.Extensions.Configuration; |
| | | 15 | | using Microsoft.Extensions.DependencyInjection; |
| | | 16 | | |
| | | 17 | | namespace Elsa.Identity.ShellFeatures; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Provides identity feature to authenticate & authorize API requests. |
| | | 21 | | /// </summary> |
| | | 22 | | [ShellFeature( |
| | | 23 | | DisplayName = "Identity", |
| | | 24 | | Description = "Provides identity management, authentication and authorization capabilities", |
| | | 25 | | DependsOn = ["SystemClock"])] |
| | | 26 | | [UsedImplicitly] |
| | | 27 | | public class IdentityFeature : IFastEndpointsShellFeature |
| | | 28 | | { |
| | | 29 | | public void ConfigureServices(IServiceCollection services) |
| | | 30 | | { |
| | 0 | 31 | | services.AddOptions<IdentityTokenOptions>().Configure<ShellConfiguration>((options, config) => config.GetSection |
| | 0 | 32 | | services.Configure<ApiKeyOptions>(ApiKeyDefaults.AuthenticationScheme, options => |
| | 0 | 33 | | { |
| | 0 | 34 | | options.Realm = "Elsa Workflows"; |
| | 0 | 35 | | options.KeyName = "ApiKey"; |
| | 0 | 36 | | }); |
| | 0 | 37 | | services.Configure<UsersOptions>(_ => { }); |
| | 0 | 38 | | services.Configure<ApplicationsOptions>(_ => { }); |
| | 0 | 39 | | services.Configure<RolesOptions>(_ => { }); |
| | | 40 | | |
| | | 41 | | // Memory stores. |
| | 0 | 42 | | services |
| | 0 | 43 | | .AddMemoryStore<User, MemoryUserStore>() |
| | 0 | 44 | | .AddMemoryStore<Application, MemoryApplicationStore>() |
| | 0 | 45 | | .AddMemoryStore<Role, MemoryRoleStore>(); |
| | | 46 | | |
| | | 47 | | // User providers. |
| | 0 | 48 | | services |
| | 0 | 49 | | .AddScoped<AdminUserProvider>() |
| | 0 | 50 | | .AddScoped<StoreBasedUserProvider>() |
| | 0 | 51 | | .AddScoped<ConfigurationBasedUserProvider>(); |
| | | 52 | | |
| | | 53 | | // Application providers. |
| | 0 | 54 | | services |
| | 0 | 55 | | .AddScoped<StoreBasedApplicationProvider>() |
| | 0 | 56 | | .AddScoped<ConfigurationBasedApplicationProvider>(); |
| | | 57 | | |
| | | 58 | | // Role providers. |
| | 0 | 59 | | services |
| | 0 | 60 | | .AddScoped<AdminRoleProvider>() |
| | 0 | 61 | | .AddScoped<StoreBasedRoleProvider>() |
| | 0 | 62 | | .AddScoped<ConfigurationBasedRoleProvider>(); |
| | | 63 | | |
| | | 64 | | // Tenant resolution strategies. |
| | 0 | 65 | | services |
| | 0 | 66 | | .AddScoped<ITenantResolver, ClaimsTenantResolver>() |
| | 0 | 67 | | .AddScoped<ITenantResolver, CurrentUserTenantResolver>(); |
| | | 68 | | |
| | | 69 | | // Services. |
| | 0 | 70 | | services |
| | 0 | 71 | | .AddScoped<IUserManager, UserManager>() |
| | 0 | 72 | | .AddScoped<IRoleManager, RoleManager>() |
| | 0 | 73 | | .AddScoped<ISecretHasher, DefaultSecretHasher>() |
| | 0 | 74 | | .AddScoped<IAccessTokenIssuer, DefaultAccessTokenIssuer>() |
| | 0 | 75 | | .AddScoped<IUserCredentialsValidator, DefaultUserCredentialsValidator>() |
| | 0 | 76 | | .AddScoped<IApplicationCredentialsValidator, DefaultApplicationCredentialsValidator>() |
| | 0 | 77 | | .AddScoped<IApiKeyGenerator>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 78 | | .AddScoped<IApiKeyParser>(sp => sp.GetRequiredService<DefaultApiKeyGeneratorAndParser>()) |
| | 0 | 79 | | .AddScoped<IClientIdGenerator, DefaultClientIdGenerator>() |
| | 0 | 80 | | .AddScoped<ISecretGenerator, DefaultSecretGenerator>() |
| | 0 | 81 | | .AddScoped<IRandomStringGenerator, DefaultRandomStringGenerator>() |
| | 0 | 82 | | .AddScoped<DefaultApiKeyGeneratorAndParser>() |
| | 0 | 83 | | .AddHttpContextAccessor() |
| | 0 | 84 | | ; |
| | | 85 | | |
| | | 86 | | // Overridable services. |
| | 0 | 87 | | services |
| | 0 | 88 | | .AddScoped<IUserStore, MemoryUserStore>() |
| | 0 | 89 | | .AddScoped<IApplicationStore, MemoryApplicationStore>() |
| | 0 | 90 | | .AddScoped<IRoleStore, MemoryRoleStore>() |
| | 0 | 91 | | .AddScoped<IUserProvider, StoreBasedUserProvider>() |
| | 0 | 92 | | .AddScoped<IApplicationProvider, StoreBasedApplicationProvider>() |
| | 0 | 93 | | .AddScoped<IRoleProvider, StoreBasedRoleProvider>(); |
| | 0 | 94 | | } |
| | | 95 | | } |