Skip to content

Commit ae4937a

Browse files
tlupesjmprieur
andauthored
Allow multiple certificate observers (#3506)
* Allow multiple certificate observers --------- Co-authored-by: Jean-Marc Prieur <[email protected]>
1 parent 3159b90 commit ae4937a

File tree

7 files changed

+413
-11
lines changed

7 files changed

+413
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#nullable enable
22
const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string!
3+
readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#nullable enable
22
const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string!
3+
readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#nullable enable
22
const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string!
3+
readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#nullable enable
22
const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string!
3+
readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#nullable enable
22
const Microsoft.Identity.Web.Constants.UserIdKey = "IDWEB_USER_ID" -> string!
3+
readonly Microsoft.Identity.Web.TokenAcquisition._certificatesObservers -> System.Collections.Generic.IReadOnlyList<Microsoft.Identity.Web.Experimental.ICertificatesObserver!>!

src/Microsoft.Identity.Web.TokenAcquisition/TokenAcquisition.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ class OAuthConstants
5858
protected readonly IServiceProvider _serviceProvider;
5959
protected readonly ITokenAcquisitionHost _tokenAcquisitionHost;
6060
protected readonly ICredentialsLoader _credentialsLoader;
61-
protected readonly ICertificatesObserver? _certificatesObserver;
61+
protected readonly IReadOnlyList<ICertificatesObserver> _certificatesObservers;
6262
protected readonly IOptionsMonitor<TokenAcquisitionExtensionOptions>? tokenAcquisitionExtensionOptionsMonitor;
6363

64+
[Obsolete("Use _certificatesObservers instead.")]
65+
protected readonly ICertificatesObserver? _certificatesObserver;
66+
6467
/// <summary>
6568
/// Scopes which are already requested by MSAL.NET. They should not be re-requested;.
6669
/// </summary>
@@ -106,7 +109,10 @@ public TokenAcquisition(
106109
_serviceProvider = serviceProvider;
107110
_tokenAcquisitionHost = tokenAcquisitionHost;
108111
_credentialsLoader = credentialsLoader;
112+
_certificatesObservers = [.. serviceProvider.GetServices<ICertificatesObserver>()];
113+
#pragma warning disable CS0618 // Type or member is obsolete. Setup for backward compatibility.
109114
_certificatesObserver = serviceProvider.GetService<ICertificatesObserver>();
115+
#pragma warning restore CS0618 // Type or member is obsolete
110116
tokenAcquisitionExtensionOptionsMonitor = serviceProvider.GetService<IOptionsMonitor<TokenAcquisitionExtensionOptions>>();
111117
_miHttpFactory = serviceProvider.GetService<IManagedIdentityTestHttpClientFactory>();
112118
}
@@ -1030,17 +1036,19 @@ private void NotifyCertificateSelection(
10301036
Exception? exception)
10311037
{
10321038
X509Certificate2 selectedCertificate = app.AppConfig.ClientCredentialCertificate;
1033-
if (_certificatesObserver != null
1034-
&& selectedCertificate != null)
1039+
if (selectedCertificate != null)
10351040
{
1036-
_certificatesObserver.OnClientCertificateChanged(
1037-
new CertificateChangeEventArg()
1038-
{
1039-
Action = action,
1040-
Certificate = app.AppConfig.ClientCredentialCertificate,
1041-
CredentialDescription = mergedOptions.ClientCredentials?.FirstOrDefault(c => c.Certificate == selectedCertificate),
1042-
ThrownException = exception,
1043-
});
1041+
for (int i = 0; i < _certificatesObservers.Count; i++)
1042+
{
1043+
_certificatesObservers[i].OnClientCertificateChanged(
1044+
new CertificateChangeEventArg()
1045+
{
1046+
Action = action,
1047+
Certificate = app.AppConfig.ClientCredentialCertificate,
1048+
CredentialDescription = mergedOptions.ClientCredentials?.FirstOrDefault(c => c.Certificate == selectedCertificate),
1049+
ThrownException = exception,
1050+
});
1051+
}
10441052
}
10451053
}
10461054

0 commit comments

Comments
 (0)