| | | 1 | | using AspNetCore.Authentication.ApiKey; |
| | | 2 | | using CShells.Features; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Identity.Providers; |
| | | 5 | | using Elsa.Requirements; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Authentication; |
| | | 8 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Identity.ShellFeatures; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides an authorization feature that configures the system with JWT bearer and API key authentication. |
| | | 16 | | /// </summary> |
| | | 17 | | [ShellFeature( |
| | | 18 | | DisplayName = "Default Authentication", |
| | | 19 | | Description = "Provides JWT bearer and API key authentication", |
| | | 20 | | DependsOn = ["Identity"])] |
| | | 21 | | [UsedImplicitly] |
| | | 22 | | public class DefaultAuthenticationFeature : IShellFeature |
| | | 23 | | { |
| | | 24 | | private const string MultiScheme = "Jwt-or-ApiKey"; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets or sets the API key provider type. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public Type ApiKeyProviderType { get; set; } = typeof(DefaultApiKeyProvider); |
| | | 30 | | |
| | | 31 | | public void ConfigureServices(IServiceCollection services) |
| | | 32 | | { |
| | 0 | 33 | | services.ConfigureOptions<ConfigureJwtBearerOptions>(); |
| | 0 | 34 | | services.ConfigureOptions<ValidateIdentityTokenOptions>(); |
| | | 35 | | |
| | 0 | 36 | | var authBuilder = services |
| | 0 | 37 | | .AddAuthentication(MultiScheme) |
| | 0 | 38 | | .AddPolicyScheme(MultiScheme, MultiScheme, options => |
| | 0 | 39 | | { |
| | 0 | 40 | | options.ForwardDefaultSelector = context => |
| | 0 | 41 | | { |
| | 0 | 42 | | return context.Request.Headers.Authorization.Any(x => x!.Contains(ApiKeyDefaults.AuthenticationSchem |
| | 0 | 43 | | ? ApiKeyDefaults.AuthenticationScheme |
| | 0 | 44 | | : JwtBearerDefaults.AuthenticationScheme; |
| | 0 | 45 | | }; |
| | 0 | 46 | | }) |
| | 0 | 47 | | .AddJwtBearer(); |
| | | 48 | | |
| | | 49 | | // Configure API key authorization based on provider type |
| | 0 | 50 | | if (ApiKeyProviderType == typeof(AdminApiKeyProvider)) |
| | 0 | 51 | | authBuilder.AddApiKeyInAuthorizationHeader<AdminApiKeyProvider>(); |
| | | 52 | | else |
| | 0 | 53 | | authBuilder.AddApiKeyInAuthorizationHeader<DefaultApiKeyProvider>(); |
| | | 54 | | |
| | 0 | 55 | | services.AddScoped<IAuthorizationHandler, LocalHostRequirementHandler>(); |
| | 0 | 56 | | services.AddScoped<IAuthorizationHandler, LocalHostPermissionRequirementHandler>(); |
| | 0 | 57 | | services.AddScoped(ApiKeyProviderType); |
| | 0 | 58 | | services.AddScoped<IApiKeyProvider>(sp => (IApiKeyProvider)sp.GetRequiredService(ApiKeyProviderType)); |
| | | 59 | | |
| | 0 | 60 | | services.AddAuthorization(options => |
| | 0 | 61 | | { |
| | 0 | 62 | | options.AddPolicy(IdentityPolicyNames.SecurityRoot, policy => policy.RequireAuthenticatedUser()); |
| | 0 | 63 | | }); |
| | 0 | 64 | | } |
| | | 65 | | } |