| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 4 | | using Elsa.ExternalAuthentication.Models; |
| | | 5 | | using Elsa.ExternalAuthentication.Permissions; |
| | | 6 | | using Elsa.ExternalAuthentication.Services; |
| | | 7 | | using Microsoft.AspNetCore.Http; |
| | | 8 | | |
| | | 9 | | namespace Elsa.ExternalAuthentication.Endpoints.Connections; |
| | | 10 | | |
| | | 11 | | internal static class ConnectionEndpointSupport |
| | | 12 | | { |
| | | 13 | | public static bool TryGetExpectedRevision(HttpContext context, out long revision) |
| | | 14 | | { |
| | 25 | 15 | | revision = default; |
| | 25 | 16 | | var value = context.Request.Headers.IfMatch.FirstOrDefault(); |
| | 25 | 17 | | return !string.IsNullOrWhiteSpace(value) && value.Length >= 2 && value[0] == '"' && value[^1] == '"' && long.Try |
| | | 18 | | } |
| | | 19 | | |
| | 31 | 20 | | public static void SetEtag(HttpContext context, long revision) => context.Response.Headers.ETag = $"\"{revision}\""; |
| | | 21 | | |
| | 14 | 22 | | public static Task SendMutationResultAsync(HttpContext context, ManagementConnectionMutationResult result, IdentityP |
| | 14 | 23 | | { |
| | 0 | 24 | | ManagementConnectionMutationResult.NotFound => SendErrorAsync(context, StatusCodes.Status404NotFound, "not_found |
| | 0 | 25 | | ManagementConnectionMutationResult.Forbidden => SendErrorAsync(context, StatusCodes.Status403Forbidden, "forbidd |
| | 4 | 26 | | ManagementConnectionMutationResult.Conflict conflict => SendErrorAsync(context, StatusCodes.Status409Conflict, " |
| | 2 | 27 | | ManagementConnectionMutationResult.PreconditionFailed conflict => SendErrorAsync(context, StatusCodes.Status412P |
| | 8 | 28 | | ManagementConnectionMutationResult.ValidationFailed validation => SendErrorAsync(context, StatusCodes.Status400B |
| | 0 | 29 | | _ => throw new InvalidOperationException("A successful connection mutation must be handled by the endpoint.") |
| | 14 | 30 | | }; |
| | | 31 | | |
| | | 32 | | public static Task SendErrorAsync(HttpContext context, int statusCode, string error, string message, object? details |
| | | 33 | | { |
| | 23 | 34 | | context.Response.StatusCode = statusCode; |
| | 23 | 35 | | return context.Response.WriteAsJsonAsync( |
| | 23 | 36 | | new ManagementErrorResponse(error, message, details, BrokerErrorFactory.CreateCorrelationId()), |
| | 23 | 37 | | cancellationToken); |
| | | 38 | | } |
| | | 39 | | |
| | 9 | 40 | | public static Task SendErrorAsync(HttpContext context, int statusCode, string error, string message, CancellationTok |
| | | 41 | | |
| | 16 | 42 | | public static bool HasPermission(ClaimsPrincipal user, string permission) => user.FindAll(PermissionNames.ClaimType) |
| | 27 | 43 | | public static bool RequiresPolicyManagement(ConnectionRequest request) => request.UnlinkedPolicy is not null || requ |
| | 25 | 44 | | public static bool IsDatabaseOwned(EffectiveIdentityProviderConnection connection) => connection.Ownership == Connec |
| | | 45 | | } |