Browse Source

Normalize the HTTP client/client handler actions to always take an OpenIddict*SystemNetHttpContext parameter

pull/2578/head
Kévin Chalet 1 week ago
parent
commit
4d03fff2d2
  1. 15
      sandbox/OpenIddict.Sandbox.Maui.Client/MauiProgram.cs
  2. 88
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs
  3. 4
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs
  4. 3
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpContext.cs
  5. 4
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpOptions.cs
  6. 6
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs
  7. 4
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs
  8. 3
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpContext.cs
  9. 4
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpOptions.cs

15
sandbox/OpenIddict.Sandbox.Maui.Client/MauiProgram.cs

@ -54,15 +54,18 @@ public static class MauiProgram
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp()
.SetProductInformation(typeof(MauiProgram).Assembly)
#if IOS
// Warning: server certificate validation is disabled to simplify testing the MAUI
// application with the iOS simulator: in production, it SHOULD NEVER be disabled.
.ConfigureHttpClientHandler(static (context, handler) =>
{
#if DEBUG && IOS
#pragma warning disable MA0039
.ConfigureHttpClientHandler("Local", handler => handler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator)
// Warning: server certificate validation is disabled to simplify testing the MAUI
// application with the iOS simulator: in production, it SHOULD NEVER be disabled.
handler.ServerCertificateCustomValidationCallback = context.Registration.ProviderName is "Local"
? HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
: handler.ServerCertificateCustomValidationCallback;
#pragma warning restore MA0039
#endif
;
});
// Add a client registration matching the client application definition in the server project.
options.AddRegistration(new OpenIddictClientRegistration

88
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs

@ -11,7 +11,6 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Reflection;
using OpenIddict.Client;
using OpenIddict.Client.SystemNetHttp;
using Polly;
@ -62,47 +61,6 @@ public sealed class OpenIddictClientSystemNetHttpBuilder
return this;
}
/// <summary>
/// Configures the <see cref="HttpClient"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
/// <remarks>
/// Note: customizations configured using this method apply to all client registrations.
/// </remarks>
/// <param name="configuration">The delegate used to configure the <see cref="HttpClient"/>.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(Action<HttpClient> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
return ConfigureHttpClient((registration, client) => configuration(client));
}
/// <summary>
/// Configures the <see cref="HttpClient"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
/// <remarks>
/// Note: customizations configured using this method only apply
/// to client registrations that use the specified provider name.
/// </remarks>
/// <param name="provider">The provider name, to which the customizations are applied.</param>
/// <param name="configuration">The delegate used to configure the <see cref="HttpClient"/>.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(string provider, Action<HttpClient> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentException.ThrowIfNullOrEmpty(provider);
return ConfigureHttpClient((registration, client) =>
{
if (string.Equals(registration.ProviderName, provider, StringComparison.Ordinal))
{
configuration(client);
}
});
}
/// <summary>
/// Configures the <see cref="HttpClient"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
@ -113,54 +71,14 @@ public sealed class OpenIddictClientSystemNetHttpBuilder
/// <param name="configuration">The delegate used to configure the <see cref="HttpClient"/>.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(Action<OpenIddictClientRegistration, HttpClient> configuration)
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(
Action<OpenIddictClientSystemNetHttpContext, HttpClient> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
return Configure(options => options.HttpClientActions.Add(configuration));
}
/// <summary>
/// Configures the <see cref="HttpClientHandler"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
/// <remarks>
/// Note: customizations configured using this method apply to all client registrations.
/// </remarks>
/// <param name="configuration">The delegate used to configure the <see cref="HttpClientHandler"/>.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler(Action<HttpClientHandler> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
return ConfigureHttpClientHandler((registration, handler) => configuration(handler));
}
/// <summary>
/// Configures the <see cref="HttpClientHandler"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
/// <remarks>
/// Note: customizations configured using this method only apply
/// to client registrations that use the specified provider name.
/// </remarks>
/// <param name="provider">The provider name, to which the customizations are applied.</param>
/// <param name="configuration">The delegate used to configure the <see cref="HttpClientHandler"/>.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler(string provider, Action<HttpClientHandler> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentException.ThrowIfNullOrEmpty(provider);
return ConfigureHttpClientHandler((registration, handler) =>
{
if (string.Equals(registration.ProviderName, provider, StringComparison.Ordinal))
{
configuration(handler);
}
});
}
/// <summary>
/// Configures the <see cref="HttpClientHandler"/> used by the OpenIddict client/System.Net.Http integration.
/// </summary>
@ -172,7 +90,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler(
Action<OpenIddictClientRegistration, HttpClientHandler> configuration)
Action<OpenIddictClientSystemNetHttpContext, HttpClientHandler> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);

4
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs

