Skip to content

Commit

Permalink
Make MaskinportenSettingsconfiguration path configurable (#756)
Browse files Browse the repository at this point in the history
* Implements new ConfigureMaskinportenClient method
  • Loading branch information
danielskovli authored Sep 10, 2024
1 parent 3e8be7c commit eca9a69
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,37 @@ Action<MaskinportenSettings> configureOptions
return services;
}

/// <summary>
/// <para>
/// Binds a <see cref="MaskinportenClient"/> configuration to the supplied config section path.
/// </para>
/// <para>
/// If you have already provided a <see cref="MaskinportenSettings"/> configuration, either manually or
/// implicitly via <see cref="WebHostBuilderExtensions.ConfigureAppWebHost"/>, this will be overridden.
/// </para>
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configSectionPath">The configuration section path (Eg. "MaskinportenSettings")</param>
public static IServiceCollection ConfigureMaskinportenClient(
this IServiceCollection services,
string configSectionPath
)
{
services.AddOptions<MaskinportenSettings>().BindConfiguration(configSectionPath).ValidateDataAnnotations();

return services;
}

/// <summary>
/// Adds a singleton <see cref="AddMaskinportenClient"/> service to the service collection.
/// Binds <see cref="MaskinportenSettings"/>, either from `appsettings.json` or `maskinporten-settings.json` (if found).
/// <br/><br/>Note: This binding happens in <see cref="WebHostBuilderExtensions.AddMaskinportenSettingsFile"/>.
/// If no <see cref="MaskinportenSettings"/> configuration is found, it binds one to the path "MaskinportenSettings".
/// </summary>
/// <param name="services">The service collection</param>
private static IServiceCollection AddMaskinportenClient(this IServiceCollection services)
{
if (services.GetOptionsDescriptor<MaskinportenSettings>() is null)
{
services
.AddOptions<MaskinportenSettings>()
.BindConfiguration("MaskinportenSettings")
.ValidateDataAnnotations();
services.ConfigureMaskinportenClient("MaskinportenSettings");
}

services.AddSingleton<IMaskinportenClient, MaskinportenClient>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,42 @@ public void ConfigureMaskinportenClient_LastConfigurationOverwritesOthers()
settings.Authority.Should().Be(authority);
}

[Fact]
public void ConfigureMaskinportenClient_BindsToSpecifiedConfigPath()
{
// Arrange
var clientId = "the-client-id";
var authority = "https://maskinporten.dev/";
var jwkBase64 = "gibberish";

List<KeyValuePair<string, string?>> configData =
[
new("CustomMaskinportenSettings:clientId", clientId),
new("CustomMaskinportenSettings:authority", authority),
new("CustomMaskinportenSettings:jwkBase64", jwkBase64),
];

// Act
var app = AppBuilder.Build(
configData: configData,
registerCustomAppServices: services =>
{
services.ConfigureMaskinportenClient("CustomMaskinportenSettings");
}
);

// Assert
var optionsMonitor = app.Services.GetRequiredService<IOptionsMonitor<MaskinportenSettings>>();
Assert.NotNull(optionsMonitor);

var settings = optionsMonitor.CurrentValue;
Assert.NotNull(settings);

settings.ClientId.Should().Be(clientId);
settings.Authority.Should().Be(authority);
settings.JwkBase64.Should().Be(jwkBase64);
}

[Theory]
[InlineData("client1", "scope1")]
[InlineData("client2", "scope1", "scope2", "scope3")]
Expand Down

0 comments on commit eca9a69

Please sign in to comment.