| | | 1 | | using Elsa.Expressions.Models; |
| | | 2 | | using Elsa.Http; |
| | | 3 | | using Elsa.SasTokens.Contracts; |
| | | 4 | | using Elsa.Workflows.Api; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | // ReSharper disable once CheckNamespace |
| | | 9 | | namespace Elsa.Extensions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides extension methods for working with <see cref="ExpressionExecutionContext"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class EventExpressionExecutionContextExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Generates a URL that can be used to trigger an event. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="context">The expression execution context.</param> |
| | | 20 | | /// <param name="eventName">The name of the event to trigger.</param> |
| | | 21 | | /// <param name="lifetime">The lifetime of the event trigger token.</param> |
| | | 22 | | /// <returns>A URL that can be used to trigger an event.</returns> |
| | | 23 | | public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, TimeSpan lif |
| | | 24 | | { |
| | 0 | 25 | | var token = context.GenerateEventTriggerTokenInternal(eventName, lifetime); |
| | 0 | 26 | | return context.GenerateEventTriggerUrlInternal(token); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Generates a URL that can be used to trigger an event. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="context">The expression execution context.</param> |
| | | 33 | | /// <param name="eventName">The name of the event to trigger.</param> |
| | | 34 | | /// <param name="expiresAt">The expiration date of the event trigger token.</param> |
| | | 35 | | /// <returns>A URL that can be used to trigger an event.</returns> |
| | | 36 | | public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, DateTimeOffs |
| | | 37 | | { |
| | 0 | 38 | | var token = context.GenerateEventTriggerTokenInternal(eventName, expiresAt: expiresAt); |
| | 0 | 39 | | return context.GenerateEventTriggerUrlInternal(token); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Generates a URL that can be used to trigger an event. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="context">The expression execution context.</param> |
| | | 46 | | /// <param name="eventName">The name of the event to trigger.</param> |
| | | 47 | | /// <returns>A URL that can be used to trigger an event.</returns> |
| | | 48 | | public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName) |
| | | 49 | | { |
| | 0 | 50 | | var token = context.GenerateEventTriggerTokenInternal(eventName); |
| | 0 | 51 | | return context.GenerateEventTriggerUrlInternal(token); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static string GenerateEventTriggerUrlInternal(this ExpressionExecutionContext context, string token) |
| | | 55 | | { |
| | 0 | 56 | | var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value; |
| | 0 | 57 | | var url = $"{options.RoutePrefix}/events/trigger?t={token}"; |
| | 0 | 58 | | var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>(); |
| | 0 | 59 | | return absoluteUrlProvider.ToAbsoluteUrl(url).ToString(); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private static string GenerateEventTriggerTokenInternal(this ExpressionExecutionContext context, string eventName, T |
| | | 63 | | { |
| | 0 | 64 | | var workflowInstanceId = context.GetWorkflowExecutionContext().Id; |
| | 0 | 65 | | var payload = new EventTokenPayload(eventName, workflowInstanceId); |
| | 0 | 66 | | var tokenService = context.GetRequiredService<ITokenService>(); |
| | | 67 | | |
| | 0 | 68 | | return lifetime != null |
| | 0 | 69 | | ? tokenService.CreateToken(payload, lifetime.Value) |
| | 0 | 70 | | : expiresAt != null |
| | 0 | 71 | | ? tokenService.CreateToken(payload, expiresAt.Value) |
| | 0 | 72 | | : tokenService.CreateToken(payload); |
| | | 73 | | } |
| | | 74 | | } |