Skip to content

Commit 44094dc

Browse files
author
sgonzalezMSFT
committed
Add acquire token silent and remove accounts tests. Fix remove accounts
bug
1 parent 074de34 commit 44094dc

File tree

15 files changed

+408
-20
lines changed

15 files changed

+408
-20
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package com.microsoft.aad.msal4j;
2+
3+
import labapi.FederationProvider;
4+
import labapi.LabResponse;
5+
import labapi.LabUserProvider;
6+
import labapi.NationalCloud;
7+
import org.testng.Assert;
8+
import org.testng.annotations.BeforeClass;
9+
import org.testng.annotations.Test;
10+
11+
import java.util.Collections;
12+
import java.util.Set;
13+
14+
public class AcquireTokenSilentIT {
15+
private LabUserProvider labUserProvider;
16+
17+
@BeforeClass
18+
public void setUp() {
19+
labUserProvider = LabUserProvider.getInstance();
20+
}
21+
22+
@Test
23+
public void acquireTokenSilent_OrganizationAuthority_TokenRefreshed() throws Exception {
24+
25+
// When using common, organization, or consumer tenants, cache has no way
26+
// of determining which access token to return therefore token is always refreshed
27+
IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache();
28+
29+
IAccount account = pca.getAccounts().join().iterator().next();
30+
SilentParameters parameters = SilentParameters.builder(
31+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
32+
account).build();
33+
34+
IAuthenticationResult result = pca.acquireTokenSilently(parameters).get();
35+
36+
Assert.assertNotNull(result);
37+
Assert.assertNotNull(result.accessToken());
38+
Assert.assertNotNull(result.idToken());
39+
}
40+
41+
@Test
42+
public void acquireTokenSilent_LabAuthority_TokenNotRefreshed() throws Exception {
43+
// Access token should be returned from cache, and not using refresh token
44+
45+
LabResponse labResponse = labUserProvider.getDefaultUser(
46+
NationalCloud.AZURE_CLOUD,
47+
false);
48+
String password = labUserProvider.getUserPassword(labResponse.getUser());
49+
String labAuthority = TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId();
50+
51+
PublicClientApplication pca = new PublicClientApplication.Builder(
52+
labResponse.getAppId()).
53+
authority(labAuthority).
54+
build();
55+
56+
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
57+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
58+
labResponse.getUser().getUpn(),
59+
password.toCharArray())
60+
.build())
61+
.get();
62+
63+
IAccount account = pca.getAccounts().join().iterator().next();
64+
SilentParameters parameters = SilentParameters.builder(
65+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
66+
build();
67+
68+
IAuthenticationResult acquireSilentResult = pca.acquireTokenSilently(parameters).get();
69+
70+
Assert.assertNotNull(acquireSilentResult.accessToken());
71+
Assert.assertNotNull(result.idToken());
72+
// Check that access and id tokens are coming from cache
73+
Assert.assertEquals(result.accessToken(), acquireSilentResult.accessToken());
74+
Assert.assertEquals(result.idToken(), acquireSilentResult.idToken());
75+
}
76+
77+
@Test
78+
public void acquireTokenSilent_ForceRefresh() throws Exception {
79+
80+
LabResponse labResponse = labUserProvider.getDefaultUser(
81+
NationalCloud.AZURE_CLOUD,
82+
false);
83+
String password = labUserProvider.getUserPassword(labResponse.getUser());
84+
85+
PublicClientApplication pca = new PublicClientApplication.Builder(
86+
labResponse.getAppId()).
87+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
88+
build();
89+
90+
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
91+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
92+
labResponse.getUser().getUpn(),
93+
password.toCharArray())
94+
.build())
95+
.get();
96+
97+
IAccount account = pca.getAccounts().join().iterator().next();
98+
SilentParameters parameters = SilentParameters.builder(
99+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
100+
forceRefresh(true).
101+
build();
102+
103+
IAuthenticationResult resultAfterRefresh = pca.acquireTokenSilently(parameters).get();
104+
105+
Assert.assertNotNull(resultAfterRefresh);
106+
Assert.assertNotNull(resultAfterRefresh.accessToken());
107+
Assert.assertNotNull(resultAfterRefresh.idToken());
108+
// Check that new refresh and id tokens are being returned
109+
Assert.assertNotEquals(result.accessToken(), resultAfterRefresh.accessToken());
110+
Assert.assertNotEquals(result.idToken(), resultAfterRefresh.idToken());
111+
}
112+
113+
@Test
114+
public void acquireTokenSilent_MultipleAccountsInCache_UseCorrectAccount() throws Exception {
115+
116+
IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache();
117+
118+
// get lab user for different account
119+
LabResponse labResponse = labUserProvider.getAdfsUser(
120+
FederationProvider.ADFSV4,
121+
true,
122+
false);
123+
String password = labUserProvider.getUserPassword(labResponse.getUser());
124+
125+
// acquire token for different account
126+
pca.acquireToken(UserNamePasswordParameters.
127+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
128+
labResponse.getUser().getUpn(),
129+
password.toCharArray())
130+
.build())
131+
.get();
132+
133+
Set<IAccount> accounts = pca.getAccounts().join();
134+
IAccount account = accounts.stream().filter(
135+
x -> x.username().equalsIgnoreCase(
136+
labResponse.getUser().getUpn())).findFirst().orElse(null);
137+
138+
SilentParameters parameters = SilentParameters.builder(
139+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
140+
forceRefresh(true).
141+
build();
142+
143+
IAuthenticationResult result = pca.acquireTokenSilently(parameters).get();
144+
145+
Assert.assertNotNull(result);
146+
Assert.assertNotNull(result.accessToken());
147+
Assert.assertNotNull(result.idToken());
148+
Assert.assertEquals(result.account().username(), labResponse.getUser().getUpn());
149+
}
150+
151+
private IPublicClientApplication getPublicClientApplicationWithTokensInCache()
152+
throws Exception {
153+
LabResponse labResponse = labUserProvider.getDefaultUser(
154+
NationalCloud.AZURE_CLOUD,
155+
false);
156+
String password = labUserProvider.getUserPassword(labResponse.getUser());
157+
158+
PublicClientApplication pca = new PublicClientApplication.Builder(
159+
labResponse.getAppId()).
160+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
161+
build();
162+
163+
pca.acquireToken(UserNamePasswordParameters.
164+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
165+
labResponse.getUser().getUpn(),
166+
password.toCharArray())
167+
.build())
168+
.get();
169+
return pca;
170+
}
171+
}

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;
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)