| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.ExternalAuthentication.Models; |
| | | 3 | | using Elsa.ExternalAuthentication.Options; |
| | | 4 | | using Microsoft.Extensions.Configuration; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | public static class ExternalAuthenticationConfigurationExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Binds external-authentication options and reconstructs configuration-backed JSON settings. |
| | | 12 | | /// </summary> |
| | | 13 | | public static void BindExternalAuthenticationOptions(this IConfigurationSection section, ExternalAuthenticationOptio |
| | | 14 | | { |
| | 1 | 15 | | section.Bind(options); |
| | | 16 | | |
| | 1 | 17 | | var connectionSections = GetIndexedChildren(section.GetSection("Connections")); |
| | 1 | 18 | | var connections = options.ConfigurationConnections.ToArray(); |
| | 4 | 19 | | for (var i = 0; i < Math.Min(connectionSections.Length, connections.Length); i++) |
| | 1 | 20 | | BindJsonSettings(connectionSections[i], connections[i]); |
| | 1 | 21 | | } |
| | | 22 | | |
| | | 23 | | private static void BindJsonSettings(IConfigurationSection section, IdentityProviderConnection connection) |
| | | 24 | | { |
| | 1 | 25 | | connection.AdapterSettings = GetJsonElement(section, "AdapterSettings"); |
| | | 26 | | |
| | 1 | 27 | | if (connection.UnlinkedPolicy is not null) |
| | 1 | 28 | | connection.UnlinkedPolicy = connection.UnlinkedPolicy with { Settings = GetJsonElement(section.GetSection("U |
| | | 29 | | |
| | 1 | 30 | | var grantSourceSections = GetIndexedChildren(section.GetSection("PermissionGrantSources")); |
| | 1 | 31 | | var grantSources = connection.PermissionGrantSources.ToArray(); |
| | 1 | 32 | | connection.PermissionGrantSources = grantSources |
| | 1 | 33 | | .Select((source, index) => index < grantSourceSections.Length |
| | 1 | 34 | | ? source with { Settings = GetJsonElement(grantSourceSections[index], "Settings") } |
| | 1 | 35 | | : source) |
| | 1 | 36 | | .ToArray(); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static JsonElement GetJsonElement(IConfiguration configuration, string sectionKey) |
| | | 40 | | { |
| | 3 | 41 | | var json = configuration.GetSectionAsJson(sectionKey); |
| | 3 | 42 | | return json is null ? default : JsonSerializer.Deserialize<JsonElement>(json); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private static IConfigurationSection[] GetIndexedChildren(IConfigurationSection section) => |
| | 2 | 46 | | section.GetChildren() |
| | 0 | 47 | | .OrderBy(child => int.TryParse(child.Key, out var index) ? index : int.MaxValue) |
| | 2 | 48 | | .ToArray(); |
| | | 49 | | } |