| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.SasTokens.Contracts; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using FastEndpoints; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.AspNetCore.Http; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Api.Endpoints.Bookmarks.Resume; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Resumes a bookmarked workflow instance with the bookmark ID specified in the provided SAS token. |
| | | 15 | | /// </summary> |
| | | 16 | | [PublicAPI] |
| | 3 | 17 | | internal class Resume(ITokenService tokenService, IWorkflowResumer workflowResumer, IBookmarkQueue bookmarkQueue, IPaylo |
| | | 18 | | { |
| | | 19 | | private const long MaxBookmarkResumeBodySize = 1024 * 1024; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public override void Configure() |
| | | 23 | | { |
| | 3 | 24 | | Routes("/bookmarks/resume"); |
| | 3 | 25 | | Verbs(Http.GET, Http.POST); |
| | 3 | 26 | | AllowAnonymous(); |
| | 3 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 31 | | { |
| | 0 | 32 | | var token = Query<string?>("t", false); |
| | | 33 | | |
| | 0 | 34 | | if (string.IsNullOrWhiteSpace(token) || !tokenService.TryDecryptToken<BookmarkTokenPayload>(token, out var paylo |
| | | 35 | | { |
| | 0 | 36 | | AddError("Invalid token."); |
| | 0 | 37 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | var asynchronous = Query<bool>("async", false); |
| | 0 | 42 | | var input = await GetInputAsync(cancellationToken); |
| | | 43 | | |
| | 0 | 44 | | if (ValidationFailed) |
| | | 45 | | { |
| | 0 | 46 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Some clients, like Blazor, may prematurely cancel their request upon navigation away from the page. |
| | | 51 | | // In this case, we don't want to cancel the workflow execution. |
| | | 52 | | // We need to better understand the conditions that cause this. |
| | 0 | 53 | | var workflowCancellationToken = CancellationToken.None; |
| | 0 | 54 | | await ResumeBookmarkedWorkflowAsync(payload, input, asynchronous, workflowCancellationToken); |
| | | 55 | | |
| | 0 | 56 | | if (!HttpContext.Response.HasStarted) |
| | 0 | 57 | | await Send.OkAsync(cancellation: cancellationToken); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | private IDictionary<string, object>? GetInputFromQueryString() |
| | | 61 | | { |
| | 0 | 62 | | var inputJson = Query<string?>("in", false); |
| | 0 | 63 | | if (string.IsNullOrWhiteSpace(inputJson)) |
| | 0 | 64 | | return null; |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | 0 | 68 | | return payloadSerializer.Deserialize<IDictionary<string, object>>(inputJson); |
| | | 69 | | } |
| | 0 | 70 | | catch (Exception e) when (e is JsonException or NotSupportedException or InvalidOperationException or FormatExce |
| | | 71 | | { |
| | 0 | 72 | | AddError("Invalid input format. Expected a valid JSON string."); |
| | 0 | 73 | | return null; |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | private async ValueTask<IDictionary<string, object>?> GetInputAsync(CancellationToken cancellationToken) |
| | | 78 | | { |
| | 0 | 79 | | return HttpContext.Request.Method == HttpMethods.Post |
| | 0 | 80 | | ? await GetInputFromBodyAsync(cancellationToken) |
| | 0 | 81 | | : GetInputFromQueryString(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private async ValueTask<IDictionary<string, object>?> GetInputFromBodyAsync(CancellationToken cancellationToken) |
| | | 85 | | { |
| | 0 | 86 | | if (HttpContext.Request.ContentLength == 0) |
| | 0 | 87 | | return null; |
| | | 88 | | |
| | 0 | 89 | | var body = await ReadRequestBodyAsync(cancellationToken); |
| | 0 | 90 | | if (ValidationFailed) |
| | 0 | 91 | | return null; |
| | | 92 | | |
| | 0 | 93 | | if (string.IsNullOrWhiteSpace(body)) |
| | 0 | 94 | | return null; |
| | | 95 | | |
| | | 96 | | try |
| | | 97 | | { |
| | 0 | 98 | | var request = apiSerializer.Deserialize<Request>(body); |
| | 0 | 99 | | if (request == null) |
| | | 100 | | { |
| | 0 | 101 | | AddError("Invalid input format. Expected a valid JSON request body."); |
| | 0 | 102 | | return null; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | return request.Input; |
| | | 106 | | } |
| | 0 | 107 | | catch (Exception e) when (e is JsonException or NotSupportedException or InvalidOperationException or FormatExce |
| | | 108 | | { |
| | 0 | 109 | | AddError("Invalid input format. Expected a valid JSON request body."); |
| | 0 | 110 | | return null; |
| | | 111 | | } |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | private async ValueTask<string?> ReadRequestBodyAsync(CancellationToken cancellationToken) |
| | | 115 | | { |
| | 0 | 116 | | var request = HttpContext.Request; |
| | 0 | 117 | | if (request.ContentLength is > MaxBookmarkResumeBodySize) |
| | | 118 | | { |
| | 0 | 119 | | AddError("Request body is too large."); |
| | 0 | 120 | | return null; |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | await using var body = new MemoryStream(); |
| | 0 | 124 | | var buffer = new byte[81920]; |
| | 0 | 125 | | long totalBytesRead = 0; |
| | | 126 | | |
| | 0 | 127 | | while (true) |
| | | 128 | | { |
| | 0 | 129 | | var bytesRead = await request.Body.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken); |
| | 0 | 130 | | if (bytesRead == 0) |
| | | 131 | | break; |
| | | 132 | | |
| | 0 | 133 | | totalBytesRead += bytesRead; |
| | 0 | 134 | | if (totalBytesRead > MaxBookmarkResumeBodySize) |
| | | 135 | | { |
| | 0 | 136 | | AddError("Request body is too large."); |
| | 0 | 137 | | return null; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | await body.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken); |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | return body.Length == 0 ? null : Encoding.UTF8.GetString(body.ToArray()); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | private async Task ResumeBookmarkedWorkflowAsync(BookmarkTokenPayload tokenPayload, IDictionary<string, object>? inp |
| | | 147 | | { |
| | 0 | 148 | | var bookmarkId = tokenPayload.BookmarkId; |
| | 0 | 149 | | var workflowInstanceId = tokenPayload.WorkflowInstanceId; |
| | | 150 | | |
| | 0 | 151 | | if (asynchronous) |
| | | 152 | | { |
| | 0 | 153 | | var item = new NewBookmarkQueueItem |
| | 0 | 154 | | { |
| | 0 | 155 | | BookmarkId = bookmarkId, |
| | 0 | 156 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 157 | | Options = new() |
| | 0 | 158 | | { |
| | 0 | 159 | | Input = input |
| | 0 | 160 | | } |
| | 0 | 161 | | }; |
| | | 162 | | |
| | 0 | 163 | | await bookmarkQueue.EnqueueAsync(item, cancellationToken); |
| | 0 | 164 | | return; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | var resumeRequest = new ResumeBookmarkRequest |
| | 0 | 168 | | { |
| | 0 | 169 | | BookmarkId = bookmarkId, |
| | 0 | 170 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 171 | | Input = input |
| | 0 | 172 | | }; |
| | | 173 | | |
| | 0 | 174 | | await workflowResumer.ResumeAsync(resumeRequest, cancellationToken); |
| | 0 | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// The request model for the Resume endpoint. |
| | | 180 | | /// </summary> |
| | | 181 | | internal class Request |
| | | 182 | | { |
| | | 183 | | /// <summary> |
| | | 184 | | /// The input to provide to the workflow when resuming. |
| | | 185 | | /// </summary> |
| | | 186 | | public IDictionary<string, object>? Input { get; set; } |
| | | 187 | | } |