Skip to content

Commit 46c0121

Browse files
author
sgonzalezMSFT
committed
Updating tests. Adding policy to cache account entity
1 parent 0c49a6f commit 46c0121

File tree

10 files changed

+160
-50
lines changed

10 files changed

+160
-50
lines changed

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

+68-41
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void acquireTokenWithAuthorizationCode_ManagedUser(){
8686
false);
8787
labUserProvider.getUserPassword(labResponse.getUser());
8888

89-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
89+
assertAcquireTokenAAD(labResponse);
9090
}
9191

9292
@Test
@@ -97,7 +97,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2019_Federated(){
9797
true);
9898
labUserProvider.getUserPassword(labResponse.getUser());
9999

100-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
100+
assertAcquireTokenAAD(labResponse);
101101
}
102102

103103
@Test
@@ -108,7 +108,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2019_NotFederated(){
108108
true);
109109
labUserProvider.getUserPassword(labResponse.getUser());
110110

111-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
111+
assertAcquireTokenAAD(labResponse);
112112
}
113113

114114
@Test
@@ -119,7 +119,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv4_Federated(){
119119
false);
120120
labUserProvider.getUserPassword(labResponse.getUser());
121121

122-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
122+
assertAcquireTokenAAD(labResponse);
123123
}
124124

125125
@Test
@@ -130,7 +130,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv4_NotFederated(){
130130
false);
131131
labUserProvider.getUserPassword(labResponse.getUser());
132132

133-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
133+
assertAcquireTokenAAD(labResponse);
134134
}
135135

136136
@Test
@@ -140,8 +140,8 @@ public void acquireTokenWithAuthorizationCode_ADFSv3_Federated(){
140140
true,
141141
false);
142142
labUserProvider.getUserPassword(labResponse.getUser());
143+
assertAcquireTokenAAD(labResponse);
143144

144-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
145145
}
146146

147147
@Test
@@ -152,7 +152,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv3_NotFederated(){
152152
false);
153153
labUserProvider.getUserPassword(labResponse.getUser());
154154

155-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
155+
assertAcquireTokenAAD(labResponse);
156156
}
157157

158158
@Test
@@ -163,7 +163,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2_Federated(){
163163
false);
164164
labUserProvider.getUserPassword(labResponse.getUser());
165165

166-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
166+
assertAcquireTokenAAD(labResponse);
167167
}
168168

169169
@Test
@@ -174,7 +174,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2_NotFederated(){
174174
false);
175175
labUserProvider.getUserPassword(labResponse.getUser());
176176

177-
assertAcquireTokenCommon(labResponse, AuthorityType.AAD);
177+
assertAcquireTokenAAD(labResponse);
178178
}
179179

180180
@Test
@@ -187,7 +187,7 @@ public void acquireTokenWithAuthorizationCode_B2C_Local(){
187187
String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852";
188188
labResponse.setAppId(b2CAppId);
189189

190-
assertAcquireTokenCommon(labResponse, AuthorityType.B2C);
190+
assertAcquireTokenB2C(labResponse);
191191
}
192192

193193
@Test
@@ -200,26 +200,39 @@ public void acquireTokenWithAuthorizationCode_B2C_Google(){
200200
String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852";
201201
labResponse.setAppId(b2CAppId);
202202

203-
assertAcquireTokenCommon(labResponse, AuthorityType.B2C);
203+
assertAcquireTokenB2C(labResponse);
204204
}
205205

