Skip to content

Commit a6e0fd4

Browse files
committed
Pass token source and update tests
1 parent 3699125 commit a6e0fd4

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByManagedIdentitySupplier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private AuthenticationResult createFromManagedIdentityResponse(ManagedIdentityRe
9898
long expiresOn = Long.parseLong(managedIdentityResponse.expiresOn);
9999
long refreshOn = calculateRefreshOn(expiresOn);
100100
AuthenticationResultMetadata metadata = AuthenticationResultMetadata.builder()
101+
.tokenSource(TokenSource.IDENTITY_PROVIDER)
101102
.refreshOn(refreshOn)
102103
.build();
103104

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenSilentSupplier.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,37 @@ AuthenticationResult execute() throws Exception {
4343
requestAuthority,
4444
silentRequest.parameters().scopes(),
4545
clientApplication.clientId());
46+
}
4647

47-
if (res == null) {
48-
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
49-
}
48+
if (res == null) {
49+
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
50+
}
5051

51-
//Some cached tokens were found, but this metadata will be overwritten if token needs to be refreshed
52-
res.metadata().tokenSource(TokenSource.CACHE);
52+
//Some cached tokens were found, but this metadata will be overwritten if token needs to be refreshed
53+
res.metadata().tokenSource(TokenSource.CACHE);
5354

54-
if (!StringHelper.isBlank(res.accessToken())) {
55-
clientApplication.serviceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount();
56-
}
55+
if (!StringHelper.isBlank(res.accessToken())) {
56+
clientApplication.serviceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount();
57+
}
58+
59+
shouldRefresh = shouldRefresh(silentRequest.parameters(), res);
5760

58-
shouldRefresh = shouldRefresh(silentRequest.parameters(), res);
59-
60-
if (shouldRefresh) {
61-
if (!StringHelper.isBlank(res.refreshToken())) {
62-
//There are certain scenarios where the cached authority may differ from the client app's authority,
63-
// such as when a request is instance aware. Unless overridden by SilentParameters.authorityUrl, the
64-
// cached authority should be used in the token refresh request
65-
if (silentRequest.parameters().authorityUrl() == null && !res.account().environment().equals(requestAuthority.host)) {
66-
requestAuthority = Authority.createAuthority(new URL(requestAuthority.authority().replace(requestAuthority.host(),
67-
res.account().environment())));
68-
}
69-
70-
res = makeRefreshRequest(res, requestAuthority, clientApplication.serviceBundle().getServerSideTelemetry().getCurrentRequest().cacheInfo());
71-
} else {
72-
res = null;
61+
if (shouldRefresh) {
62+
if (!StringHelper.isBlank(res.refreshToken())) {
63+
//There are certain scenarios where the cached authority may differ from the client app's authority,
64+
// such as when a request is instance aware. Unless overridden by SilentParameters.authorityUrl, the
65+
// cached authority should be used in the token refresh request
66+
if (silentRequest.parameters().authorityUrl() == null && !res.account().environment().equals(requestAuthority.host)) {
67+
requestAuthority = Authority.createAuthority(new URL(requestAuthority.authority().replace(requestAuthority.host(),
68+
res.account().environment())));
7369
}
70+
71+
res = makeRefreshRequest(res, requestAuthority, clientApplication.serviceBundle().getServerSideTelemetry().getCurrentRequest().cacheInfo());
72+
} else {
73+
res = null;
7474
}
7575
}
76+
7677
if (res == null || StringHelper.isBlank(res.accessToken())) {
7778
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
7879
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/ManagedIdentityTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,14 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
191191
.build()).get();
192192

193193
assertNotNull(result.accessToken());
194-
195-
String accessToken = result.accessToken();
194+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
196195

197196
result = miApp.acquireTokenForManagedIdentity(
198197
ManagedIdentityParameters.builder(resource)
199198
.build()).get();
200199

201200
assertNotNull(result.accessToken());
202-
assertEquals(accessToken, result.accessToken());
201+
assertEquals(TokenSource.CACHE, result.metadata().tokenSource());
203202
verify(httpClientMock, times(1)).send(any());
204203
}
205204

@@ -228,6 +227,7 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
228227
.build()).get();
229228

230229
assertNotNull(result.accessToken());
230+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
231231
verify(httpClientMock, times(1)).send(any());
232232
}
233233

@@ -253,6 +253,7 @@ void managedIdentityTest_RefreshOnHalfOfExpiresOn() throws Exception {
253253
long timestampSeconds = (System.currentTimeMillis() / 1000);
254254

255255
assertNotNull(result.accessToken());
256+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
256257
assertEquals((result.expiresOn() - timestampSeconds)/2, result.refreshOn() - timestampSeconds);
257258

258259
verify(httpClientMock, times(1)).send(any());
@@ -320,12 +321,14 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
320321
.build()).get();
321322

322323
assertNotNull(result.accessToken());
324+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
323325

324326
result = miApp.acquireTokenForManagedIdentity(
325327
ManagedIdentityParameters.builder(anotherResource)
326328
.build()).get();
327329

328330
assertNotNull(result.accessToken());
331+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
329332
verify(httpClientMock, times(2)).send(any());
330333
// TODO: Assert token source to check the token source is IDP and not Cache.
331334
}
@@ -565,12 +568,14 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
565568
.build()).get();
566569

567570
assertNotNull(resultMiApp1.accessToken());
571+
assertEquals(TokenSource.IDENTITY_PROVIDER, resultMiApp1.metadata().tokenSource());
568572

569573
IAuthenticationResult resultMiApp2 = miApp2.acquireTokenForManagedIdentity(
570574
ManagedIdentityParameters.builder(resource)
571575
.build()).get();
572576

573577
assertNotNull(resultMiApp2.accessToken());
578+
assertEquals(TokenSource.CACHE, resultMiApp2.metadata().tokenSource());
574579

575580
//acquireTokenForManagedIdentity does a cache lookup by default, and all ManagedIdentityApplication's share a cache,
576581
// so calling acquireTokenForManagedIdentity with the same parameters in two different ManagedIdentityApplications

0 commit comments

Comments
 (0)