| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Http.Bookmarks; |
| | | 3 | | using Elsa.Http.Options; |
| | | 4 | | using Elsa.Workflows.Runtime.Filters; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | using System.Net; |
| | | 10 | | using System.Net.Mime; |
| | | 11 | | using System.Text.Json; |
| | | 12 | | using Elsa.Workflows.Activities; |
| | | 13 | | using Elsa.Workflows.Runtime.Entities; |
| | | 14 | | using FastEndpoints; |
| | | 15 | | using System.Diagnostics.CodeAnalysis; |
| | | 16 | | using Elsa.Workflows; |
| | | 17 | | using Elsa.Workflows.Management; |
| | | 18 | | using Elsa.Workflows.Management.Entities; |
| | | 19 | | using Elsa.Workflows.Models; |
| | | 20 | | using Elsa.Workflows.Options; |
| | | 21 | | using Elsa.Workflows.Runtime; |
| | | 22 | | using Open.Linq.AsyncExtensions; |
| | | 23 | | |
| | | 24 | | namespace Elsa.Http.Middleware; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// An ASP.NET middleware component that tries to match the inbound request path to an associated workflow and then run |
| | | 28 | | /// </summary> |
| | | 29 | | [PublicAPI] |
| | 15 | 30 | | public class HttpWorkflowsMiddleware(RequestDelegate next) |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Attempts to match the inbound request path to an associated workflow and then run that workflow. |
| | | 34 | | /// </summary> |
| | | 35 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")] |
| | | 36 | | public async Task InvokeAsync( |
| | | 37 | | HttpContext httpContext, |
| | | 38 | | IServiceProvider serviceProvider, |
| | | 39 | | IOptions<HttpActivityOptions> options, |
| | | 40 | | IHttpWorkflowLookupService httpWorkflowLookupService) |
| | | 41 | | { |
| | 307 | 42 | | var path = httpContext.Request.Path.Value!.NormalizeRoute(); |
| | 307 | 43 | | var basePath = options.Value.BasePath?.ToString().NormalizeRoute(); |
| | | 44 | | |
| | 307 | 45 | | if (!string.IsNullOrWhiteSpace(basePath) && !IsBasePathMatch(path, basePath)) |
| | | 46 | | { |
| | 39 | 47 | | await next(httpContext); |
| | 39 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 268 | 51 | | var matchingPath = GetMatchingRoute(serviceProvider, path).Route; |
| | 268 | 52 | | matchingPath = TryStripBasePath(matchingPath, basePath) ?? matchingPath; |
| | | 53 | | |
| | | 54 | | // Graceful-shutdown gate: when the runtime is paused or draining, we don't accept new HTTP-triggered work. |
| | | 55 | | // The ingress source registry visibility is provided by HttpTriggerIngressSource — this is the actual mechanism |
| | 268 | 56 | | var quiescenceSignal = serviceProvider.GetService<IQuiescenceSignal>(); |
| | 268 | 57 | | if (quiescenceSignal is not null && !quiescenceSignal.IsAcceptingNewWork) |
| | | 58 | | { |
| | 0 | 59 | | httpContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; |
| | | 60 | | // Retry-After is reason-aware: drain is short (host is exiting and will be replaced shortly), but an |
| | | 61 | | // administrative pause is indefinite, so a longer back-off avoids a tight retry loop while operators |
| | | 62 | | // perform maintenance. |
| | 0 | 63 | | httpContext.Response.Headers.RetryAfter = quiescenceSignal.CurrentState.Reason.HasFlag(QuiescenceReason.Drai |
| | 0 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 268 | 67 | | matchingPath = matchingPath.NormalizeRoute(); |
| | | 68 | | |
| | 268 | 69 | | var input = new Dictionary<string, object> |
| | 268 | 70 | | { |
| | 268 | 71 | | [HttpEndpoint.HttpContextInputKey] = true, |
| | 268 | 72 | | [HttpEndpoint.PathInputKey] = path |
| | 268 | 73 | | }; |
| | | 74 | | |
| | 268 | 75 | | var cancellationToken = httpContext.RequestAborted; |
| | 268 | 76 | | var request = httpContext.Request; |
| | 268 | 77 | | var method = request.Method.ToLowerInvariant(); |
| | 268 | 78 | | var workflowInstanceId = await GetWorkflowInstanceIdAsync(serviceProvider, httpContext, cancellationToken); |
| | 268 | 79 | | var correlationId = await GetCorrelationIdAsync(serviceProvider, httpContext, cancellationToken); |
| | 268 | 80 | | var bookmarkHash = ComputeBookmarkHash(serviceProvider, matchingPath, method); |
| | 268 | 81 | | var lookupResult = await httpWorkflowLookupService.FindWorkflowAsync(bookmarkHash, cancellationToken); |
| | | 82 | | |
| | 268 | 83 | | if (lookupResult != null) |
| | | 84 | | { |
| | 258 | 85 | | var triggers = lookupResult.Triggers; |
| | | 86 | | |
| | 258 | 87 | | if (triggers.Count > 1) |
| | | 88 | | { |
| | 0 | 89 | | await HandleMultipleWorkflowsFoundAsync(httpContext, () => triggers.Select(x => new |
| | 0 | 90 | | { |
| | 0 | 91 | | x.WorkflowDefinitionId |
| | 0 | 92 | | }), cancellationToken); |
| | 0 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | 258 | 96 | | var trigger = triggers.FirstOrDefault(); |
| | 258 | 97 | | if (trigger != null) |
| | | 98 | | { |
| | 258 | 99 | | var workflowGraph = lookupResult.WorkflowGraph!; |
| | 258 | 100 | | await StartWorkflowAsync(httpContext, trigger, workflowGraph, input, workflowInstanceId, correlationId); |
| | 258 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 10 | 105 | | var bookmarks = await FindBookmarksAsync(serviceProvider, bookmarkHash, workflowInstanceId, correlationId, cance |
| | | 106 | | |
| | 10 | 107 | | if (bookmarks.Count > 1) |
| | | 108 | | { |
| | 0 | 109 | | await HandleMultipleWorkflowsFoundAsync(httpContext, () => bookmarks.Select(x => new |
| | 0 | 110 | | { |
| | 0 | 111 | | x.WorkflowInstanceId |
| | 0 | 112 | | }), cancellationToken); |
| | 0 | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | 10 | 116 | | var bookmark = bookmarks.SingleOrDefault(); |
| | | 117 | | |
| | 10 | 118 | | if (bookmark != null) |
| | | 119 | | { |
| | 2 | 120 | | await ResumeWorkflowAsync(httpContext, bookmark, input, correlationId); |
| | 2 | 121 | | return; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // If a base path was configured, the requester tried to execute a workflow that doesn't exist. |
| | 8 | 125 | | if (basePath != null) |
| | | 126 | | { |
| | 7 | 127 | | await httpContext.Response.SendNotFoundAsync(cancellation: cancellationToken); |
| | 7 | 128 | | return; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // If no base path was configured, the request should be handled by subsequent middlewares. |
| | 1 | 132 | | await next(httpContext); |
| | 307 | 133 | | } |
| | | 134 | | |
| | | 135 | | private async Task<WorkflowGraph?> FindWorkflowGraphAsync(IServiceProvider serviceProvider, StoredTrigger trigger, C |
| | | 136 | | { |
| | 0 | 137 | | var workflowDefinitionService = serviceProvider.GetRequiredService<IWorkflowDefinitionService>(); |
| | 0 | 138 | | var workflowDefinitionId = trigger.WorkflowDefinitionVersionId; |
| | 0 | 139 | | return await workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionId, cancellationToken); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | private async Task<IEnumerable<StoredTrigger>> FindTriggersAsync(IServiceProvider serviceProvider, string bookmarkHa |
| | | 143 | | { |
| | 0 | 144 | | var triggerStore = serviceProvider.GetRequiredService<ITriggerStore>(); |
| | 0 | 145 | | var triggerFilter = new TriggerFilter |
| | 0 | 146 | | { |
| | 0 | 147 | | Hash = bookmarkHash |
| | 0 | 148 | | }; |
| | 0 | 149 | | return await triggerStore.FindManyAsync(triggerFilter, cancellationToken); |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | private async Task<IEnumerable<StoredBookmark>> FindBookmarksAsync(IServiceProvider serviceProvider, string bookmark |
| | | 153 | | { |
| | 10 | 154 | | var bookmarkStore = serviceProvider.GetRequiredService<IBookmarkStore>(); |
| | 10 | 155 | | var bookmarkFilter = new BookmarkFilter |
| | 10 | 156 | | { |
| | 10 | 157 | | Hash = bookmarkHash, |
| | 10 | 158 | | WorkflowInstanceId = workflowInstanceId, |
| | 10 | 159 | | CorrelationId = correlationId |
| | 10 | 160 | | }; |
| | 10 | 161 | | return await bookmarkStore.FindManyAsync(bookmarkFilter, cancellationToken); |
| | 10 | 162 | | } |
| | | 163 | | |
| | | 164 | | private async Task StartWorkflowAsync(HttpContext httpContext, StoredTrigger trigger, WorkflowGraph workflowGraph, I |
| | | 165 | | { |
| | 258 | 166 | | var bookmarkPayload = trigger.GetPayload<HttpEndpointBookmarkPayload>(); |
| | 258 | 167 | | var workflowOptions = new RunWorkflowOptions |
| | 258 | 168 | | { |
| | 258 | 169 | | Input = input, |
| | 258 | 170 | | CorrelationId = correlationId, |
| | 258 | 171 | | TriggerActivityId = trigger.ActivityId, |
| | 258 | 172 | | WorkflowInstanceId = workflowInstanceId |
| | 258 | 173 | | }; |
| | | 174 | | |
| | 258 | 175 | | await ExecuteWorkflowAsync(httpContext, workflowGraph, workflowOptions, bookmarkPayload, null, input); |
| | 258 | 176 | | } |
| | | 177 | | |
| | | 178 | | private async Task ResumeWorkflowAsync(HttpContext httpContext, StoredBookmark bookmark, IDictionary<string, object> |
| | | 179 | | { |
| | 2 | 180 | | var serviceProvider = httpContext.RequestServices; |
| | 2 | 181 | | var cancellationToken = httpContext.RequestAborted; |
| | 2 | 182 | | var bookmarkPayload = bookmark.GetPayload<HttpEndpointBookmarkPayload>(); |
| | 2 | 183 | | var workflowInstanceStore = serviceProvider.GetRequiredService<IWorkflowInstanceStore>(); |
| | 2 | 184 | | var workflowInstance = await workflowInstanceStore.FindAsync(bookmark.WorkflowInstanceId, cancellationToken); |
| | | 185 | | |
| | 2 | 186 | | if (workflowInstance == null) |
| | | 187 | | { |
| | 0 | 188 | | await httpContext.Response.SendNotFoundAsync(cancellation: cancellationToken); |
| | 0 | 189 | | return; |
| | | 190 | | } |
| | | 191 | | |
| | 2 | 192 | | var workflowDefinitionService = serviceProvider.GetRequiredService<IWorkflowDefinitionService>(); |
| | 2 | 193 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(workflowInstance.DefinitionVersionId, |
| | | 194 | | |
| | 2 | 195 | | if (workflowGraph == null) |
| | | 196 | | { |
| | 0 | 197 | | await httpContext.Response.SendNotFoundAsync(cancellation: cancellationToken); |
| | 0 | 198 | | return; |
| | | 199 | | } |
| | | 200 | | |
| | 2 | 201 | | var runWorkflowParams = new RunWorkflowOptions |
| | 2 | 202 | | { |
| | 2 | 203 | | WorkflowInstanceId = workflowInstance.Id, |
| | 2 | 204 | | Input = input, |
| | 2 | 205 | | CorrelationId = correlationId, |
| | 2 | 206 | | ActivityHandle = bookmark.ActivityInstanceId != null ? ActivityHandle.FromActivityInstanceId(bookmark.Activi |
| | 2 | 207 | | BookmarkId = bookmark.Id |
| | 2 | 208 | | }; |
| | | 209 | | |
| | 2 | 210 | | await ExecuteWorkflowAsync(httpContext, workflowGraph, runWorkflowParams, bookmarkPayload, workflowInstance, inp |
| | 2 | 211 | | } |
| | | 212 | | |
| | | 213 | | private async Task ExecuteWorkflowAsync(HttpContext httpContext, WorkflowGraph workflowGraph, RunWorkflowOptions wor |
| | | 214 | | { |
| | 260 | 215 | | var serviceProvider = httpContext.RequestServices; |
| | 260 | 216 | | var cancellationToken = httpContext.RequestAborted; |
| | 260 | 217 | | var workflow = workflowGraph.Workflow; |
| | | 218 | | |
| | 260 | 219 | | if (!await AuthorizeAsync(serviceProvider, httpContext, workflow, bookmarkPayload, cancellationToken)) |
| | | 220 | | { |
| | 0 | 221 | | httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; |
| | 0 | 222 | | return; |
| | | 223 | | } |
| | | 224 | | |
| | 260 | 225 | | var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>(); |
| | 260 | 226 | | var result = await ExecuteWithinTimeoutAsync(async ct => |
| | 260 | 227 | | { |
| | 260 | 228 | | if (workflowInstance == null) |
| | 258 | 229 | | return await workflowRunner.RunAsync(workflowGraph, workflowOptions, ct); |
| | 2 | 230 | | return await workflowRunner.RunAsync(workflow, workflowInstance.WorkflowState, workflowOptions, ct); |
| | 520 | 231 | | }, bookmarkPayload.RequestTimeout, httpContext); |
| | 260 | 232 | | await HandleWorkflowFaultAsync(serviceProvider, httpContext, result, cancellationToken); |
| | 260 | 233 | | } |
| | | 234 | | |
| | | 235 | | private async Task<T> ExecuteWithinTimeoutAsync<T>(Func<CancellationToken, Task<T>> action, TimeSpan? requestTimeout |
| | | 236 | | { |
| | | 237 | | // If no request timeout is specified, execute the action without any timeout. |
| | 260 | 238 | | if (requestTimeout == null) |
| | 260 | 239 | | return await action(httpContext.RequestAborted); |
| | | 240 | | |
| | | 241 | | // Create a combined cancellation token that cancels when the request is aborted or when the request timeout is |
| | 0 | 242 | | using var requestTimeoutCancellationTokenSource = new CancellationTokenSource(); |
| | 0 | 243 | | requestTimeoutCancellationTokenSource.CancelAfter(requestTimeout.Value); |
| | 0 | 244 | | using var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted, requ |
| | 0 | 245 | | var originalCancellationToken = httpContext.RequestAborted; |
| | | 246 | | |
| | | 247 | | // Replace the original cancellation token with the combined one. |
| | 0 | 248 | | httpContext.RequestAborted = combinedTokenSource.Token; |
| | | 249 | | |
| | | 250 | | // Execute the action. |
| | 0 | 251 | | var result = await action(httpContext.RequestAborted); |
| | | 252 | | |
| | | 253 | | // Restore the original cancellation token. |
| | 0 | 254 | | httpContext.RequestAborted = originalCancellationToken; |
| | | 255 | | |
| | 0 | 256 | | return result; |
| | 260 | 257 | | } |
| | | 258 | | |
| | | 259 | | private HttpRouteData GetMatchingRoute(IServiceProvider serviceProvider, string path) |
| | | 260 | | { |
| | 268 | 261 | | var routeMatcher = serviceProvider.GetRequiredService<IRouteMatcher>(); |
| | 268 | 262 | | var routeTable = serviceProvider.GetRequiredService<IRouteTable>(); |
| | | 263 | | |
| | 268 | 264 | | var matchingRouteQuery = |
| | 268 | 265 | | from routeData in routeTable |
| | 2684 | 266 | | let routeValues = routeMatcher.Match(routeData.Route, path) |
| | 2684 | 267 | | where routeValues != null |
| | 534 | 268 | | select new |
| | 534 | 269 | | { |
| | 534 | 270 | | route = routeData, |
| | 534 | 271 | | routeValues |
| | 534 | 272 | | }; |
| | | 273 | | |
| | 268 | 274 | | var matchingRoute = matchingRouteQuery.FirstOrDefault(); |
| | | 275 | | |
| | 268 | 276 | | return matchingRoute?.route ?? new HttpRouteData(path); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private static string? TryStripBasePath(string route, string? basePath) |
| | | 280 | | { |
| | 268 | 281 | | if (string.IsNullOrWhiteSpace(basePath) || basePath == "/") |
| | 1 | 282 | | return route; |
| | | 283 | | |
| | 267 | 284 | | var routeSegments = GetRouteSegments(route); |
| | 267 | 285 | | var basePathSegments = GetRouteSegments(basePath); |
| | 267 | 286 | | var basePathIndex = FindSegmentSequence(routeSegments, basePathSegments); |
| | | 287 | | |
| | 267 | 288 | | if (basePathIndex != 0) |
| | 0 | 289 | | return null; |
| | | 290 | | |
| | 267 | 291 | | var remainingSegments = routeSegments.Skip(basePathIndex + basePathSegments.Length); |
| | 267 | 292 | | return remainingSegments.Any() ? $"/{string.Join('/', remainingSegments)}" : "/"; |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | private static bool IsBasePathMatch(string route, string basePath) => |
| | 306 | 296 | | basePath == "/" || |
| | 306 | 297 | | string.Equals(route, basePath, StringComparison.OrdinalIgnoreCase) || |
| | 306 | 298 | | route.StartsWith($"{basePath}/", StringComparison.OrdinalIgnoreCase); |
| | | 299 | | |
| | | 300 | | private static int FindSegmentSequence(string[] routeSegments, string[] candidateSegments) |
| | | 301 | | { |
| | 267 | 302 | | if (candidateSegments.Length == 0) |
| | 0 | 303 | | return 0; |
| | | 304 | | |
| | 534 | 305 | | for (var startIndex = 0; startIndex <= routeSegments.Length - candidateSegments.Length; startIndex++) |
| | | 306 | | { |
| | 267 | 307 | | var isMatch = true; |
| | | 308 | | |
| | 1068 | 309 | | for (var candidateIndex = 0; candidateIndex < candidateSegments.Length; candidateIndex++) |
| | | 310 | | { |
| | 267 | 311 | | if (!string.Equals(routeSegments[startIndex + candidateIndex], candidateSegments[candidateIndex], String |
| | | 312 | | { |
| | 0 | 313 | | isMatch = false; |
| | 0 | 314 | | break; |
| | | 315 | | } |
| | | 316 | | } |
| | | 317 | | |
| | 267 | 318 | | if (isMatch) |
| | 267 | 319 | | return startIndex; |
| | | 320 | | } |
| | | 321 | | |
| | 0 | 322 | | return -1; |
| | | 323 | | } |
| | | 324 | | |
| | 534 | 325 | | private static string[] GetRouteSegments(string route) => route.Trim('/').Split('/', StringSplitOptions.RemoveEmptyE |
| | | 326 | | |
| | | 327 | | private async Task<string?> GetCorrelationIdAsync(IServiceProvider serviceProvider, HttpContext httpContext, Cancell |
| | | 328 | | { |
| | 268 | 329 | | var correlationIdSelectors = serviceProvider.GetServices<IHttpCorrelationIdSelector>(); |
| | | 330 | | |
| | 268 | 331 | | var correlationId = default(string); |
| | | 332 | | |
| | 2122 | 333 | | foreach (var selector in correlationIdSelectors.OrderByDescending(x => x.Priority)) |
| | | 334 | | { |
| | 530 | 335 | | correlationId = await selector.GetCorrelationIdAsync(httpContext, cancellationToken); |
| | | 336 | | |
| | 530 | 337 | | if (correlationId != null) |
| | | 338 | | break; |
| | | 339 | | } |
| | | 340 | | |
| | 268 | 341 | | return correlationId; |
| | 268 | 342 | | } |
| | | 343 | | |
| | | 344 | | private async Task<string?> GetWorkflowInstanceIdAsync(IServiceProvider serviceProvider, HttpContext httpContext, Ca |
| | | 345 | | { |
| | 268 | 346 | | var workflowInstanceIdSelectors = serviceProvider.GetServices<IHttpWorkflowInstanceIdSelector>(); |
| | | 347 | | |
| | 268 | 348 | | var workflowInstanceId = default(string); |
| | | 349 | | |
| | 2122 | 350 | | foreach (var selector in workflowInstanceIdSelectors.OrderByDescending(x => x.Priority)) |
| | | 351 | | { |
| | 530 | 352 | | workflowInstanceId = await selector.GetWorkflowInstanceIdAsync(httpContext, cancellationToken); |
| | | 353 | | |
| | 530 | 354 | | if (workflowInstanceId != null) |
| | | 355 | | break; |
| | | 356 | | } |
| | | 357 | | |
| | 268 | 358 | | return workflowInstanceId; |
| | 268 | 359 | | } |
| | | 360 | | |
| | | 361 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")] |
| | | 362 | | private static async Task WriteResponseAsync(HttpContext httpContext, CancellationToken cancellationToken) |
| | | 363 | | { |
| | 0 | 364 | | var response = httpContext.Response; |
| | | 365 | | |
| | 0 | 366 | | if (!response.HasStarted) |
| | | 367 | | { |
| | 0 | 368 | | response.ContentType = MediaTypeNames.Application.Json; |
| | 0 | 369 | | response.StatusCode = StatusCodes.Status200OK; |
| | | 370 | | |
| | 0 | 371 | | var model = new |
| | 0 | 372 | | { |
| | 0 | 373 | | workflowInstanceIds = Array.Empty<string>(), |
| | 0 | 374 | | }; |
| | | 375 | | |
| | 0 | 376 | | var json = JsonSerializer.Serialize(model); |
| | 0 | 377 | | await response.WriteAsync(json, cancellationToken); |
| | | 378 | | } |
| | 0 | 379 | | } |
| | | 380 | | |
| | | 381 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")] |
| | | 382 | | private async Task<bool> HandleMultipleWorkflowsFoundAsync(HttpContext httpContext, Func<IEnumerable<object>> workfl |
| | | 383 | | { |
| | 0 | 384 | | httpContext.Response.ContentType = "application/json"; |
| | 0 | 385 | | httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; |
| | | 386 | | |
| | 0 | 387 | | var responseContent = JsonSerializer.Serialize(new |
| | 0 | 388 | | { |
| | 0 | 389 | | errorMessage = "The call is ambiguous and matches multiple workflows.", |
| | 0 | 390 | | workflows = workflowMatches().ToArray() |
| | 0 | 391 | | }); |
| | | 392 | | |
| | 0 | 393 | | await httpContext.Response.WriteAsync(responseContent, cancellationToken); |
| | 0 | 394 | | return true; |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | private async Task<bool> HandleWorkflowFaultAsync(IServiceProvider serviceProvider, HttpContext httpContext, RunWork |
| | | 398 | | { |
| | 260 | 399 | | if (!workflowExecutionResult.WorkflowState.Incidents.Any() || httpContext.Response.HasStarted) |
| | 260 | 400 | | return false; |
| | | 401 | | |
| | 0 | 402 | | var httpEndpointFaultHandler = serviceProvider.GetRequiredService<IHttpEndpointFaultHandler>(); |
| | 0 | 403 | | var workflowInstanceManager = serviceProvider.GetRequiredService<IWorkflowInstanceManager>(); |
| | 0 | 404 | | var workflowState = (await workflowInstanceManager.FindByIdAsync(workflowExecutionResult.WorkflowState.Id, cance |
| | 0 | 405 | | await httpEndpointFaultHandler.HandleAsync(new(httpContext, workflowState.WorkflowState, cancellationToken)); |
| | 0 | 406 | | return true; |
| | 260 | 407 | | } |
| | | 408 | | |
| | | 409 | | private async Task<bool> AuthorizeAsync( |
| | | 410 | | IServiceProvider serviceProvider, |
| | | 411 | | HttpContext httpContext, |
| | | 412 | | Workflow workflow, |
| | | 413 | | HttpEndpointBookmarkPayload bookmarkPayload, |
| | | 414 | | CancellationToken cancellationToken) |
| | | 415 | | { |
| | 260 | 416 | | var httpEndpointAuthorizationHandler = serviceProvider.GetRequiredService<IHttpEndpointAuthorizationHandler>(); |
| | | 417 | | |
| | 260 | 418 | | if (bookmarkPayload.Authorize == false) |
| | 260 | 419 | | return true; |
| | | 420 | | |
| | 0 | 421 | | return await httpEndpointAuthorizationHandler.AuthorizeAsync(new(httpContext, workflow, bookmarkPayload.Policy)) |
| | 260 | 422 | | } |
| | | 423 | | |
| | | 424 | | private string ComputeBookmarkHash(IServiceProvider serviceProvider, string path, string method) |
| | | 425 | | { |
| | 268 | 426 | | var bookmarkPayload = new HttpEndpointBookmarkPayload(path, method); |
| | 268 | 427 | | var bookmarkHasher = serviceProvider.GetRequiredService<IStimulusHasher>(); |
| | 268 | 428 | | return bookmarkHasher.Hash(HttpStimulusNames.HttpEndpoint, bookmarkPayload); |
| | | 429 | | } |
| | | 430 | | } |