Skip to content

Commit ffa9f72

Browse files
authored
Merge pull request #65 from AzureAD/sagonzal/cacheTests
Add acquire token silent and remove accounts tests. Fix remove accounts
2 parents 074de34 + 2632e38 commit ffa9f72

File tree

15 files changed

+454
-20
lines changed

15 files changed

+454
-20
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
package com.microsoft.aad.msal4j;
25+
26+
import labapi.FederationProvider;
27+
import labapi.LabResponse;
28+
import labapi.LabUserProvider;
29+
import labapi.NationalCloud;
30+
import org.testng.Assert;
31+
import org.testng.annotations.BeforeClass;
32+
import org.testng.annotations.Test;
33+
34+
import java.util.Collections;
35+
import java.util.Set;
36+
37+
public class AcquireTokenSilentIT {
38+
private LabUserProvider labUserProvider;
39+
40+
@BeforeClass
41+
public void setUp() {
42+
labUserProvider = LabUserProvider.getInstance();
43+
}
44+
45+
@Test
46+
public void acquireTokenSilent_OrganizationAuthority_TokenRefreshed() throws Exception {
47+
48+
// When using common, organization, or consumer tenants, cache has no way
49+
// of determining which access token to return therefore token is always refreshed
50+
IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache();
51+
52+
IAccount account = pca.getAccounts().join().iterator().next();
53+
SilentParameters parameters = SilentParameters.builder(
54+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
55+
account).build();
56+
57+
IAuthenticationResult result = pca.acquireTokenSilently(parameters).get();
58+
59+
Assert.assertNotNull(result);
60+
Assert.assertNotNull(result.accessToken());
61+
Assert.assertNotNull(result.idToken());
62+
}
63+
64+
@Test
65+
public void acquireTokenSilent_LabAuthority_TokenNotRefreshed() throws Exception {
66+
// Access token should be returned from cache, and not using refresh token
67+
68+
LabResponse labResponse = labUserProvider.getDefaultUser(
69+
NationalCloud.AZURE_CLOUD,
70+
false);
71+
String password = labUserProvider.getUserPassword(labResponse.getUser());
72+
String labAuthority = TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId();
73+
74+
PublicClientApplication pca = new PublicClientApplication.Builder(
75+
labResponse.getAppId()).
76+
authority(labAuthority).
77+
build();
78+
79+
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
80+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
81+
labResponse.getUser().getUpn(),
82+
password.toCharArray())
83+
.build())
84+
.get();
85+
86+
IAccount account = pca.getAccounts().join().iterator().next();
87+
SilentParameters parameters = SilentParameters.builder(
88+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
89+
build();
90+
91+
IAuthenticationResult acquireSilentResult = pca.acquireTokenSilently(parameters).get();
92+
93+
Assert.assertNotNull(acquireSilentResult.accessToken());
94+
Assert.assertNotNull(result.idToken());
95+
// Check that access and id tokens are coming from cache
96+
Assert.assertEquals(result.accessToken(), acquireSilentResult.accessToken());
97+
Assert.assertEquals(result.idToken(), acquireSilentResult.idToken());
98+
}
99+
100+
@Test
101+
public void acquireTokenSilent_ForceRefresh() throws Exception {
102+
103+
LabResponse labResponse = labUserProvider.getDefaultUser(
104+
NationalCloud.AZURE_CLOUD,
105+
false);
106+
String password = labUserProvider.getUserPassword(labResponse.getUser());
107+
108+
PublicClientApplication pca = new PublicClientApplication.Builder(
109+
labResponse.getAppId()).
110+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
111+
build();
112+
113+
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
114+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
115+
labResponse.getUser().getUpn(),
116+
password.toCharArray())
117+
.build())
118+
.get();
119+
120+
IAccount account = pca.getAccounts().join().iterator().next();
121+
SilentParameters parameters = SilentParameters.builder(
122+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
123+
forceRefresh(true).
124+
build();
125+
126+
IAuthenticationResult resultAfterRefresh = pca.acquireTokenSilently(parameters).get();
127+
128+
Assert.assertNotNull(resultAfterRefresh);
129+
Assert.assertNotNull(resultAfterRefresh.accessToken());
130+
Assert.assertNotNull(resultAfterRefresh.idToken());
131+
// Check that new refresh and id tokens are being returned
132+
Assert.assertNotEquals(result.accessToken(), resultAfterRefresh.accessToken());
133+
Assert.assertNotEquals(result.idToken(), resultAfterRefresh.idToken());
134+
}
135+
136+
@Test
137+
public void acquireTokenSilent_MultipleAccountsInCache_UseCorrectAccount() throws Exception {
138+
139+
IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache();
140+
141+
// get lab user for different account
142+
LabResponse labResponse = labUserProvider.getAdfsUser(
143+
FederationProvider.ADFSV4,
144+
true,
145+
false);
146+
String password = labUserProvider.getUserPassword(labResponse.getUser());
147+
148+
// acquire token for different account
149+
pca.acquireToken(UserNamePasswordParameters.
150+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
151+
labResponse.getUser().getUpn(),
152+
password.toCharArray())
153+
.build())
154+
.get();
155+
156+
Set<IAccount> accounts = pca.getAccounts().join();
157+
IAccount account = accounts.stream().filter(
158+
x -> x.username().equalsIgnoreCase(
159+
labResponse.getUser().getUpn())).findFirst().orElse(null);
160+
161+
SilentParameters parameters = SilentParameters.builder(
162+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
163+
forceRefresh(true).
164+
build();
165+
166+
IAuthenticationResult result = pca.acquireTokenSilently(parameters).get();
167+
168+
Assert.assertNotNull(result);
169+
Assert.assertNotNull(result.accessToken());
170+
Assert.assertNotNull(result.idToken());
171+
Assert.assertEquals(result.account().username(), labResponse.getUser().getUpn());
172+
}
173+
174+
private IPublicClientApplication getPublicClientApplicationWithTokensInCache()
175+
throws Exception {
176+
LabResponse labResponse = labUserProvider.getDefaultUser(
177+
NationalCloud.AZURE_CLOUD,
178+
false);
179+
String password = labUserProvider.getUserPassword(labResponse.getUser());
180+
181+
PublicClientApplication pca = new PublicClientApplication.Builder(
182+
labResponse.getAppId()).
183+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
184+
build();
185+
186+
pca.acquireToken(UserNamePasswordParameters.
187+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
188+
labResponse.getUser().getUpn(),
189+
password.toCharArray())
190+
.build())
191+
.get();
192+
return pca;
193+
}
194+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.testng.util.Strings;
4242