206-
@Test
207-
public void acquireTokenWithAuthorizationCode_B2C_Facebook(){
208-
LabResponse labResponse = labUserProvider.getB2cUser(
209-
B2CIdentityProvider.FACEBOOK,
210-
false);
211-
labUserProvider.getUserPassword(labResponse.getUser());
206+
// TODO uncomment when lab fixes facebook test account
207+
// @Test
208+
// public void acquireTokenWithAuthorizationCode_B2C_Facebook(){
209+
// LabResponse labResponse = labUserProvider.getB2cUser(
210+
// B2CIdentityProvider.FACEBOOK,
211+
// false);
212+
// labUserProvider.getUserPassword(labResponse.getUser());
213+
//
214+
// String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852";
215+
// labResponse.setAppId(b2CAppId);
216+
//
217+
// assertAcquireTokenB2C(labResponse);
218+
// }
212219

213-
String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852";
214-
labResponse.setAppId(b2CAppId);
215220

216-
assertAcquireTokenCommon(labResponse, AuthorityType.B2C);
217-
}
221+
private void assertAcquireTokenAAD(LabResponse labResponse){
222+
String authCode = acquireAuthorizationCodeAutomated(labResponse, AuthorityType.AAD);
223+
AuthenticationResult result = acquireTokenInteractiveAAD(labResponse, authCode);
218224

225+
Assert.assertNotNull(result);
226+
Assert.assertNotNull(result.accessToken());
227+
Assert.assertNotNull(result.refreshToken());
228+
Assert.assertNotNull(result.idToken());
229+
// TODO AuthenticationResult should have an getAccountInfo API
230+
// Assert.assertEquals(labResponse.getUser().getUpn(), result.getAccountInfo().getUsername());
231+
}
219232

