Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix PostConfigureOAuth2IntrospectionOptions with named options #166

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NatMarchand
Copy link

@NatMarchand NatMarchand commented Oct 24, 2022

Hi there!
We are currently encountering a weird issue described as follow:
Given I bind OAuth2IntrospectionOptions with an IConfiguration section
and using a IOptionsMonitor 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 NatMarchand force-pushed the fix/fixpostconfigureoauth2introspectionoptionsnamed branch from 70151da to d8eb36e Compare October 24, 2022 16:51
@leastprivilege leastprivilege self-assigned this Oct 24, 2022
@NatMarchand
Copy link
Author

Hello there !
Is there anything I can do to help you about this MR ? :)

@leastprivilege leastprivilege removed their request for review November 28, 2022 15:18
@brockallen
Copy link
Member

Are you seeing this under .NET 6 or .NET Core 3.1?

@NatMarchand
Copy link
Author

The code above was tested on LinqPad with dotnet 7 but we also have issues with our apps running on dotnet 6

@brockallen
Copy link
Member

I'd like to understand better why it's invoked without a name/scheme only when the source reloads, as opposed to normal startup (first time). Can you find out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants