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

Dynamic Providers with OIDC Events #190

Open
josephdecock opened this issue May 23, 2024 · 1 comment
Open

Dynamic Providers with OIDC Events #190

josephdecock opened this issue May 23, 2024 · 1 comment

Comments

@josephdecock
Copy link
Member

We use the OIDC handler's events for lots of advanced functionality (JWT authentication, JAR, PAR, etc). It would be good to add a sample to show how to do so for dynamic providers.

@josephdecock
Copy link
Member Author

Some beginnings:

public class ConfigureDynamicProviders(IHttpContextAccessor httpContextAccessor) 
    : IPostConfigureOptions<OpenIdConnectOptions>
{
    public void PostConfigure(string? name, OpenIdConnectOptions options)
    {
        ArgumentNullException.ThrowIfNull(name, nameof(name));
        if(httpContextAccessor.HttpContext is null)
        {
            throw new InvalidOperationException("No Http Context (are you accidentally trying to configure providers in a background service?)");
        }

        // We have to resolve the cache this way because of scoping
        var cache = httpContextAccessor.HttpContext.RequestServices.GetRequiredService<DynamicAuthenticationSchemeCache>();

        var provider = cache.GetIdentityProvider<OidcProvider>(name);
        if(provider != null)
        {
            provider.Properties.TryGetValue("custom", out var custom);

            // Instantiate an Events object that will do the authentication work you like
            // options.Events = new PrivateKeyJwtOidcEvents(name, custom)
        }
        else
        {
            // The cache will miss for any statically configured OIDC providers (if you have a mix of static and dynamic)
        }
    }
}

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

No branches or pull requests

1 participant