Skip to content

Commit 99e5b51

Browse files
authored
using tenant from request for caching (#86)
1 parent 4d4636d commit 99e5b51

22 files changed

+73
-37
lines changed

src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenSilentIT.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,50 @@ private IPublicClientApplication getPublicClientApplicationWithTokensInCache()
171171
.get();
172172
return pca;
173173
}
174+
175+
@Test
176+
private void acquireTokenSilent_usingCommonAuthority_returnCachedAt() throws Exception {
177+
acquireTokenSilent_returnCachedTokens(TestConstants.ORGANIZATIONS_AUTHORITY);
178+
}
179+
180+
@Test
181+
private void acquireTokenSilent_usingTenantSpecificAuthority_returnCachedAt() throws Exception {
182+
LabResponse labResponse = labUserProvider.getDefaultUser(
183+
NationalCloud.AZURE_CLOUD,
184+
false);
185+
String tenantSpecificAuthority = TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId();
186+
187+
acquireTokenSilent_returnCachedTokens(tenantSpecificAuthority);
188+
}
189+
190+
void acquireTokenSilent_returnCachedTokens(String authority) throws Exception {
191+
192+
LabResponse labResponse = labUserProvider.getDefaultUser(
193+
NationalCloud.AZURE_CLOUD,
194+
false);
195+
String password = labUserProvider.getUserPassword(labResponse.getUser());
196+
197+
PublicClientApplication pca = new PublicClientApplication.Builder(
198+
labResponse.getAppId()).
199+
authority(authority).
200+
build();
201+
202+
IAuthenticationResult interactiveAuthResult = pca.acquireToken(UserNamePasswordParameters.
203+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
204+
labResponse.getUser().getUpn(),
205+
password.toCharArray())
206+
.build())
207+
.get();
208+
209+
Assert.assertNotNull(interactiveAuthResult);
210+
211+
IAuthenticationResult silentAuthResult = pca.acquireTokenSilently(
212+
SilentParameters.builder(
213+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), interactiveAuthResult.account())
214+
.build())
215+
.get();
216+
217+
Assert.assertNotNull(silentAuthResult);
218+
Assert.assertEquals(interactiveAuthResult.accessToken(), silentAuthResult.accessToken());
219+
}
174220
}

src/main/java/com/microsoft/aad/msal4j/AccountCacheEntity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ String getKey() {
5959
return String.join(Constants.CACHE_KEY_SEPARATOR, keyParts).toLowerCase();
6060
}
6161

