Skip to content

Commit 5ab9e9c

Browse files
committed
Override the default TLS client certificate selectors to support using certificates that don't meet the default requirements for specific providers
1 parent 6f68ef7 commit 5ab9e9c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationConfiguration.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66

77
using System.ComponentModel;
8+
using System.Security.Cryptography.X509Certificates;
89
using Microsoft.Extensions.Options;
10+
using OpenIddict.Client.SystemNetHttp;
11+
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants;
912

1013
namespace OpenIddict.Client.WebIntegration;
1114

@@ -14,7 +17,8 @@ namespace OpenIddict.Client.WebIntegration;
1417
/// </summary>
1518
[EditorBrowsable(EditorBrowsableState.Advanced)]
1619
public sealed partial class OpenIddictClientWebIntegrationConfiguration : IConfigureOptions<OpenIddictClientOptions>,
17-
IPostConfigureOptions<OpenIddictClientOptions>
20+
IPostConfigureOptions<OpenIddictClientOptions>,
21+
IPostConfigureOptions<OpenIddictClientSystemNetHttpOptions>
1822
{
1923
/// <inheritdoc/>
2024
public void Configure(OpenIddictClientOptions options)
@@ -47,6 +51,38 @@ public void PostConfigure(string? name, OpenIddictClientOptions options)
4751
});
4852
}
4953

54+
/// <inheritdoc/>
55+
public void PostConfigure(string? name, OpenIddictClientSystemNetHttpOptions options)
56+
{
57+
if (options is null)
58+
{
59+
throw new ArgumentNullException(nameof(options));
60+
}
61+
62+
// Override the default/user-defined selectors to support attaching TLS client
63+
// certificates that don't meet the requirements enforced by default by OpenIddict.
64+
options.SelfSignedTlsClientAuthenticationCertificateSelector = CreateSelector(options.SelfSignedTlsClientAuthenticationCertificateSelector);
65+
options.TlsClientAuthenticationCertificateSelector = CreateSelector(options.TlsClientAuthenticationCertificateSelector);
66+
67+
static Func<OpenIddictClientRegistration, X509Certificate2?> CreateSelector(Func<OpenIddictClientRegistration, X509Certificate2?> selector)
68+
=> registration =>
69+
{
70+
var certificate = registration.ProviderType switch
71+
{
72+
ProviderTypes.ProSantéConnect => registration.GetProSantéConnectSettings().SigningCertificate,
73+
74+
_ => null
75+
};
76+
77+
if (certificate is not null)
78+
{
79+
return certificate;
80+
}
81+
82+
return selector(registration);
83+
};
84+
}
85+
5086
/// <summary>
5187
/// Amends the registration with the provider-specific configuration logic.
5288
/// </summary>

src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.DependencyInjection.Extensions;
88
using Microsoft.Extensions.Options;
99
using OpenIddict.Client;
10+
using OpenIddict.Client.SystemNetHttp;
1011
using OpenIddict.Client.WebIntegration;
1112

1213
namespace Microsoft.Extensions.DependencyInjection;
@@ -40,6 +41,8 @@ public static OpenIddictClientWebIntegrationBuilder UseWebProviders(this OpenIdd
4041
// Note: TryAddEnumerable() is used here to ensure the initializers are registered only once.
4142
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
4243
IConfigureOptions<OpenIddictClientOptions>, OpenIddictClientWebIntegrationConfiguration>());
44+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
45+
IPostConfigureOptions<OpenIddictClientSystemNetHttpOptions>, OpenIddictClientWebIntegrationConfiguration>());
4346

4447
// Note: the IPostConfigureOptions<OpenIddictClientOptions> service responsible for populating
4548
// the client registrations MUST be registered before OpenIddictClientConfiguration to ensure

0 commit comments

Comments
 (0)