220-
private void assertAcquireTokenCommon(LabResponse labResponse, AuthorityType authorityType){
221-
String authCode = acquireAuthorizationCodeAutomated(labResponse, authorityType);
222-
AuthenticationResult result = acquireTokenInteractive(labResponse, authorityType, authCode);
233+
private void assertAcquireTokenB2C(LabResponse labResponse){
234+
String authCode = acquireAuthorizationCodeAutomated(labResponse, AuthorityType.B2C);
235+
AuthenticationResult result = acquireTokenInteractiveB2C(labResponse, authCode);
223236

224237
Assert.assertNotNull(result);
225238
Assert.assertNotNull(result.accessToken());
@@ -229,14 +242,16 @@ private void assertAcquireTokenCommon(LabResponse labResponse, AuthorityType aut
229242
// Assert.assertEquals(labResponse.getUser().getUpn(), result.getAccountInfo().getUsername());
230243
}
231244

232-
private AuthenticationResult acquireTokenInteractive(
245+
private AuthenticationResult acquireTokenInteractiveAAD(
233246
LabResponse labResponse,
234-
AuthorityType authorityType,
235247
String authCode){
236248

237249
AuthenticationResult result;
238250
try {
239-
PublicClientApplication pca = createPublicClientApplication(labResponse, authorityType);
251+
PublicClientApplication pca = PublicClientApplication.builder(
252+
labResponse.getAppId()).
253+
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
254+
build();
240255

241256
result = pca.acquireToken(AuthorizationCodeParameters
242257
.builder(authCode,
@@ -252,22 +267,31 @@ private AuthenticationResult acquireTokenInteractive(
252267
return result;
253268
}
254269

255-
private PublicClientApplication createPublicClientApplication(
256-
LabResponse labResponse,
257-
AuthorityType authorityType) throws MalformedURLException {
258-
if(authorityType == AuthorityType.AAD){
259-
return new PublicClientApplication.Builder(
260-
labResponse.getAppId()).
261-
authority(TestConstants.AUTHORITY_ORGANIZATIONS).
262-
build();
263-
} else {
264-
return new PublicClientApplication.Builder(
265-
labResponse.getAppId()).
266-
b2cAuthority(TestConstants.B2C_AUTHORITY_SIGN_IN).
267-
build();
270+
private AuthenticationResult acquireTokenInteractiveB2C(LabResponse labResponse,
271+
String authCode) {
272+
AuthenticationResult result;
273+
try{
274+
IClientCredential credential = ClientCredentialFactory.create("=]Y)_A7LX`]6\"]_PoD!)Lo24");
275+
ConfidentialClientApplication cca = ConfidentialClientApplication.builder(
276+
labResponse.getAppId(),
277+
credential)
278+
.b2cAuthority(TestConstants.B2C_AUTHORITY_SIGN_IN)
279+
.build();
280+
281+
result = cca.acquireToken(AuthorizationCodeParameters.builder(
282+
authCode,
283+
new URI(TestConstants.LOCALHOST + tcpListener.getPort()))
284+
.scopes(Collections.singleton(TestConstants.B2C_LAB_SCOPE))
285+
.build())
286+
.get();
287+
} catch (Exception e){
288+
LOG.error("Error acquiring token with authCode: " + e.getMessage());
289+
throw new RuntimeException("Error acquiring token with authCode: " + e.getMessage());
268290
}
291+
return result;
269292
}
270293

294+
271295
private String acquireAuthorizationCodeAutomated(
272296
LabResponse labUserData,
273297
AuthorityType authorityType){
@@ -364,18 +388,21 @@ private String buildAuthenticationCodeURL(String appId, AuthorityType authorityT
364388
int portNumber = tcpListener.getPort();
365389

366390
String authority;
391+
String scope;
367392
if(authorityType == AuthorityType.AAD){
368393
authority = TestConstants.AUTHORITY_ORGANIZATIONS;
394+
scope = TestConstants.GRAPH_DEFAULT_SCOPE;
369395
} else {
370396
authority = TestConstants.B2C_AUTHORITY_URL;
397+
scope = TestConstants.B2C_LAB_SCOPE;
371398
}
372399

373400
redirectUrl = authority + "oauth2/v2.0/authorize?" +
374401
"response_type=code" +
375402
"&response_mode=query" +
376403
"&client_id=" + appId +
377404
"&redirect_uri=" + URLEncoder.encode(TestConstants.LOCALHOST + portNumber, "UTF-8") +
378-
"&scope=" + URLEncoder.encode("openid offline_access profile " + TestConstants.GRAPH_DEFAULT_SCOPE, "UTF-8");
405+
"&scope=" + URLEncoder.encode("openid offline_access profile " + scope, "UTF-8");
379406

380407
if(authorityType == AuthorityType.B2C){
381408
redirectUrl = redirectUrl + "&p=" + TestConstants.B2C_SIGN_IN_POLICY;

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

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class TestConstants {
2727
public final static String KEYVAULT_DEFAULT_SCOPE = "https://vault.azure.net/.default";
2828
public final static String GRAPH_DEFAULT_SCOPE = "https://graph.windows.net/.default";
29+
public final static String B2C_LAB_SCOPE = "https://msidlabb2c.onmicrosoft.com/msaapp/user_impersonation";
2930

3031
public final static String AUTHORITY_ORGANIZATIONS = "https://login.microsoftonline.com/organizations/";
3132
public final static String AUTHORITY_MICROSOFT = "https://login.microsoftonline.com/microsoft.onmicrosoft.com";

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ String getKey() {
7878
return String.join(Constants.CACHE_KEY_SEPARATOR, keyParts).toLowerCase();
7979
}
8080

81-
static Account create(String clientInfoStr, String environment, IdToken idToken) {
81+
static Account create(String clientInfoStr, String environment, IdToken idToken, String policy) {
8282

8383
Account account = new Account();
8484
account.authorityType(MSSTS_ACCOUNT_TYPE);
8585
account.clientInfoStr = clientInfoStr;
86-
account.homeAccountId(account.clientInfo().toAccountIdentifier());
86+
account.homeAccountId(policy != null ?
87+
account.clientInfo().toAccountIdentifier() + Constants.CACHE_KEY_SEPARATOR + policy :
88+
account.clientInfo().toAccountIdentifier());
8789
account.environment(environment);
8890

8991
if (idToken != null) {
@@ -97,4 +99,8 @@ static Account create(String clientInfoStr, String environment, IdToken idToken)
9799

98100
return account;
99101
}
102+
103+
static Account create(String clientInfoStr, String environment, IdToken idToken){
104+
return create(clientInfoStr, environment, idToken, null);
105+
}
100106
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class B2CAuthority extends Authority{
1212

1313
final String B2CTokenEndpointFormat = "https://%s/{tenant}" + TOKEN_ENDPOINT + "?p={policy}";
14+
String policy;
1415

1516
B2CAuthority(final URL authorityUrl){
1617
super(authorityUrl);
@@ -26,7 +27,7 @@ private void setAuthorityProperties() {
2627
"B2C 'authority' Uri should have at least 3 segments in the path " +
2728
"(i.e. https://<host>/tfp/<tenant>/<policy>/...)");
2829
}
29-
String policy = segments[2];
30+
policy = segments[2];
3031

3132
final String b2cAuthorityFormat = "https://%s/%s/%s/%s/";
3233
this.authority = String.format(

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ abstract static class Builder<T extends Builder<T>> {
304304

305305
// Optional parameters - initialized to default values
306306
private String authority = DEFAULT_AUTHORITY;
307-
private Authority authenticationAuthority;
307+
private Authority authenticationAuthority = createDefaultAADAuthority();
308308
private boolean validateAuthority = true;
309309
private String correlationId = UUID.randomUUID().toString();
310310
private boolean logPii = false;
@@ -445,6 +445,16 @@ public T setTokenCacheAccessAspect(ITokenCacheAccessAspect val) {
445445
return self();
446446
}
447447

448+
private static Authority createDefaultAADAuthority() {
449+
Authority authority;
450+
try {
451+
authority = new AADAuthority(new URL(DEFAULT_AUTHORITY));
452+
} catch(Exception e){
453+
throw new AuthenticationException(e);
454+
}
455+
return authority;
456+
}
457+
448458
abstract ClientApplicationBase build();
449459
}
450460

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,23 @@ AuthenticationResult executeOauthRequestAndProcessResponse()
106106
IdToken idToken = JsonHelper.convertJsonToObject(idTokenJson, IdToken.class);
107107

108108
if (!StringHelper.isBlank(response.getClientInfo())) {
109-
account = Account.create(response.getClientInfo(), url.getHost(), idToken);
109+
110+
AuthorityType type = msalRequest.application().authenticationAuthority.authorityType;
111+
if(type == AuthorityType.B2C){
112+
113+
B2CAuthority authority = (B2CAuthority) msalRequest.application().authenticationAuthority;
114+
115+
account = Account.create(
116+
response.getClientInfo(),
117+
url.getHost(),
118+
idToken,
119+
authority.policy);
120+
} else {
121+
account = Account.create(
122+
response.getClientInfo(),
123+
url.getHost(),
124+
idToken);
125+
}
110126
}
111127
}
112128
long currTimestampSec = new Date().getTime() / 1000;
+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1-
package PACKAGE_NAME;
1+
2+
import com.microsoft.aad.msal4j.AuthenticationResult;
3+
import com.microsoft.aad.msal4j.PublicClientApplication;
4+
import com.microsoft.aad.msal4j.UserNamePasswordParameters;
5+
6+
7+
import java.util.Collections;
8+
import java.util.concurrent.CompletableFuture;
29

310
public class B2CFlow {
4-
}
11+
12+
public static void main(String args[]) throws Exception {
13+
getAccessTokenFromUserCredentials();
14+
}
15+
16+
private static void getAccessTokenFromUserCredentials() throws Exception {
17+
18+
PublicClientApplication app = PublicClientApplication.builder(TestData.PUBLIC_CLIENT_ID)
19+
.b2cAuthority(TestData.B2C_AUTHORITY)
20+
.build();
21+
22+
CompletableFuture<AuthenticationResult> future = app.acquireToken(
23+
UserNamePasswordParameters.builder(
24+
Collections.singleton(TestData.LAB_DEFAULT_B2C_SCOPE),
25+
TestData.USER_NAME,
26+
TestData.USER_PASSWORD.toCharArray()).build());
27+
28+
future.handle((res, ex) -> {
29+
if(ex != null) {
30+
System.out.println("Oops! We have an exception - " + ex.getMessage());
31+
return "Unknown!";
32+
}
33+
System.out.println("Returned ok - " + res);
34+
35+
System.out.println("Access Token - " + res.accessToken());
36+
System.out.println("Refresh Token - " + res.refreshToken());
37+
System.out.println("ID Token - " + res.idToken());
38+
return res;
39+
}).join();
40+
}
41+
}

0 commit comments

Comments
 (0)