| | | 1 | | using AspNetCore.Authentication.ApiKey; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Features.Abstractions; |
| | | 4 | | using Elsa.Features.Attributes; |
| | | 5 | | using Elsa.Features.Services; |
| | | 6 | | using Elsa.Identity.Providers; |
| | | 7 | | using Elsa.Requirements; |
| | | 8 | | using Microsoft.AspNetCore.Authentication; |
| | | 9 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 10 | | using Microsoft.AspNetCore.Authorization; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Identity.Features; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides an authorization feature that configures the system with JWT bearer and API key authentication. |
| | | 17 | | /// </summary> |
| | | 18 | | [DependsOn(typeof(IdentityFeature))] |
| | | 19 | | public class DefaultAuthenticationFeature : FeatureBase |
| | | 20 | | { |
| | | 21 | | private const string MultiScheme = "Jwt-or-ApiKey"; |
| | 1 | 22 | | private Func<AuthenticationBuilder, AuthenticationBuilder> _configureApiKeyAuthorization = builder => builder.AddApi |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 1 | 25 | | public DefaultAuthenticationFeature(IModule module) : base(module) |
| | | 26 | | { |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets or sets the <see cref="ApiKeyProviderType"/>. |
| | | 31 | | /// </summary> |
| | 2 | 32 | | public Type ApiKeyProviderType { get; set; } = typeof(DefaultApiKeyProvider); |
| | 4 | 33 | | public Action<AuthorizationOptions> ConfigureAuthorizationOptions { get; set; } = options => options.AddPolicy(Ident |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Configures the API key provider type. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <typeparam name="T">The type of the API key provider.</typeparam> |
| | | 39 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | | 40 | | public DefaultAuthenticationFeature UseApiKeyAuthorization<T>() where T : class, IApiKeyProvider |
| | | 41 | | { |
| | 2 | 42 | | _configureApiKeyAuthorization = builder => builder.AddApiKeyInAuthorizationHeader<T>(); |
| | 1 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Configures the API key provider type to <see cref="AdminApiKeyProvider"/>. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | 1 | 50 | | public DefaultAuthenticationFeature UseAdminApiKey() => UseApiKeyAuthorization<AdminApiKeyProvider>(); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Disables the local host requirement for the security root policy. |
| | | 54 | | /// We are considering removing this requirement by default in the future, in favor of a new "setup mode" paradigm. |
| | | 55 | | /// </summary> |
| | | 56 | | public DefaultAuthenticationFeature DisableLocalHostRequirement() |
| | | 57 | | { |
| | 0 | 58 | | ConfigureAuthorizationOptions = options => options.AddPolicy(IdentityPolicyNames.SecurityRoot, policy => policy. |
| | 0 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public override void Apply() |
| | | 64 | | { |
| | 1 | 65 | | Services.ConfigureOptions<ConfigureJwtBearerOptions>(); |
| | 1 | 66 | | Services.ConfigureOptions<ValidateIdentityTokenOptions>(); |
| | | 67 | | |
| | 1 | 68 | | var authBuilder = Services |
| | 1 | 69 | | .AddAuthentication(MultiScheme) |
| | 1 | 70 | | .AddPolicyScheme(MultiScheme, MultiScheme, options => |
| | 1 | 71 | | { |
| | 1 | 72 | | options.ForwardDefaultSelector = context => |
| | 1 | 73 | | { |
| | 472 | 74 | | return context.Request.Headers.Authorization.Any(x => x!.Contains(ApiKeyDefaults.AuthenticationSchem |
| | 236 | 75 | | ? ApiKeyDefaults.AuthenticationScheme |
| | 236 | 76 | | : JwtBearerDefaults.AuthenticationScheme; |
| | 1 | 77 | | }; |
| | 1 | 78 | | }) |
| | 1 | 79 | | .AddJwtBearer(); |
| | | 80 | | |
| | 1 | 81 | | _configureApiKeyAuthorization(authBuilder); |
| | | 82 | | |
| | 1 | 83 | | Services.AddScoped<IAuthorizationHandler, LocalHostRequirementHandler>(); |
| | 1 | 84 | | Services.AddScoped<IAuthorizationHandler, LocalHostPermissionRequirementHandler>(); |
| | 1 | 85 | | Services.AddScoped(ApiKeyProviderType); |
| | 1 | 86 | | Services.AddScoped<IApiKeyProvider>(sp => (IApiKeyProvider)sp.GetRequiredService(ApiKeyProviderType)); |
| | 1 | 87 | | Services.AddAuthorization(ConfigureAuthorizationOptions); |
| | 1 | 88 | | } |
| | | 89 | | } |