-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathMtlsPopParametersInitializer.cs
More file actions
197 lines (173 loc) · 8.7 KB
/
MtlsPopParametersInitializer.cs
File metadata and controls
197 lines (173 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Identity.Client.AppConfig;
using Microsoft.Identity.Client.AuthScheme;
using Microsoft.Identity.Client.AuthScheme.PoP;
using Microsoft.Identity.Client.Instance;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.Internal.ClientCredential;
using Microsoft.Identity.Client.TelemetryCore;
namespace Microsoft.Identity.Client.ApiConfig.Parameters
{
/// <summary>
/// Encapsulates the mTLS/PoP initialization logic for token requests.
/// Keeps AcquireTokenCommonParameters lean and makes the init logic testable in isolation.
/// </summary>
internal static class MtlsPopParametersInitializer
{
internal static async Task TryInitAsync(
AcquireTokenCommonParameters p,
IServiceBundle serviceBundle,
CancellationToken ct)
{
if (p.IsMtlsPopRequested)
{
await InitExplicitMtlsPopAsync(p, serviceBundle, ct).ConfigureAwait(false);
return;
}
await TryInitImplicitBearerOverMtlsAsync(p, serviceBundle, ct).ConfigureAwait(false);
}
/// <summary>
/// NON-PoP request:
/// We may still need mTLS transport in two situations:
/// Case 1 – The app-level SendCertificateOverMtls option is set and the credential is certificate-based
/// (both static <see cref="CertificateClientCredential"/> and dynamic
/// <see cref="DynamicCertificateClientCredential"/> are supported).
/// Case 2 – The credential is a signed-assertion provider that returns a TokenBindingCertificate.
/// </summary>
private static async Task TryInitImplicitBearerOverMtlsAsync(
AcquireTokenCommonParameters tokenParameters,
IServiceBundle serviceBundle,
CancellationToken ct)
{
if (tokenParameters.MtlsCertificate != null)
{
return;
}
// Case 1 – App opted into mTLS Bearer via SendCertificateOverMtls on a certificate-based credential.
if (serviceBundle.Config.CertificateOptions?.SendCertificateOverMtls == true &&
serviceBundle.Config.ClientCredential is CertificateAndClaimsClientCredential certBasedCred)
{
// Static credentials have Certificate set directly
tokenParameters.MtlsCertificate = certBasedCred.Certificate
?? await certBasedCred.ResolveCertificateForMtlsAsync(
CreateAssertionRequestOptions(tokenParameters, serviceBundle, ct))
.ConfigureAwait(false);
return;
}
// Case 2 – Only cert-capable credentials implement this capability interface.
// No SendCertificateOverMtls guard here: the TokenBindingCertificate pattern is a
// distinct opt-in where the assertion delegate itself signals mTLS intent by returning
// a non-null cert. This is separate from Case 1 (SendCertificateOverMtls + cert-based
// credential).
//
// Call pattern per request:
// - This call always fires (even cache hits) to check for TokenBindingCertificate
// and set MtlsCertificate for proper endpoint routing.
// - GetCredentialMaterialAsync (in ClientAssertionDelegateCredential) calls the
// delegate a second time on network requests to produce the signed assertion JWT.
// - Cache hits: delegate called once (here only). Network requests: twice.
// - Delegates are expected to be cheap (return a pre-generated/cached assertion).
if (serviceBundle.Config.ClientCredential is IClientSignedAssertionProvider signedProvider)
{
var opts = CreateAssertionRequestOptions(tokenParameters, serviceBundle, ct);
ClientSignedAssertion ar =
await signedProvider.GetAssertionAsync(opts, ct).ConfigureAwait(false);
if (ar?.TokenBindingCertificate != null)
{
tokenParameters.MtlsCertificate = ar.TokenBindingCertificate;
}
}
}
/// <summary>
/// EXPLICIT PoP requested:
/// Validate and initialize PoP parameters (auth scheme + cert + region check).
/// </summary>
private static async Task InitExplicitMtlsPopAsync(
AcquireTokenCommonParameters p,
IServiceBundle serviceBundle,
CancellationToken ct)
{
// Case 1 – Certificate credential
if (serviceBundle.Config.ClientCredential is CertificateClientCredential certCred)
{
if (certCred.Certificate == null)
{
throw new MsalClientException(
MsalError.MtlsCertificateNotProvided,
MsalErrorMessage.MtlsCertificateNotProvidedMessage);
}
await InitMtlsPopParametersAsync(p, certCred.Certificate, serviceBundle, ct).ConfigureAwait(false);
return;
}
// Case 2 – Signed assertion provider (JWT + optional cert)
if (serviceBundle.Config.ClientCredential is IClientSignedAssertionProvider signedProvider)
{
var opts = CreateAssertionRequestOptions(p, serviceBundle, ct);
ClientSignedAssertion ar =
await signedProvider.GetAssertionAsync(opts, ct).ConfigureAwait(false);
if (ar?.TokenBindingCertificate == null)
{
throw new MsalClientException(
MsalError.MtlsCertificateNotProvided,
MsalErrorMessage.MtlsCertificateNotProvidedMessage);
}
await InitMtlsPopParametersAsync(p, ar.TokenBindingCertificate, serviceBundle, ct).ConfigureAwait(false);
return;
}
// Case 3 – Any other credential (client-secret etc.)
throw new MsalClientException(
MsalError.MtlsCertificateNotProvided,
MsalErrorMessage.MtlsCertificateNotProvidedMessage);
}
private static AssertionRequestOptions CreateAssertionRequestOptions(
AcquireTokenCommonParameters p,
IServiceBundle serviceBundle,
CancellationToken ct)
{
return new AssertionRequestOptions
{
ClientID = serviceBundle.Config.ClientId,
ClientCapabilities = serviceBundle.Config.ClientCapabilities,
Claims = p.Claims,
CancellationToken = ct,
ClientAssertionFmiPath = p.ClientAssertionFmiPath,
CorrelationId = p.CorrelationId,
// Best-effort context. IMPORTANT: use AbsoluteUri, not Uri.Authority (host only).
TokenEndpoint = serviceBundle.Config.Authority.AuthorityInfo.CanonicalAuthority.AbsoluteUri
};
}
private static async Task InitMtlsPopParametersAsync(
AcquireTokenCommonParameters p,
X509Certificate2 cert,
IServiceBundle serviceBundle,
CancellationToken ct = default)
{
// AAD only validation
if (serviceBundle.Config.Authority.AuthorityInfo.AuthorityType == AuthorityType.Aad)
{
string tenant = AuthorityInfo.GetFirstPathSegment(serviceBundle.Config.Authority.AuthorityInfo.CanonicalAuthority);
if (AadAuthority.IsCommonOrOrganizationsTenant(tenant))
{
throw new MsalClientException(
MsalError.MissingTenantedAuthority,
MsalErrorMessage.MtlsNonTenantedAuthorityNotAllowedMessage);
}
}
// If the current operation supports the AfterCredentialEvaluation lifecycle hook,
// invoke it with the cert instead of replacing the operation. This enables
// composition (e.g., CDT + mTLS POP) where the operation handles both concerns.
if (p.AuthenticationOperation is IAuthenticationOperation3 op3)
{
await op3.AfterCredentialEvaluationAsync(new CredentialEvaluationContext(cert), ct).ConfigureAwait(false);
p.MtlsCertificate = cert;
return;
}
p.AuthenticationOperation = new MtlsPopAuthenticationOperation(cert);
p.MtlsCertificate = cert;
}
}
}