| | | 1 | | using Elsa.Http.Middleware; |
| | | 2 | | using Microsoft.AspNetCore.Builder; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Adds extension methods to <see cref="IApplicationBuilder"/> related to workflow middleware components. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ApplicationBuilderExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Installs the <see cref="HttpWorkflowsMiddleware"/> component. |
| | | 15 | | /// </summary> |
| | 3 | 16 | | public static IApplicationBuilder UseWorkflows(this IApplicationBuilder app) => app.UseMiddleware<HttpWorkflowsMiddl |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Applies an ASP.NET Core rate limiting policy to inbound HTTP workflow trigger routes. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="app">The application builder.</param> |
| | | 22 | | /// <param name="basePath">The HTTP workflow trigger base path to protect.</param> |
| | | 23 | | /// <param name="policyName">The registered ASP.NET Core rate limiting policy name. Leave empty to skip rate limitin |
| | | 24 | | public static IApplicationBuilder UseWorkflowsRateLimiting(this IApplicationBuilder app, PathString? basePath, strin |
| | | 25 | | { |
| | 3 | 26 | | var normalizedBasePath = basePath?.ToString(); |
| | 3 | 27 | | return app.UseWorkflowsRateLimiting(normalizedBasePath, policyName); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Applies an ASP.NET Core rate limiting policy to inbound HTTP workflow trigger routes. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="app">The application builder.</param> |
| | | 34 | | /// <param name="basePath">The HTTP workflow trigger base path to protect.</param> |
| | | 35 | | /// <param name="policyName">The registered ASP.NET Core rate limiting policy name. Leave empty to skip rate limitin |
| | | 36 | | public static IApplicationBuilder UseWorkflowsRateLimiting(this IApplicationBuilder app, string? basePath, string? p |
| | | 37 | | { |
| | 11 | 38 | | if (string.IsNullOrWhiteSpace(policyName) || string.IsNullOrWhiteSpace(basePath)) |
| | 3 | 39 | | return app; |
| | | 40 | | |
| | 8 | 41 | | var pathPrefix = NormalizeBasePath(basePath); |
| | 8 | 42 | | return pathPrefix.HasValue |
| | 8 | 43 | | ? app.UseRateLimitingPolicyForPath(pathPrefix, policyName, "Elsa HTTP workflow trigger rate limiting endpoin |
| | 8 | 44 | | : app; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private static PathString NormalizeBasePath(string basePath) |
| | | 48 | | { |
| | 8 | 49 | | var value = basePath.Trim(); |
| | | 50 | | |
| | 8 | 51 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 52 | | return PathString.Empty; |
| | | 53 | | |
| | 8 | 54 | | value = value.Trim('/'); |
| | | 55 | | |
| | 8 | 56 | | return string.IsNullOrEmpty(value) ? PathString.Empty : new PathString("/" + value); |
| | | 57 | | } |
| | | 58 | | } |