/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Http; using Microsoft.Extensions.Options; namespace OpenIddict.Client.SystemNetHttp; /// /// Contains the methods required to ensure that the OpenIddict client/System.Net.Http integration configuration is valid. /// public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptions, IConfigureNamedOptions { #if !SUPPORTS_SERVICE_PROVIDER_IN_HTTP_MESSAGE_HANDLER_BUILDER private readonly IServiceProvider _provider; public OpenIddictClientSystemNetHttpConfiguration(IServiceProvider provider) => _provider = provider ?? throw new ArgumentNullException(nameof(provider)); #endif public void Configure(OpenIddictClientOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } // Register the built-in event handlers used by the OpenIddict System.Net.Http client components. options.Handlers.AddRange(OpenIddictClientSystemNetHttpHandlers.DefaultHandlers); } public void Configure(HttpClientFactoryOptions options) => Configure(Options.DefaultName, options); public void Configure(string? name, HttpClientFactoryOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } // Only amend the HTTP client factory options if the instance is managed by OpenIddict. var assembly = typeof(OpenIddictClientSystemNetHttpOptions).Assembly.GetName(); if (!string.Equals(name, assembly.Name, StringComparison.Ordinal)) { return; } options.HttpClientActions.Add(options => { // By default, HttpClient uses a default timeout of 100 seconds and allows payloads of up to 2GB. // To help reduce the effects of malicious responses (e.g responses returned at a very slow pace // or containing an infine amount of data), the default values are amended to use lower values. options.MaxResponseContentBufferSize = 10 * 1024 * 1024; options.Timeout = TimeSpan.FromMinutes(1); }); options.HttpMessageHandlerBuilderActions.Add(builder => { #if SUPPORTS_SERVICE_PROVIDER_IN_HTTP_MESSAGE_HANDLER_BUILDER var options = builder.Services.GetRequiredService>(); #else var options = _provider.GetRequiredService>(); #endif var policy = options.CurrentValue.HttpErrorPolicy; if (policy is not null) { builder.AdditionalHandlers.Add(new PolicyHttpMessageHandler(policy)); } }); } }