-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathClientCertificateTest.java
More file actions
420 lines (339 loc) · 18.6 KB
/
ClientCertificateTest.java
File metadata and controls
420 lines (339 loc) · 18.6 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import com.nimbusds.jwt.SignedJWT;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPrivateKey;
import java.security.cert.X509Certificate;
import java.util.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
class ClientCertificateTest {
@Test
void testNullKey() {
NullPointerException ex = assertThrows(NullPointerException.class, () -> ClientCertificate.create((PrivateKey) null, null));
assertEquals("PrivateKey is null or empty", ex.getMessage());
}
@Test
void testGetClient() {
final RSAPrivateKey key = mock(RSAPrivateKey.class);
final BigInteger modulus = mock(BigInteger.class);
doReturn(2048).when(modulus).bitLength();
doReturn(modulus).when(key).getModulus();
final ClientCertificate kc = ClientCertificate.create(key, null);
assertNotNull(kc);
}
@Test
void testIClientCertificateInterface_Sha1andSha256() throws NoSuchAlgorithmException, CertificateException {
//See https://github.com/AzureAD/microsoft-authentication-library-for-java/issues/863 for context on this test.
//Essentially, it aims to test compatibility for customers that implemented IClientCertificate in older versions of the library.
//IClientCertificate.publicCertificateHash256() returns null by default if not implemented...
IClientCertificate certificate = new TestClientCredential();
assertNull(certificate.publicCertificateHash256());
//... but ClientCredentialFactory has an implemented version, so it should not be null.
certificate = ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert());
assertNotNull(certificate.publicCertificateHash256());
}
@Test
void testIClientCertificateInterface_CredentialFactoryUsesSha256() throws Exception {
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
ConfidentialClientApplication cca =
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert()))
.authority("https://login.microsoftonline.com/tenant")
.instanceDiscovery(false)
.validateAuthority(false)
.httpClient(httpClientMock)
.build();
HashMap<String, String> tokenResponseValues = new HashMap<>();
tokenResponseValues.put("access_token", "accessTokenSha256");
when(httpClientMock.send(any(HttpRequest.class))).thenAnswer(parameters -> {
HttpRequest request = parameters.getArgument(0);
String requestBody = request.body();
String clientAssertion = extractClientAssertion(requestBody);
if (clientAssertion != null) {
SignedJWT signedJWT = SignedJWT.parse(clientAssertion);
if (signedJWT.getHeader().toJSONObject().containsKey("x5t#S256")) {
return TestHelper.expectedResponse(200, TestHelper.getSuccessfulTokenResponse(tokenResponseValues));
}
}
//If the client assertion is null or does not contain the x5t#S256 header,
// that indicates a problem in assertion generation and this test should fail.
return null;
});
ClientCredentialParameters parameters = ClientCredentialParameters.builder(Collections.singleton("scopes")).build();
IAuthenticationResult result = cca.acquireToken(parameters).get();
assertNotNull(result);
assertEquals("accessTokenSha256", result.accessToken());
}
@Test
void testClientCertificate_GeneratesNewAssertionEachTime() throws Exception {
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
List<String> capturedAssertions = new ArrayList<>();
ConfidentialClientApplication cca =
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert()))
.authority("https://login.microsoftonline.com/tenant")
.instanceDiscovery(false)
.validateAuthority(false)
.httpClient(httpClientMock)
.build();
// Mock the HTTP client to capture assertions from each request
when(httpClientMock.send(any(HttpRequest.class))).thenAnswer(parameters -> {
HttpRequest request = parameters.getArgument(0);
String requestBody = request.body();
String clientAssertion = extractClientAssertion(requestBody);
if (clientAssertion != null) {
capturedAssertions.add(clientAssertion);
// Verify it's a valid JWT with proper headers
SignedJWT signedJWT = SignedJWT.parse(clientAssertion);
if (signedJWT.getHeader().toJSONObject().containsKey("x5t#S256")) {
HashMap<String, String> tokenResponseValues = new HashMap<>();
tokenResponseValues.put("access_token", "access_token_" + capturedAssertions.size());
return TestHelper.expectedResponse(200, TestHelper.getSuccessfulTokenResponse(tokenResponseValues));
}
}
return null;
});
ClientCredentialParameters parameters = ClientCredentialParameters.builder(Collections.singleton("scopes")).skipCache(true).build();
// Make two token requests
IAuthenticationResult result1 = cca.acquireToken(parameters).get();
IAuthenticationResult result2 = cca.acquireToken(parameters).get();
// Verify two unique assertions were generated
assertEquals(2, capturedAssertions.size(), "Two assertions should have been generated");
assertNotEquals(capturedAssertions.get(0), capturedAssertions.get(1),
"Each token request should generate a unique assertion");
// Optional: Parse and verify JWT properties if needed
SignedJWT jwt1 = SignedJWT.parse(capturedAssertions.get(0));
SignedJWT jwt2 = SignedJWT.parse(capturedAssertions.get(1));
// Different JTI (JWT ID) should be used for each assertion
assertNotEquals(jwt1.getJWTClaimsSet().getJWTID(), jwt2.getJWTClaimsSet().getJWTID(),
"Each assertion should have a unique JTI claim");
// Verify the tokens are different
assertNotEquals(result1.accessToken(), result2.accessToken(),
"The access tokens from each request should be different");
}
@Test
void testClientCertificate_TenantOverride() throws Exception {
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
Map<String, String> capturedTenants = new HashMap<>();
ConfidentialClientApplication cca =
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert()))
.authority("https://login.microsoftonline.com/default-tenant")
.instanceDiscovery(false)
.validateAuthority(false)
.httpClient(httpClientMock)
.build();
// Mock the HTTP client to capture and analyze assertions from each request
when(httpClientMock.send(any(HttpRequest.class))).thenAnswer(parameters -> {
HttpRequest request = parameters.getArgument(0);
String requestBody = request.body();
String url = request.url().toString();
// Capture which tenant was used in the authority
String tenant = extractTenantFromUrl(url);
// Extract the assertion to verify its audience claim
String clientAssertion = extractClientAssertion(requestBody);
if (clientAssertion != null) {
SignedJWT signedJWT = SignedJWT.parse(clientAssertion);
// Get the audience claim to verify it matches the tenant
String audience = signedJWT.getJWTClaimsSet().getAudience().get(0);
// Store the tenant and audience for verification
capturedTenants.put(tenant, audience);
// Verify it's a valid JWT with proper headers
if (signedJWT.getHeader().toJSONObject().containsKey("x5t#S256")) {
HashMap<String, String> tokenResponseValues = new HashMap<>();
tokenResponseValues.put("access_token", "access_token_for_" + tenant);
return TestHelper.expectedResponse(200, TestHelper.getSuccessfulTokenResponse(tokenResponseValues));
}
}
return null;
});
// First request with default tenant
ClientCredentialParameters defaultParameters = ClientCredentialParameters.builder(Collections.singleton("scopes"))
.skipCache(true)
.build();
IAuthenticationResult resultDefault = cca.acquireToken(defaultParameters).get();
// Second request with override tenant
String overrideTenant = "override-tenant";
ClientCredentialParameters overrideParameters = ClientCredentialParameters.builder(Collections.singleton("scopes"))
.skipCache(true)
.tenant(overrideTenant)
.build();
IAuthenticationResult resultOverride = cca.acquireToken(overrideParameters).get();
// Verify both requests were processed
assertEquals(2, capturedTenants.size(), "Two requests with different tenants should have been processed");
// Verify both tenants were used
assertTrue(capturedTenants.containsKey("default-tenant"), "Default tenant should have been used");
assertTrue(capturedTenants.containsKey(overrideTenant), "Override tenant should have been used");
// Verify the audience in the JWT assertions reflects the different tenants
assertNotEquals(
capturedTenants.get("default-tenant"),
capturedTenants.get(overrideTenant),
"JWT audience should differ between default and override tenant"
);
// Verify the audience claims match the expected format with the correct tenant
assertTrue(
capturedTenants.get("default-tenant").contains("default-tenant"),
"Audience for default tenant should contain the default tenant name"
);
assertTrue(
capturedTenants.get(overrideTenant).contains(overrideTenant),
"Audience for override tenant should contain the override tenant name"
);
// Verify different access tokens were returned
assertNotEquals(resultDefault.accessToken(), resultOverride.accessToken(),
"Access tokens should differ when using different tenants");
}
@Test
void testClientCertificate_TenantOverride_B2C() throws Exception {
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
String replacementTenant = "overrideTenant";
ConfidentialClientApplication cca =
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert()))
.b2cAuthority(TestConfiguration.B2C_AUTHORITY)
.instanceDiscovery(false)
.validateAuthority(false)
.httpClient(httpClientMock)
.build();
when(httpClientMock.send(any(HttpRequest.class))).thenAnswer(parameters -> {
HttpRequest request = parameters.getArgument(0);
String requestBody = request.body();
String url = request.url().toString();
// Extract the assertion to verify its audience claim
String clientAssertion = extractClientAssertion(requestBody);
if (clientAssertion != null && url.contains(replacementTenant)) {
HashMap<String, String> tokenResponseValues = new HashMap<>();
tokenResponseValues.put("access_token", "access_token_for_" + replacementTenant);
return TestHelper.expectedResponse(200, TestHelper.getSuccessfulTokenResponse(tokenResponseValues));
}
return null;
});
ClientCredentialParameters overrideParameters = ClientCredentialParameters.builder(Collections.singleton("scopes"))
.skipCache(true)
.tenant(replacementTenant)
.build();
IAuthenticationResult result = cca.acquireToken(overrideParameters).get();
assertNotNull(result);
assertEquals("access_token_for_"+ replacementTenant, result.accessToken());
}
/**
* Extracts the tenant name from an authority URL
* @param url The full URL containing the tenant
* @return The tenant name
*/
private String extractTenantFromUrl(String url) {
// Authority URL format is typically https://login.microsoftonline.com/tenant/...
String[] parts = url.split("/");
for (int i = 0; i < parts.length; i++) {
if (parts[i].equalsIgnoreCase("login.microsoftonline.com") && i + 1 < parts.length) {
return parts[i + 1];
}
}
return null;
}
/**
* Extracts the client_assertion value from a URL-encoded request body
* @param requestBody The request body string
* @return The extracted client assertion or null if not found
*/
private String extractClientAssertion(String requestBody) {
try {
// Split the request body into key-value pairs
String[] pairs = requestBody.split("&");
for (String pair : pairs) {
// Find the client_assertion parameter
if (pair.startsWith("client_assertion=")) {
// Extract and URL-decode the value
return URLDecoder.decode(pair.substring("client_assertion=".length()), StandardCharsets.UTF_8.toString());
}
}
} catch (Exception e) {
// In case of any parsing errors
System.err.println("Error extracting client assertion: " + e.getMessage());
}
return null;
}
class TestClientCredential implements IClientCertificate {
@Override
public PrivateKey privateKey() {
return null;
}
@Override
public String publicCertificateHash() {
return "";
}
@Override
public List<String> getEncodedPublicKeyCertificateChain() {
return Collections.emptyList();
}
}
// ========== ClientCertificate: SHA-1 Hash ==========
@Test
void testPublicCertificateHash_Sha1() throws Exception {
IClientCertificate cert = ClientCredentialFactory.createFromCertificate(
TestHelper.getPrivateKey(), TestHelper.getX509Cert());
String sha1Hash = cert.publicCertificateHash();
assertNotNull(sha1Hash, "SHA-1 hash should not be null");
assertFalse(sha1Hash.isEmpty(), "SHA-1 hash should not be empty");
// Base64-encoded SHA-1 is 28 characters
assertEquals(28, sha1Hash.length(), "Base64-encoded SHA-1 should be 28 chars");
}
@Test
void testPublicCertificateHash_Sha256DiffersFromSha1() throws Exception {
IClientCertificate cert = ClientCredentialFactory.createFromCertificate(
TestHelper.getPrivateKey(), TestHelper.getX509Cert());
String sha1Hash = cert.publicCertificateHash();
String sha256Hash = cert.publicCertificateHash256();
assertNotEquals(sha1Hash, sha256Hash,
"SHA-1 and SHA-256 hashes should be different");
}
// ========== ClientCertificate: Certificate Chain Encoding ==========
@Test
void testGetEncodedPublicKeyCertificateChain_singleCert() throws Exception {
ClientCertificate cert = ClientCertificate.create(
TestHelper.getPrivateKey(), TestHelper.getX509Cert());
List<String> chain = cert.getEncodedPublicKeyCertificateChain();
assertNotNull(chain);
assertEquals(1, chain.size(), "Single cert should produce chain of length 1");
assertFalse(chain.get(0).isEmpty(), "Encoded cert should not be empty");
}
@Test
void testGetEncodedPublicKeyCertificateChain_multiCert() throws Exception {
// Create a chain with the same cert repeated (simulates a CA chain)
List<X509Certificate> certChain = Arrays.asList(
TestHelper.getX509Cert(), TestHelper.getX509Cert());
ClientCertificate cert = new ClientCertificate(TestHelper.getPrivateKey(), certChain);
List<String> chain = cert.getEncodedPublicKeyCertificateChain();
assertEquals(2, chain.size(), "Chain with 2 certs should produce 2 encoded entries");
}
// ========== ClientCertificate: getAssertion ==========
@Test
void testGetAssertion_nullAuthority_throwsNullPointerException() {
ClientCertificate cert = ClientCertificate.create(
TestHelper.getPrivateKey(), TestHelper.getX509Cert());
assertThrows(NullPointerException.class,
() -> cert.getAssertion(null, "client-id", false));
}
@Test
void testGetAssertion_aadAuthority_usesSha256() throws Exception {
ClientCertificate cert = ClientCertificate.create(
TestHelper.getPrivateKey(), TestHelper.getX509Cert());
Authority authority = Authority.createAuthority(
new java.net.URL("https://login.microsoftonline.com/tenant/"));
String assertion = cert.getAssertion(authority, "client-id", false);
assertNotNull(assertion, "Assertion should not be null");
// Verify it's a valid JWT (3 dot-separated parts)
String[] parts = assertion.split("\\.");
assertEquals(3, parts.length, "JWT assertion should have 3 parts");
}
}