| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Connections.Contracts; |
| | | 4 | | using Elsa.Connections.Features; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Connections.Services; |
| | | 8 | | |
| | | 9 | | /// <summary>Separates metadata inspection from connection management and credential use.</summary> |
| | 4 | 10 | | public sealed class DefaultConnectionMetadataInspector( |
| | 4 | 11 | | IConnectionLifecycleStore? store, |
| | 4 | 12 | | IConnectionUseAuthorizer authorizer, |
| | 4 | 13 | | ITenantAccessor tenantAccessor, |
| | 4 | 14 | | IOptions<ConnectionInspectionOptions> options) : IConnectionMetadataInspector |
| | | 15 | | { |
| | | 16 | | public async Task<ConnectionInspectionMetadata?> InspectAsync( |
| | | 17 | | ClaimsPrincipal principal, string connectionId, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 11 | 19 | | var tenantId = tenantAccessor.TenantId; |
| | 11 | 20 | | var environmentId = options.Value.EnvironmentId; |
| | 11 | 21 | | if (store is null || principal.Identity?.IsAuthenticated != true || string.IsNullOrWhiteSpace(connectionId) || c |
| | 11 | 22 | | string.IsNullOrWhiteSpace(tenantId) || tenantId is Tenant.DefaultTenantId or Tenant.AgnosticTenantId || tena |
| | 11 | 23 | | string.IsNullOrWhiteSpace(environmentId) || environmentId.Length > 200) |
| | | 24 | | { |
| | 6 | 25 | | return null; |
| | | 26 | | } |
| | | 27 | | |
| | 5 | 28 | | var request = new ConnectionUseRequest(principal, ConnectionUseKind.Human, tenantId, environmentId, connectionId |
| | 5 | 29 | | if (!await authorizer.AuthorizeAsync(request, cancellationToken)) |
| | | 30 | | { |
| | 3 | 31 | | return null; |
| | | 32 | | } |
| | | 33 | | |
| | 2 | 34 | | var connection = await store.FindAsync(connectionId, tenantId, environmentId, cancellationToken); |
| | 2 | 35 | | return connection is null |
| | 2 | 36 | | ? null |
| | 2 | 37 | | : new ConnectionInspectionMetadata(connection.Id, connection.ProviderId, connection.ProviderAccountId, |
| | 2 | 38 | | connection.Status, connection.Revision); |
| | 11 | 39 | | } |
| | | 40 | | } |