62-
static AccountCacheEntity create(String clientInfoStr, String environment, IdToken idToken, String policy) {
62+
static AccountCacheEntity create(String clientInfoStr, Authority requestAuthority, IdToken idToken, String policy) {
6363

6464
AccountCacheEntity account = new AccountCacheEntity();
6565
account.authorityType(MSSTS_ACCOUNT_TYPE);
6666
account.clientInfoStr = clientInfoStr;
6767
account.homeAccountId(policy != null ?
6868
account.clientInfo().toAccountIdentifier() + Constants.CACHE_KEY_SEPARATOR + policy :
6969
account.clientInfo().toAccountIdentifier());
70-
account.environment(environment);
70+
account.environment(requestAuthority.host());
71+
account.realm(requestAuthority.tenant());
7172

7273
if (idToken != null) {
73-
account.realm(idToken.tenantIdentifier);
7474
String localAccountId = !StringHelper.isBlank(idToken.objectIdentifier)
7575
? idToken.objectIdentifier : idToken.subject;
7676
account.localAccountId(localAccountId);
@@ -81,8 +81,8 @@ static AccountCacheEntity create(String clientInfoStr, String environment, IdTok
8181
return account;
8282
}
8383

84-
static AccountCacheEntity create(String clientInfoStr, String environment, IdToken idToken){
85-
return create(clientInfoStr, environment, idToken, null);
84+
static AccountCacheEntity create(String clientInfoStr, Authority requestAuthority, IdToken idToken){
85+
return create(clientInfoStr, requestAuthority, idToken, null);
8686
}
8787

8888
IAccount toAccount(){

src/main/java/com/microsoft/aad/msal4j/TokenCache.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,7 @@ static private AccessTokenCacheEntity createAccessTokenCacheEntity(TokenRequest
240240
at.environment(environmentAlias);
241241
at.clientId(tokenRequest.getMsalRequest().application().clientId());
242242
at.secret(authenticationResult.accessToken());
243-
244-
IdToken idTokenObj = authenticationResult.idTokenObject();
245-
if (idTokenObj != null) {
246-
at.realm(idTokenObj.tenantIdentifier);
247-
} else {
248-
at.realm(tokenRequest.requestAuthority.tenant());
249-
}
243+
at.realm(tokenRequest.requestAuthority.tenant());
250244

251245
String scopes = !StringHelper.isBlank(authenticationResult.scopes()) ? authenticationResult.scopes() :
252246
tokenRequest.getMsalRequest().msalAuthorizationGrant().getScopes();
@@ -275,11 +269,7 @@ static private IdTokenCacheEntity createIdTokenCacheEntity(TokenRequest tokenReq
275269
idToken.environment(environmentAlias);
276270
idToken.clientId(tokenRequest.getMsalRequest().application().clientId());
277271
idToken.secret(authenticationResult.idToken());
278-
279-
IdToken idTokenObj = authenticationResult.idTokenObject();
280-
if (idTokenObj != null) {
281-
idToken.realm(idTokenObj.tenantIdentifier);
282-
}
272+
idToken.realm(tokenRequest.requestAuthority.tenant());
283273

284274
return idToken;
285275
}

src/main/java/com/microsoft/aad/msal4j/TokenRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ AuthenticationResult executeOauthRequestAndProcessResponse()
8080

8181
accountCacheEntity = AccountCacheEntity.create(
8282
response.getClientInfo(),
83-
requestAuthority.host(),
83+
requestAuthority,
8484
idToken,
8585
authority.policy);
8686
} else {
8787
accountCacheEntity = AccountCacheEntity.create(
8888
response.getClientInfo(),
89-
requestAuthority.host(),
89+
requestAuthority,
9090
idToken);
9191
}
9292
}

src/test/resources/AAD_cache_data/account_cache_entity.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
44
"username": "[email protected]",
55
"environment": "login.microsoftonline.com",
6-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
6+
"realm": "common",
77
"authority_type": "MSSTS",
88
"name": "Cloud IDLAB Basic User",
99
"client_info": "eyJ1aWQiOiI5ZjQ4ODBkOC04MGJhLTRjNDAtOTdiYy1mN2EyM2M3MDMwODQiLCJ1dGlkIjoiZjY0NWFkOTItZTM4ZC00ZDFhLWI1MTAtZDFiMDlhNzRhOGNhIn0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-f645ad92-e38d-4d1a-b510-d1b09a74a8ca
1+
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-common

src/test/resources/AAD_cache_data/at_cache_entity.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"extended_expires_on": "<extended_expires_on>",
55
"credential_type": "AccessToken",
66
"environment": "login.microsoftonline.com",
7-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
7+
"realm": "common",
88
"expires_on": "<expires_on>",
99
"cached_at": "<cached_at>",
1010
"client_id": "b6c69a37-df96-4db0-9088-2ab96e1d8215",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-accesstoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-f645ad92-e38d-4d1a-b510-d1b09a74a8ca-calendars.read openid profile tasks.read user.read email
1+
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-accesstoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-common-calendars.read openid profile tasks.read user.read email

src/test/resources/AAD_cache_data/id_token_cache_entity.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"credential_type": "IdToken",
44
"environment": "login.microsoftonline.com",
55
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
6-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
6+
"realm": "common",
77
"client_id": "b6c69a37-df96-4db0-9088-2ab96e1d8215"
88
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-idtoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-f645ad92-e38d-4d1a-b510-d1b09a74a8ca-
1+
9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-idtoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-common-

0 commit comments

Comments
 (0)