IdentityModel.AspNetCore.OAuth2Introspection icon indicating copy to clipboard operation
IdentityModel.AspNetCore.OAuth2Introspection copied to clipboard

fix: fix PostConfigureOAuth2IntrospectionOptions with named options

Open NatMarchand opened this issue 2 years ago • 4 comments

Hi there! We are currently encountering a weird issue described as follow: Given I bind OAuth2IntrospectionOptions with an IConfiguration section and using a IOptionsMonitor<OAuth2IntrospectionOptions> somewhere When a configuration provider triggers a reload Then an exception occurs in the PostConfigureOAuth2IntrospectionOptions because dotnet tries to PostConfigure an unnamed options instance (which is of course not configured) and the message is "You must either set Authority or IntrospectionEndpoint"

Here's my repro (on LinqPad):

async Task Main()
{
	var configurationProvider = new MyConfigurationProvider();
	var builder = WebApplication.CreateBuilder();
	((IConfigurationBuilder)builder.Configuration).Add(configurationProvider);
	builder.Services
		.AddAuthentication()
		.AddOAuth2Introspection();

	builder.Services
		.AddOptions<OAuth2IntrospectionOptions>(OAuth2IntrospectionDefaults.AuthenticationScheme)
		.BindConfiguration("IdentityServer");

	var app = builder.Build();

	var monitor = app.Services.GetRequiredService<IOptionsMonitor<OAuth2IntrospectionOptions>>();
	monitor.Get("Bearer").Dump();

	configurationProvider.NotifyReload(); // <-- exception occurs here
}


public class MyConfigurationProvider : ConfigurationProvider, IConfigurationSource
{
	public IConfigurationProvider Build(IConfigurationBuilder builder)
	{
		return this;
	}

	public override void Load()
	{
		this.Data.Clear();
		this.Data.Add("IdentityServer:Authority", "http://dummy");
	}

	public void NotifyReload()
	{
		this.OnReload();
	}
}

My PR fixes this behavior by registering a named PostConfigureOAuth2IntrospectionOptions.

NatMarchand avatar Oct 24 '22 16:10 NatMarchand