< Summary

Information
Class: Elsa.ExternalAuthentication.Services.AdapterSettingsSecretFieldGuard
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/AdapterSettingsSecretFieldGuard.cs
Line coverage
89%
Covered lines: 26
Uncovered lines: 3
Coverable lines: 29
Total lines: 58
Line coverage: 89.6%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ThrowIfContainsDeclaredSecret(...)50%4471.42%
RedactDeclaredSecrets(...)75%121294.44%
GetSecretFieldNames(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/AdapterSettingsSecretFieldGuard.cs

#LineLine coverage
 1using System.Text.Json;
 2using Elsa.ExternalAuthentication.Models;
 3
 4namespace Elsa.ExternalAuthentication.Services;
 5
 6/// <summary>
 7/// Prevents adapter setting fields that are declared as secret bindings from being persisted or returned as ordinary se
 8/// </summary>
 9public static class AdapterSettingsSecretFieldGuard
 10{
 11    /// <summary>Throws when a descriptor-declared secret is supplied through an adapter settings document.</summary>
 12    public static void ThrowIfContainsDeclaredSecret(JsonElement settings, ExternalAuthenticationAdapterDescriptor descr
 13    {
 114        if (settings.ValueKind != JsonValueKind.Object)
 015            return;
 16
 117        var names = GetSecretFieldNames(descriptor);
 218        var name = names.FirstOrDefault(name => settings.TryGetProperty(name, out _));
 119        if (name is not null)
 120            throw new InvalidOperationException($"Configuration connection '{connectionKey}' supplies secret field '{nam
 021    }
 22
 23    /// <summary>Returns a settings document with descriptor-declared secret fields redacted.</summary>
 24    public static JsonElement RedactDeclaredSecrets(JsonElement settings, ExternalAuthenticationAdapterDescriptor descri
 25    {
 3226        if (settings.ValueKind != JsonValueKind.Object)
 027            return settings.ValueKind == JsonValueKind.Undefined ? default : settings.Clone();
 28
 3229        var names = GetSecretFieldNames(descriptor);
 6430        if (names.Count == 0 || !names.Any(name => settings.TryGetProperty(name, out _)))
 3131            return settings.Clone();
 32
 133        using var stream = new MemoryStream();
 134        using (var writer = new Utf8JsonWriter(stream))
 35        {
 136            writer.WriteStartObject();
 637            foreach (var property in settings.EnumerateObject())
 38            {
 239                writer.WritePropertyName(property.Name);
 240                if (names.Contains(property.Name))
 141                    writer.WriteStringValue(ExternalAuthenticationRedactor.RedactedValue);
 42                else
 143                    property.Value.WriteTo(writer);
 44            }
 45
 146            writer.WriteEndObject();
 147        }
 48
 149        using var document = JsonDocument.Parse(stream.ToArray());
 150        return document.RootElement.Clone();
 151    }
 52
 53    private static HashSet<string> GetSecretFieldNames(ExternalAuthenticationAdapterDescriptor descriptor) =>
 3354        descriptor.Fields
 6555            .Where(x => x.IsSecretBinding)
 3356            .Select(x => x.Name)
 3357            .ToHashSet(StringComparer.Ordinal);
 58}