Skip to content

Commit

Permalink
feat: Add section to KeycloakConfigurationSource
Browse files Browse the repository at this point in the history
  • Loading branch information
NikiforovAll committed Apr 26, 2024
1 parent 73cf9ca commit b52e8f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Keycloak.AuthServices.Authentication.Configuration;

using Keycloak.AuthServices.Common;
using Microsoft.Extensions.Hosting;

/// <summary>
Expand All @@ -12,15 +13,22 @@ public static class HostBuilderExtensions
/// </summary>
/// <param name="hostBuilder"></param>
/// <param name="fileName"></param>
/// <param name="section"></param>
/// <returns></returns>
public static IHostBuilder ConfigureKeycloakConfigurationSource(
this IHostBuilder hostBuilder,
string fileName = "keycloak.json"
string fileName = "keycloak.json",
string section = ConfigurationConstants.ConfigurationPrefix
) =>
hostBuilder.ConfigureAppConfiguration(
(_, builder) =>
{
var source = new KeycloakConfigurationSource { Path = fileName, Optional = false };
var source = new KeycloakConfigurationSource
{
Path = fileName,
Optional = false,
ConfigurationPrefix = section
};
builder.Sources.Insert(0, source);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace Keycloak.AuthServices.Authentication.Configuration;

using System.Globalization;
using System.Text;
using Common;
using Microsoft.Extensions.Configuration.Json;

/// <summary>
/// Keycloak file configuration provider
/// </summary>
public class KeycloakConfigurationProvider : JsonConfigurationProvider
{
private readonly string configurationPrefix;

private const char KeycloakPropertyDelimiter = '-';
private const char NestedConfigurationDelimiter = ':';
private const int Utf8LowerCaseDistant = 32;
Expand All @@ -19,8 +20,16 @@ public class KeycloakConfigurationProvider : JsonConfigurationProvider
/// Initializes a new instance with the specified source.
/// </summary>
/// <param name="source">The source settings.</param>
public KeycloakConfigurationProvider(KeycloakConfigurationSource source)
: base(source) => this.stringBuilder = new StringBuilder();
/// <param name="configurationPrefix"></param>
public KeycloakConfigurationProvider(
KeycloakConfigurationSource source,
string configurationPrefix
)
: base(source)
{
this.stringBuilder = new StringBuilder();
this.configurationPrefix = configurationPrefix;
}

/// <summary>
/// Loads the JSON data from a stream.
Expand Down Expand Up @@ -71,10 +80,10 @@ private string NormalizeKey(string key)
}

var result =
ConfigurationConstants.ConfigurationPrefix
+ NestedConfigurationDelimiter
+ this.stringBuilder.ToString();
this.configurationPrefix + NestedConfigurationDelimiter + this.stringBuilder.ToString();

this.stringBuilder.Clear();

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Keycloak.AuthServices.Authentication.Configuration;
/// </summary>
public class KeycloakConfigurationSource : JsonConfigurationSource
{
/// <summary>
/// Configuration prefix for source.
/// </summary>
public string ConfigurationPrefix { get; set; } = default!;

/// <summary>
/// Builds the <see cref="JsonConfigurationProvider"/> for this source.
/// </summary>
Expand All @@ -16,6 +21,7 @@ public class KeycloakConfigurationSource : JsonConfigurationSource
public override IConfigurationProvider Build(IConfigurationBuilder builder)
{
this.EnsureDefaults(builder);
return new KeycloakConfigurationProvider(this);

return new KeycloakConfigurationProvider(this, this.ConfigurationPrefix);
}
}

0 comments on commit b52e8f4

Please sign in to comment.