@ -93,7 +93,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio
// Register the user-defined HTTP client actions.
foreach (var action in settings.HttpClientActions)
{
options.HttpClientActions.Add(client => action(context.Registration, client));
options.HttpClientActions.Add(client => action(context, client));
}
options.HttpMessageHandlerBuilderActions.Add(builder =>
@ -140,7 +140,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio
// Register the user-defined HTTP client handler actions.
foreach (var action in settings.HttpClientHandlerActions)
{
options.HttpMessageHandlerBuilderActions.Add(builder => action(context.Registration,
options.HttpMessageHandlerBuilderActions.Add(builder => action(context,
builder.PrimaryHandler as HttpClientHandler
?? throw new InvalidOperationException(SR.FormatID0373(typeof(HttpClientHandler).FullName))));
}

3
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpContext.cs

@ -15,7 +15,6 @@ namespace OpenIddict.Client.SystemNetHttp;
/// <summary>
/// Represents the context used by the System.Net.Http integration when creating a new HTTP client.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class OpenIddictClientSystemNetHttpContext
{
private static readonly AsyncLocal<OpenIddictClientSystemNetHttpContext?> _current = new();
@ -29,6 +28,7 @@ public sealed class OpenIddictClientSystemNetHttpContext
/// <summary>
/// Gets or sets the ambient context for the current execution flow.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static OpenIddictClientSystemNetHttpContext? Current
{
get => _current.Value;
@ -45,6 +45,7 @@ public sealed class OpenIddictClientSystemNetHttpContext
/// </summary>
/// <param name="context">The client context for which to compute the stable identifier.</param>
/// <returns>A string representing the stable identifier for the specified context.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static string ComputeStableId(OpenIddictClientSystemNetHttpContext context)
{
ArgumentNullException.ThrowIfNull(context);

4
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpOptions.cs

@ -95,11 +95,11 @@ public sealed class OpenIddictClientSystemNetHttpOptions
/// Gets the user-defined actions used to amend the <see cref="HttpClient"/>
/// instances created by the OpenIddict client/System.Net.Http integration.
/// </summary>
public List<Action<OpenIddictClientRegistration, HttpClient>> HttpClientActions { get; } = [];
public List<Action<OpenIddictClientSystemNetHttpContext, HttpClient>> HttpClientActions { get; } = [];
/// <summary>
/// Gets the user-defined actions used to amend the <see cref="HttpClientHandler"/>
/// instances created by the OpenIddict client/System.Net.Http integration.
/// </summary>
public List<Action<OpenIddictClientRegistration, HttpClientHandler>> HttpClientHandlerActions { get; } = [];
public List<Action<OpenIddictClientSystemNetHttpContext, HttpClientHandler>> HttpClientHandlerActions { get; } = [];
}

6
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs

@ -67,7 +67,8 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder
/// <param name="configuration">The delegate used to configure the <see cref="HttpClient"/>.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClient(Action<HttpClient> configuration)
public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClient(
Action<OpenIddictValidationSystemNetHttpContext, HttpClient> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
@ -80,7 +81,8 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder
/// <param name="configuration">The delegate used to configure the <see cref="HttpClientHandler"/>.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/> instance.</returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClientHandler(Action<HttpClientHandler> configuration)
public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClientHandler(
Action<OpenIddictValidationSystemNetHttpContext, HttpClientHandler> configuration)
{
ArgumentNullException.ThrowIfNull(configuration);

4
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs

@ -90,7 +90,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO
// Register the user-defined HTTP client actions.
foreach (var action in settings.HttpClientActions)
{
options.HttpClientActions.Add(action);
options.HttpClientActions.Add(client => action(context, client));
}
options.HttpMessageHandlerBuilderActions.Add(builder =>
@ -137,7 +137,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO
// Register the user-defined HTTP client handler actions.
foreach (var action in settings.HttpClientHandlerActions)
{
options.HttpMessageHandlerBuilderActions.Add(builder => action(builder.PrimaryHandler as HttpClientHandler
options.HttpMessageHandlerBuilderActions.Add(builder => action(context, builder.PrimaryHandler as HttpClientHandler
?? throw new InvalidOperationException(SR.FormatID0373(typeof(HttpClientHandler).FullName))));
}
}

3
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpContext.cs

@ -14,7 +14,6 @@ namespace OpenIddict.Validation.SystemNetHttp;
/// <summary>
/// Represents the context used by the System.Net.Http integration when creating a new HTTP client.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class OpenIddictValidationSystemNetHttpContext
{
private static readonly AsyncLocal<OpenIddictValidationSystemNetHttpContext?> _current = new();
@ -28,6 +27,7 @@ public sealed class OpenIddictValidationSystemNetHttpContext
/// <summary>
/// Gets or sets the ambient context for the current execution flow.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static OpenIddictValidationSystemNetHttpContext? Current
{
get => _current.Value;
@ -39,6 +39,7 @@ public sealed class OpenIddictValidationSystemNetHttpContext
/// </summary>
/// <param name="context">The client context for which to compute the stable identifier.</param>
/// <returns>A string representing the stable identifier for the specified context.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static string ComputeStableId(OpenIddictValidationSystemNetHttpContext context)
{
ArgumentNullException.ThrowIfNull(context);

4
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpOptions.cs

@ -95,11 +95,11 @@ public sealed class OpenIddictValidationSystemNetHttpOptions
/// Gets the user-defined actions used to amend the <see cref="HttpClient"/>
/// instances created by the OpenIddict validation/System.Net.Http integration.
/// </summary>
public List<Action<HttpClient>> HttpClientActions { get; } = [];
public List<Action<OpenIddictValidationSystemNetHttpContext, HttpClient>> HttpClientActions { get; } = [];
/// <summary>
/// Gets the user-defined actions used to amend the <see cref="HttpClientHandler"/>
/// instances created by the OpenIddict validation/System.Net.Http integration.
/// </summary>
public List<Action<HttpClientHandler>> HttpClientHandlerActions { get; } = [];
public List<Action<OpenIddictValidationSystemNetHttpContext, HttpClientHandler>> HttpClientHandlerActions { get; } = [];
}

Loading…
Cancel
Save