|
4 | 4 | package com.microsoft.aad.msal4j; |
5 | 5 |
|
6 | 6 | import com.nimbusds.oauth2.sdk.util.URLUtils; |
7 | | -import labapi.App; |
8 | 7 | import org.junit.jupiter.api.Nested; |
9 | 8 | import org.junit.jupiter.api.Test; |
10 | 9 | import org.junit.jupiter.api.TestInstance; |
11 | 10 | import org.junit.jupiter.api.extension.ExtendWith; |
12 | 11 | import org.junit.jupiter.params.ParameterizedTest; |
13 | 12 | import org.junit.jupiter.params.provider.MethodSource; |
14 | 13 | import org.junit.jupiter.params.provider.ValueSource; |
15 | | -import org.mockito.ArgumentCaptor; |
16 | 14 | import org.mockito.junit.jupiter.MockitoExtension; |
17 | 15 |
|
18 | 16 | import java.net.SocketException; |
19 | 17 | import java.nio.file.Path; |
20 | 18 | import java.nio.file.Paths; |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.format.DateTimeFormatter; |
| 21 | +import java.time.temporal.ChronoUnit; |
21 | 22 | import java.util.Collections; |
22 | 23 | import java.util.HashMap; |
23 | 24 | import java.util.List; |
@@ -53,6 +54,12 @@ private String getSuccessfulResponse(String resource) { |
53 | 54 | "\"Bearer\",\"client_id\":\"client_id\"}"; |
54 | 55 | } |
55 | 56 |
|
| 57 | + private String getSuccessfulResponseWithISOExpiresOn(String resource) { |
| 58 | + String expiresOn = DateTimeFormatter.ISO_INSTANT.format(Instant.now().plus(24, ChronoUnit.HOURS));//A long-lived, 24 hour token |
| 59 | + return "{\"access_token\":\"accesstoken\",\"expires_on\":\"" + expiresOn + "\",\"resource\":\"" + resource + "\",\"token_type\":" + |
| 60 | + "\"Bearer\",\"client_id\":\"client_id\"}"; |
| 61 | + } |
| 62 | + |
56 | 63 | private String getSuccessfulResponseWithInvalidJson() { |
57 | 64 | return "missing starting bracket \"access_token\":\"accesstoken\",\"token_type\":" + "\"Bearer\",\"client_id\":\"a bunch of problems}"; |
58 | 65 | } |
@@ -313,6 +320,39 @@ void managedIdentityTest_RefreshOnHalfOfExpiresOn() throws Exception { |
313 | 320 | verify(httpClientMock, times(1)).send(any()); |
314 | 321 | } |
315 | 322 |
|
| 323 | + @Test |
| 324 | + void managedIdentityTest_ISOExpiresOn() throws Exception { |
| 325 | + //All managed identity flows use the same AcquireTokenByManagedIdentitySupplier where refreshOn is set, |
| 326 | + // so any of the MI options should let us verify that it's being set correctly |
| 327 | + IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.APP_SERVICE, appServiceEndpoint); |
| 328 | + ManagedIdentityApplication.setEnvironmentVariables(environmentVariables); |
| 329 | + DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class); |
| 330 | + |
| 331 | + when(httpClientMock.send(expectedRequest(ManagedIdentitySourceType.APP_SERVICE, resource))).thenReturn(expectedResponse(200, getSuccessfulResponseWithISOExpiresOn(resource))); |
| 332 | + |
| 333 | + miApp = ManagedIdentityApplication |
| 334 | + .builder(ManagedIdentityId.systemAssigned()) |
| 335 | + .httpClient(httpClientMock) |
| 336 | + .build(); |
| 337 | + |
| 338 | + // Clear caching to avoid cross test pollution. |
| 339 | + miApp.tokenCache().accessTokens.clear(); |
| 340 | + |
| 341 | + AuthenticationResult result = (AuthenticationResult) miApp.acquireTokenForManagedIdentity( |
| 342 | + ManagedIdentityParameters.builder(resource) |
| 343 | + .build()).get(); |
| 344 | + |
| 345 | + // Calculate what the expected expiration time should be |
| 346 | + long expectedExpiresOn = System.currentTimeMillis() / 1000 + (24 * 3600); // 24 hours from now, used in getSuccessfulResponseWithISOExpiresOn |
| 347 | + |
| 348 | + assertNotNull(result.accessToken()); |
| 349 | + assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource()); |
| 350 | + //Allow a few seconds of difference to account for execution time |
| 351 | + assertTrue((result.expiresOn() - expectedExpiresOn) <= 5); |
| 352 | + |
| 353 | + verify(httpClientMock, times(1)).send(any()); |
| 354 | + } |
| 355 | + |
316 | 356 | @ParameterizedTest |
317 | 357 | @MethodSource("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createDataUserAssignedNotSupported") |
318 | 358 | void managedIdentityTest_UserAssigned_NotSupported(ManagedIdentitySourceType source, String endpoint, ManagedIdentityId id) throws Exception { |
|
0 commit comments