| | | 1 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 2 | | using Elsa.ExternalAuthentication.Models; |
| | | 3 | | using Microsoft.Extensions.Configuration; |
| | | 4 | | |
| | | 5 | | namespace Elsa.ExternalAuthentication.Services; |
| | | 6 | | |
| | | 7 | | /// <summary>Resolves a deployment-owned secret from configuration without ever exposing it through connection models.</ |
| | 0 | 8 | | public sealed class ConfigurationSecretBindingResolver( |
| | 0 | 9 | | IConfiguration configuration, |
| | 0 | 10 | | IExternalAuthenticationHandleHasher hasher) : ISecretBindingResolver |
| | | 11 | | { |
| | | 12 | | public const string ResolverType = "configuration"; |
| | 0 | 13 | | public string Type => ResolverType; |
| | | 14 | | |
| | | 15 | | public ValueTask<SecretBindingState> GetStateAsync(SecretBinding binding, CancellationToken cancellationToken = defa |
| | | 16 | | { |
| | 0 | 17 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 18 | | EnsureType(binding); |
| | 0 | 19 | | var configured = !string.IsNullOrWhiteSpace(configuration[binding.Reference]); |
| | 0 | 20 | | return ValueTask.FromResult(new SecretBindingState(configured, configured)); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public ValueTask<ResolvedSecretBinding> ResolveAsync(SecretBinding binding, CancellationToken cancellationToken = de |
| | | 24 | | { |
| | 0 | 25 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 26 | | EnsureType(binding); |
| | 0 | 27 | | var value = configuration[binding.Reference]; |
| | 0 | 28 | | if (string.IsNullOrWhiteSpace(value)) |
| | 0 | 29 | | throw new InvalidOperationException("The configured secret binding could not be resolved."); |
| | 0 | 30 | | return ValueTask.FromResult(new ResolvedSecretBinding(new SensitiveString(value), hasher.Hash($"{ResolverType}:{ |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | private static void EnsureType(SecretBinding binding) |
| | | 34 | | { |
| | 0 | 35 | | if (!string.Equals(binding.ResolverType, ResolverType, StringComparison.Ordinal) || string.IsNullOrWhiteSpace(bi |
| | 0 | 36 | | throw new InvalidOperationException("The configured secret binding is invalid."); |
| | 0 | 37 | | } |
| | | 38 | | } |