4343
import java.io.UnsupportedEncodingException;
44-
import java.net.MalformedURLException;
4544
import java.net.URI;
4645
import java.net.URLEncoder;
4746
import java.util.Collections;
@@ -247,7 +246,7 @@ private IAuthenticationResult acquireTokenInteractiveAAD(
247246
try {
248247
PublicClientApplication pca = PublicClientApplication.builder(
249248
labResponse.getAppId()).
250-
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
249+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
251250
build();
252251

253252
result = pca.acquireToken(AuthorizationCodeParameters
@@ -388,7 +387,7 @@ private String buildAuthenticationCodeURL(String appId, AuthorityType authorityT
388387
String authority;
389388
String scope;
390389
if(authorityType == AuthorityType.AAD){
391-
authority = TestConstants.AUTHORITY_ORGANIZATIONS;
390+
authority = TestConstants.ORGANIZATIONS_AUTHORITY;
392391
scope = TestConstants.GRAPH_DEFAULT_SCOPE;
393392
} else {
394393
authority = TestConstants.B2C_AUTHORITY_URL;

src/integrationtest/java/com.microsoft.aad.msal4j/CachePersistenceIntegrationTest.java renamed to src/integrationtest/java/com.microsoft.aad.msal4j/CachePersistenceIT.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
import java.io.IOException;
2929
import java.net.URISyntaxException;
3030

31-
public class CachePersistenceIntegrationTest {
31+
public class CachePersistenceIT {
32+
3233
static class TokenPersistence implements ITokenCacheAccessAspect{
34+
String data;
35+
3336
TokenPersistence(String data){
3437
this.data = data;
3538
}
36-
String data;
39+
3740
@Override
3841
public void beforeCacheAccess(ITokenCacheAccessContext iTokenCacheAccessContext){
3942
iTokenCacheAccessContext.tokenCache().deserialize(data);
@@ -44,6 +47,7 @@ public void afterCacheAccess(ITokenCacheAccessContext iTokenCacheAccessContext)
4447
data = iTokenCacheAccessContext.tokenCache().serialize();
4548
}
4649
}
50+
4751
@Test
4852
public void cacheDeserializationSerializationTest() throws IOException, URISyntaxException {
4953
String dataToInitCache = TestHelper.readResource(this.getClass(), "/cache_data/serialized_cache.json");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void acquireTokenClientCredentials_ClientSecret() throws Exception{
6262
private void assertAcquireTokenCommon(String clientId, IClientCredential credential) throws Exception{
6363
ConfidentialClientApplication cca = new ConfidentialClientApplication.Builder(
6464
clientId, credential).
65-
authority(TestConstants.AUTHORITY_MICROSOFT).
65+
authority(TestConstants.MICROSOFT_AUTHORITY).
6666
build();
6767

6868
IAuthenticationResult result = cca.acquireToken(ClientCredentialParameters

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void DeviceCodeFlowTest() throws Exception {
6464

6565
PublicClientApplication pca = new PublicClientApplication.Builder(
6666
labResponse.getAppId()).
67-
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
67+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
6868
build();
6969

7070
Consumer<DeviceCode> deviceCodeConsumer = (DeviceCode deviceCode) -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void assertAcquireTokenCommon(NationalCloud cloud) throws Exception{
6363

6464
PublicClientApplication pca = new PublicClientApplication.Builder(
6565
labResponse.getAppId()).
66-
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
66+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
6767
build();
6868

6969
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setUp() throws Exception {
4747
String password = labUserProvider.getUserPassword(labResponse.getUser());
4848
pca = new PublicClientApplication.Builder(
4949
labResponse.getAppId()).
50-
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
50+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
5151
build();
5252

5353
AuthenticationResult result = (AuthenticationResult)pca.acquireToken(UserNamePasswordParameters

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public class TestConstants {
2828
public final static String GRAPH_DEFAULT_SCOPE = "https://graph.windows.net/.default";
2929
public final static String B2C_LAB_SCOPE = "https://msidlabb2c.onmicrosoft.com/msaapp/user_impersonation";
3030

31-
public final static String AUTHORITY_ORGANIZATIONS = "https://login.microsoftonline.com/organizations/";
32-
public final static String AUTHORITY_MICROSOFT = "https://login.microsoftonline.com/microsoft.onmicrosoft.com";
31+
public final static String MICROSOFT_AUTHORITY_HOST = "https://login.microsoftonline.com/";
32+
public final static String ORGANIZATIONS_AUTHORITY = MICROSOFT_AUTHORITY_HOST + "organizations/";
33+
public final static String MICROSOFT_AUTHORITY = MICROSOFT_AUTHORITY_HOST + "microsoft.onmicrosoft.com";
3334

3435
public final static String B2C_AUTHORITY = "https://msidlabb2c.b2clogin.com/tfp/msidlabb2c.onmicrosoft.com/";
3536
public final static String B2C_AUTHORITY_URL = "https://msidlabb2c.b2clogin.com/msidlabb2c.onmicrosoft.com/";

0 commit comments

Comments
 (0)