25
25
26
26
import infrastructure .SeleniumExtensions ;
27
27
import infrastructure .TcpListener ;
28
+ import labapi .B2CIdentityProvider ;
28
29
import labapi .FederationProvider ;
29
30
import labapi .LabResponse ;
30
31
import labapi .LabUserProvider ;
40
41
import org .testng .util .Strings ;
41
42
42
43
import java .io .UnsupportedEncodingException ;
44
+ import java .net .MalformedURLException ;
43
45
import java .net .URI ;
44
46
import java .net .URLEncoder ;
45
47
import java .util .Collections ;
@@ -65,11 +67,11 @@ public void setUpLapUserProvider() {
65
67
66
68
@ AfterMethod
67
69
public void cleanUp (){
68
- seleniumDriver .quit ();
69
- if (AuthorizationCodeQueue != null ){
70
- AuthorizationCodeQueue .clear ();
71
- }
72
- tcpListener .close ();
70
+ seleniumDriver .quit ();
71
+ if (AuthorizationCodeQueue != null ){
72
+ AuthorizationCodeQueue .clear ();
73
+ }
74
+ tcpListener .close ();
73
75
}
74
76
75
77
@ BeforeMethod
@@ -84,7 +86,7 @@ public void acquireTokenWithAuthorizationCode_ManagedUser(){
84
86
false );
85
87
labUserProvider .getUserPassword (labResponse .getUser ());
86
88
87
- assertAcquireTokenCommon (labResponse );
89
+ assertAcquireTokenAAD (labResponse );
88
90
}
89
91
90
92
@ Test
@@ -95,9 +97,9 @@ public void acquireTokenWithAuthorizationCode_ADFSv2019_Federated(){
95
97
true );
96
98
labUserProvider .getUserPassword (labResponse .getUser ());
97
99
98
- assertAcquireTokenCommon (labResponse );
100
+ assertAcquireTokenAAD (labResponse );
99
101
}
100
-
102
+
101
103
@ Test
102
104
public void acquireTokenWithAuthorizationCode_ADFSv2019_NotFederated (){
103
105
LabResponse labResponse = labUserProvider .getAdfsUser (
@@ -106,7 +108,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2019_NotFederated(){
106
108
true );
107
109
labUserProvider .getUserPassword (labResponse .getUser ());
108
110
109
- assertAcquireTokenCommon (labResponse );
111
+ assertAcquireTokenAAD (labResponse );
110
112
}
111
113
112
114
@ Test
@@ -117,7 +119,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv4_Federated(){
117
119
false );
118
120
labUserProvider .getUserPassword (labResponse .getUser ());
119
121
120
- assertAcquireTokenCommon (labResponse );
122
+ assertAcquireTokenAAD (labResponse );
121
123
}
122
124
123
125
@ Test
@@ -128,7 +130,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv4_NotFederated(){
128
130
false );
129
131
labUserProvider .getUserPassword (labResponse .getUser ());
130
132
131
- assertAcquireTokenCommon (labResponse );
133
+ assertAcquireTokenAAD (labResponse );
132
134
}
133
135
134
136
@ Test
@@ -138,8 +140,8 @@ public void acquireTokenWithAuthorizationCode_ADFSv3_Federated(){
138
140
true ,
139
141
false );
140
142
labUserProvider .getUserPassword (labResponse .getUser ());
143
+ assertAcquireTokenAAD (labResponse );
141
144
142
- assertAcquireTokenCommon (labResponse );
143
145
}
144
146
145
147
@ Test
@@ -150,7 +152,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv3_NotFederated(){
150
152
false );
151
153
labUserProvider .getUserPassword (labResponse .getUser ());
152
154
153
- assertAcquireTokenCommon (labResponse );
155
+ assertAcquireTokenAAD (labResponse );
154
156
}
155
157
156
158
@ Test
@@ -161,7 +163,7 @@ public void acquireTokenWithAuthorizationCode_ADFSv2_Federated(){
161
163
false );
162
164
labUserProvider .getUserPassword (labResponse .getUser ());
163
165
164
- assertAcquireTokenCommon (labResponse );
166
+ assertAcquireTokenAAD (labResponse );
165
167
}
166
168
167
169
@ Test
@@ -172,12 +174,53 @@ public void acquireTokenWithAuthorizationCode_ADFSv2_NotFederated(){
172
174
false );
173
175
labUserProvider .getUserPassword (labResponse .getUser ());
174
176
175
- assertAcquireTokenCommon (labResponse );
177
+ assertAcquireTokenAAD (labResponse );
176
178
}
177
179
178
- private void assertAcquireTokenCommon (LabResponse labResponse ){
179
- String authCode = acquireAuthorizationCodeAutomated (labResponse );
180
- AuthenticationResult result = acquireTokenInteractive (labResponse , authCode );
180
+ @ Test
181
+ public void acquireTokenWithAuthorizationCode_B2C_Local (){
182
+ LabResponse labResponse = labUserProvider .getB2cUser (
183
+ B2CIdentityProvider .LOCAL ,
184
+ false );
185
+ labUserProvider .getUserPassword (labResponse .getUser ());
186
+
187
+ String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852" ;
188
+ labResponse .setAppId (b2CAppId );
189
+
190
+ assertAcquireTokenB2C (labResponse );
191
+ }
192
+
193
+ @ Test
194
+ public void acquireTokenWithAuthorizationCode_B2C_Google (){
195
+ LabResponse labResponse = labUserProvider .getB2cUser (
196
+ B2CIdentityProvider .GOOGLE ,
197
+ false );
198
+ labUserProvider .getUserPassword (labResponse .getUser ());
199
+
200
+ String b2CAppId = "b876a048-55a5-4fc5-9403-f5d90cb1c852" ;
201
+ labResponse .setAppId (b2CAppId );
202
+
203
+ assertAcquireTokenB2C (labResponse );
204
+ }
205
+
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
+ // }
219
+
220
+
221
+ private void assertAcquireTokenAAD (LabResponse labResponse ){
222
+ String authCode = acquireAuthorizationCodeAutomated (labResponse , AuthorityType .AAD );
223
+ AuthenticationResult result = acquireTokenInteractiveAAD (labResponse , authCode );
181
224
182
225
Assert .assertNotNull (result );
183
226
Assert .assertNotNull (result .accessToken ());
@@ -187,11 +230,25 @@ private void assertAcquireTokenCommon(LabResponse labResponse){
187
230
// Assert.assertEquals(labResponse.getUser().getUpn(), result.getAccountInfo().getUsername());
188
231
}
189
232
190
- private AuthenticationResult acquireTokenInteractive (LabResponse labResponse ,
191
- String authCode ){
233
+ private void assertAcquireTokenB2C (LabResponse labResponse ){
234
+ String authCode = acquireAuthorizationCodeAutomated (labResponse , AuthorityType .B2C );
235
+ AuthenticationResult result = acquireTokenInteractiveB2C (labResponse , authCode );
236
+
237
+ Assert .assertNotNull (result );
238
+ Assert .assertNotNull (result .accessToken ());
239
+ Assert .assertNotNull (result .refreshToken ());
240
+ Assert .assertNotNull (result .idToken ());
241
+ // TODO AuthenticationResult should have an getAccountInfo API
242
+ // Assert.assertEquals(labResponse.getUser().getUpn(), result.getAccountInfo().getUsername());
243
+ }
244
+
245
+ private AuthenticationResult acquireTokenInteractiveAAD (
246
+ LabResponse labResponse ,
247
+ String authCode ){
248
+
192
249
AuthenticationResult result ;
193
250
try {
194
- PublicClientApplication pca = new PublicClientApplication .Builder (
251
+ PublicClientApplication pca = PublicClientApplication .builder (
195
252
labResponse .getAppId ()).
196
253
authority (TestConstants .AUTHORITY_ORGANIZATIONS ).
197
254
build ();
@@ -210,7 +267,34 @@ private AuthenticationResult acquireTokenInteractive(LabResponse labResponse,
210
267
return result ;
211
268
}
212
269
213
- private String acquireAuthorizationCodeAutomated (LabResponse labUserData ){
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 ());
290
+ }
291
+ return result ;
292
+ }
293
+
294
+
295
+ private String acquireAuthorizationCodeAutomated (
296
+ LabResponse labUserData ,
297
+ AuthorityType authorityType ){
214
298
BlockingQueue <Boolean > tcpStartUpNotificationQueue = new LinkedBlockingQueue <>();
215
299
startTcpListener (tcpStartUpNotificationQueue );
216
300
@@ -222,7 +306,7 @@ private String acquireAuthorizationCodeAutomated(LabResponse labUserData){
222
306
if (tcpListenerStarted == null || !tcpListenerStarted ){
223
307
throw new RuntimeException ("Could not start TCP listener" );
224
308
}
225
- runSeleniumAutomatedLogin (labUserData );
309
+ runSeleniumAutomatedLogin (labUserData , authorityType );
226
310
authServerResponse = getResponseFromTcpListener ();
227
311
} catch (Exception e ){
228
312
if (!Strings .isNullOrEmpty (
@@ -232,14 +316,28 @@ private String acquireAuthorizationCodeAutomated(LabResponse labUserData){
232
316
LOG .error ("Error running automated selenium login: " + e .getMessage ());
233
317
throw new RuntimeException ("Error running automated selenium login: " + e .getMessage ());
234
318
}
235
- return parseServerResponse (authServerResponse );
319
+ return parseServerResponse (authServerResponse , authorityType );
236
320
}
237
321
238
- private void runSeleniumAutomatedLogin (LabResponse labUserData ) throws
239
- UnsupportedEncodingException {
240
- String url = buildAuthenticationCodeURL (labUserData .getAppId ());
322
+ private void runSeleniumAutomatedLogin (LabResponse labUserData , AuthorityType authorityType )
323
+ throws UnsupportedEncodingException {
324
+ String url = buildAuthenticationCodeURL (labUserData .getAppId (), authorityType );
241
325
seleniumDriver .navigate ().to (url );
242
- SeleniumExtensions .performLogin (seleniumDriver , labUserData .getUser ());
326
+ if (authorityType == AuthorityType .B2C ){
327
+ switch (labUserData .getUser ().getB2CIdentityProvider ()){
328
+ case LOCAL :
329
+ SeleniumExtensions .performLocalLogin (seleniumDriver , labUserData .getUser ());
330
+ break ;
331
+ case GOOGLE :
332
+ SeleniumExtensions .performGoogleLogin (seleniumDriver , labUserData .getUser ());
333
+ break ;
334
+ case FACEBOOK :
335
+ SeleniumExtensions .performFacebookLogin (seleniumDriver , labUserData .getUser ());
336
+ break ;
337
+ }
338
+ } else {
339
+ SeleniumExtensions .performADLogin (seleniumDriver , labUserData .getUser ());
340
+ }
243
341
}
244
342
245
343
private void startTcpListener (BlockingQueue <Boolean > tcpStartUpNotifierQueue ){
@@ -264,9 +362,15 @@ private String getResponseFromTcpListener(){
264
362
return response ;
265
363
}
266
364
267
- private String parseServerResponse (String serverResponse ){
365
+ private String parseServerResponse (String serverResponse , AuthorityType authorityType ){
268
366
// Response will be a GET request with query parameter ?code=authCode
269
- String regexp = "code=(.*)&" ;
367
+ String regexp ;
368
+ if (authorityType == AuthorityType .B2C ){
369
+ regexp = "(?<=code=)(?:(?! HTTP).)*" ;
370
+ } else {
371
+ regexp = "(?<=code=)(?:(?!&).)*" ;
372
+ }
373
+
270
374
Pattern pattern = Pattern .compile (regexp );
271
375
Matcher matcher = pattern .matcher (serverResponse );
272
376
@@ -275,18 +379,34 @@ private String parseServerResponse(String serverResponse){
275
379
throw new IllegalStateException ("No authorization code in server response: " +
276
380
serverResponse );
277
381
}
278
- return matcher .group (1 );
382
+ return matcher .group (0 );
279
383
}
280
384
281
- private String buildAuthenticationCodeURL (String appId ) throws UnsupportedEncodingException {
385
+ private String buildAuthenticationCodeURL (String appId , AuthorityType authorityType )
386
+ throws UnsupportedEncodingException {
282
387
String redirectUrl ;
283
388
int portNumber = tcpListener .getPort ();
284
- redirectUrl = TestConstants .AUTHORITY_ORGANIZATIONS + "oauth2/v2.0/authorize?" +
285
- "response_type=code&" +
286
- "response_mode=query&" +
389
+
390
+ String authority ;
391
+ String scope ;
392
+ if (authorityType == AuthorityType .AAD ){
393
+ authority = TestConstants .AUTHORITY_ORGANIZATIONS ;
394
+ scope = TestConstants .GRAPH_DEFAULT_SCOPE ;
395
+ } else {
396
+ authority = TestConstants .B2C_AUTHORITY_URL ;
397
+ scope = TestConstants .B2C_LAB_SCOPE ;
398
+ }
399
+
400
+ redirectUrl = authority + "oauth2/v2.0/authorize?" +
401
+ "response_type=code" +
402
+ "&response_mode=query" +
287
403
"&client_id=" + appId +
288
404
"&redirect_uri=" + URLEncoder .encode (TestConstants .LOCALHOST + portNumber , "UTF-8" ) +
289
- "&scope=" + URLEncoder .encode ("openid offline_access profile " + TestConstants .GRAPH_DEFAULT_SCOPE , "UTF-8" );
405
+ "&scope=" + URLEncoder .encode ("openid offline_access profile " + scope , "UTF-8" );
406
+
407
+ if (authorityType == AuthorityType .B2C ){
408
+ redirectUrl = redirectUrl + "&p=" + TestConstants .B2C_SIGN_IN_POLICY ;
409
+ }
290
410
291
411
return redirectUrl ;
292
412
}
0 commit comments