| | | 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.Constants; |
| | | 7 | | using Elsa.Identity.Options; |
| | | 8 | | using Elsa.Identity.Providers; |
| | | 9 | | using Microsoft.AspNetCore.Authentication; |
| | | 10 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 11 | | using Microsoft.AspNetCore.Authorization; |
| | | 12 | | using Microsoft.Extensions.DependencyInjection; |
| | | 13 | | |
| | | 14 | | namespace Elsa.Identity.Features; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides an authorization feature that configures the system with JWT bearer and API key authentication. |
| | | 18 | | /// </summary> |
| | | 19 | | [DependsOn(typeof(IdentityFeature))] |
| | | 20 | | public class DefaultAuthenticationFeature : FeatureBase |
| | | 21 | | { |
| | | 22 | | private const string MultiScheme = "Jwt-or-ApiKey"; |
| | 6 | 23 | | private Func<AuthenticationBuilder, AuthenticationBuilder> _configureApiKeyAuthorization = builder => builder.AddApi |
| | | 24 | | private Action<AuthorizationOptions>? _configureAuthorizationOptions; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 6 | 27 | | public DefaultAuthenticationFeature(IModule module) : base(module) |
| | | 28 | | { |
| | 6 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the <see cref="ApiKeyProviderType"/>. |
| | | 33 | | /// </summary> |
| | 12 | 34 | | public Type ApiKeyProviderType { get; set; } = typeof(DefaultApiKeyProvider); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the authorization options configuration. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <remarks> |
| | | 40 | | /// Defaults to a no-op. It previously registered the <c>SecurityRoot</c> policy, which has been retired in |
| | | 41 | | /// favour of endpoint permissions -- see ADR 0010. Hosts that add their own policies keep setting this. |
| | | 42 | | /// </remarks> |
| | | 43 | | public Action<AuthorizationOptions> ConfigureAuthorizationOptions |
| | | 44 | | { |
| | 11 | 45 | | get => _configureAuthorizationOptions ?? (_ => { }); |
| | 2 | 46 | | set => _configureAuthorizationOptions = value; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Configures the API key provider type. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <typeparam name="T">The type of the API key provider.</typeparam> |
| | | 53 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | | 54 | | public DefaultAuthenticationFeature UseApiKeyAuthorization<T>() where T : class, IApiKeyProvider |
| | | 55 | | { |
| | 3 | 56 | | ApiKeyProviderType = typeof(T); |
| | 6 | 57 | | _configureApiKeyAuthorization = builder => builder.AddApiKeyInAuthorizationHeader<T>(); |
| | 3 | 58 | | return this; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Configures the API key provider type to <see cref="AdminApiKeyProvider"/>. The provider denies all keys unless c |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | 3 | 65 | | public DefaultAuthenticationFeature UseAdminApiKey() => UseApiKeyAuthorization<AdminApiKeyProvider>(); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Configures the admin API key provider with an explicit API key. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="apiKey">The API key to accept.</param> |
| | | 71 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | | 72 | | public DefaultAuthenticationFeature UseAdminApiKey(string apiKey) |
| | | 73 | | { |
| | 6 | 74 | | Services.Configure<AdminApiKeyOptions>(options => options.ApiKey = apiKey); |
| | 3 | 75 | | return UseAdminApiKey(); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Configures the admin API key provider with an explicit API key. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="configure">The admin API key options to configure.</param> |
| | | 82 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | | 83 | | public DefaultAuthenticationFeature UseAdminApiKey(Action<AdminApiKeyOptions> configure) |
| | | 84 | | { |
| | 0 | 85 | | Services.Configure(configure); |
| | 0 | 86 | | return UseAdminApiKey(); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Enables the all-zero development admin API key. Do not use in production. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <returns>The current <see cref="DefaultAuthenticationFeature"/>.</returns> |
| | 3 | 93 | | public DefaultAuthenticationFeature UseDevelopmentAdminApiKey() => UseAdminApiKey(AdminApiKeyProvider.DevelopmentApi |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public override void Apply() |
| | | 97 | | { |
| | 3 | 98 | | Services.ConfigureOptions<ConfigureJwtBearerOptions>(); |
| | 6 | 99 | | Services.Configure<AdminApiKeyOptions>(_ => { }); |
| | 3 | 100 | | Services.AddIdentityTokenOptionsValidation(); |
| | | 101 | | |
| | 3 | 102 | | var authBuilder = Services |
| | 3 | 103 | | .AddAuthentication(MultiScheme) |
| | 3 | 104 | | .AddPolicyScheme(MultiScheme, MultiScheme, options => |
| | 3 | 105 | | { |
| | 1 | 106 | | options.ForwardDefaultSelector = context => |
| | 1 | 107 | | { |
| | 594 | 108 | | return context.Request.Headers.Authorization.Any(x => x!.Contains(ApiKeyDefaults.AuthenticationSchem |
| | 297 | 109 | | ? ApiKeyDefaults.AuthenticationScheme |
| | 297 | 110 | | : JwtBearerDefaults.AuthenticationScheme; |
| | 1 | 111 | | }; |
| | 1 | 112 | | }) |
| | 3 | 113 | | .AddJwtBearer() |
| | 3 | 114 | | .AddJwtBearer(IdentityAuthenticationSchemes.RefreshToken); |
| | | 115 | | |
| | 3 | 116 | | _configureApiKeyAuthorization(authBuilder); |
| | | 117 | | |
| | 3 | 118 | | Services.AddScoped(ApiKeyProviderType); |
| | 3 | 119 | | Services.AddScoped<IApiKeyProvider>(sp => (IApiKeyProvider)sp.GetRequiredService(ApiKeyProviderType)); |
| | 3 | 120 | | Services.AddAuthorization(ConfigureAuthorizationOptions); |
| | 3 | 121 | | } |
| | | 122 | | } |