5
5
6
6
package org .wildfly .test .integration .elytron .oidc .client ;
7
7
8
+ import static org .jboss .as .controller .descriptions .ModelDescriptionConstants .SYSTEM_PROPERTY ;
9
+ import static org .jboss .as .controller .descriptions .ModelDescriptionConstants .VALUE ;
8
10
import static org .junit .Assert .assertEquals ;
9
11
import static org .junit .Assert .assertNotNull ;
10
12
import static org .junit .Assert .assertTrue ;
11
13
import static org .junit .Assume .assumeTrue ;
14
+ import static org .wildfly .security .http .oidc .Oidc .OIDC_SCOPE ;
12
15
import static org .wildfly .test .integration .elytron .oidc .client .KeycloakConfiguration .ALLOWED_ORIGIN ;
13
16
14
17
import java .io .IOException ;
39
42
import org .jboss .arquillian .container .test .api .OperateOnDeployment ;
40
43
import org .jboss .as .arquillian .api .ServerSetupTask ;
41
44
import org .jboss .as .arquillian .container .ManagementClient ;
45
+ import org .jboss .as .controller .PathAddress ;
46
+ import org .jboss .as .controller .operations .common .Util ;
42
47
import org .jboss .as .test .http .util .TestHttpClientUtils ;
48
+ import org .jboss .as .test .integration .management .ManagementOperations ;
43
49
import org .jboss .as .test .integration .security .common .servlets .SimpleSecuredServlet ;
44
50
import org .jboss .as .test .integration .security .common .servlets .SimpleServlet ;
45
51
import org .jboss .as .test .shared .TestSuiteEnvironment ;
46
52
import org .jboss .as .test .shared .util .AssumeTestGroupUtil ;
53
+ import org .jboss .as .version .Stability ;
54
+ import org .jboss .dmr .ModelNode ;
47
55
import org .jsoup .Jsoup ;
48
56
import org .jsoup .nodes .Document ;
49
57
import org .jsoup .nodes .Element ;
52
60
import org .keycloak .representations .idm .RealmRepresentation ;
53
61
import org .wildfly .common .iteration .CodePointIterator ;
54
62
import org .wildfly .security .jose .util .JsonSerialization ;
63
+ import org .wildfly .test .integration .elytron .oidc .client .subsystem .SimpleServletWithScope ;
55
64
56
65
import io .restassured .RestAssured ;
57
66
@@ -88,6 +97,16 @@ public abstract class OidcBaseTest {
88
97
static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method" ;
89
98
static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers" ;
90
99
public static final String CORS_CLIENT = "CorsClient" ;
100
+ public static final String OPENID_SCOPE_APP = "OpenIDScopeApp" ;
101
+ public static final String INVALID_SCOPE_APP = "InvalidScopeApp" ;
102
+ public static final String SINGLE_SCOPE_APP = "SingleScopeApp" ;
103
+ public static final String MULTIPLE_SCOPE_APP = "MultipleScopeApp" ;
104
+
105
+ private final Stability desiredStability ;
106
+
107
+ public OidcBaseTest (Stability desiredStability ) {
108
+ this .desiredStability = desiredStability ;
109
+ }
91
110
92
111
private enum BearerAuthType {
93
112
BEARER ,
@@ -320,6 +339,46 @@ public void testCorsRequestWithoutEnableCors() throws Exception {
320
339
SimpleServlet .RESPONSE_BODY , null , CORS_CLIENT , CLIENT_SECRET , ALLOWED_ORIGIN , false );
321
340
}
322
341
342
+ /**
343
+ * Tests that use different scope values to request access to claims values.
344
+ */
345
+
346
+ @ Test
347
+ @ OperateOnDeployment (OPENID_SCOPE_APP )
348
+ public void testOpenIDScope () throws Exception {
349
+ String expectedScope = OIDC_SCOPE ;
350
+ loginToApp (KeycloakConfiguration .ALICE , KeycloakConfiguration .ALICE_PASSWORD , HttpURLConnection .HTTP_OK , SimpleServlet .RESPONSE_BODY , true ,
351
+ new URL ("http" , TestSuiteEnvironment .getHttpAddress (), TestSuiteEnvironment .getHttpPort (),
352
+ "/" + OPENID_SCOPE_APP + SimpleServletWithScope .SERVLET_PATH ).toURI (), expectedScope , false );
353
+ }
354
+
355
+ @ Test
356
+ @ OperateOnDeployment (SINGLE_SCOPE_APP )
357
+ public void testSingleScope () throws Exception {
358
+ String expectedScope = OIDC_SCOPE + "+profile" ;
359
+ loginToApp (KeycloakConfiguration .ALICE , KeycloakConfiguration .ALICE_PASSWORD , HttpURLConnection .HTTP_OK , SimpleServlet .RESPONSE_BODY , true ,
360
+ new URL ("http" , TestSuiteEnvironment .getHttpAddress (), TestSuiteEnvironment .getHttpPort (),
361
+ "/" + SINGLE_SCOPE_APP + SimpleServletWithScope .SERVLET_PATH ).toURI (), expectedScope , false );
362
+ }
363
+
364
+ @ Test
365
+ @ OperateOnDeployment (MULTIPLE_SCOPE_APP )
366
+ public void testMultipleScope () throws Exception {
367
+ String expectedScope = OIDC_SCOPE + "+phone+profile+microprofile-jwt+email" ;
368
+ loginToApp (KeycloakConfiguration .ALICE , KeycloakConfiguration .ALICE_PASSWORD , HttpURLConnection .HTTP_OK , SimpleServlet .RESPONSE_BODY , true ,
369
+ new URL ("http" , TestSuiteEnvironment .getHttpAddress (), TestSuiteEnvironment .getHttpPort (),
370
+ "/" + MULTIPLE_SCOPE_APP + SimpleServletWithScope .SERVLET_PATH ).toURI (), expectedScope , false );
371
+ }
372
+
373
+ @ Test
374
+ @ OperateOnDeployment (INVALID_SCOPE_APP )
375
+ public void testInvalidScope () throws Exception {
376
+ String expectedScope = OIDC_SCOPE + "+INVALID_SCOPE" ;
377
+ loginToApp (KeycloakConfiguration .ALICE , KeycloakConfiguration .ALICE_PASSWORD , HttpURLConnection .HTTP_OK , SimpleServlet .RESPONSE_BODY , false ,
378
+ new URL ("http" , TestSuiteEnvironment .getHttpAddress (), TestSuiteEnvironment .getHttpPort (),
379
+ "/" + INVALID_SCOPE_APP + SimpleServletWithScope .SERVLET_PATH ).toURI (), expectedScope , true );
380
+ }
381
+
323
382
public static void loginToApp (String appName , String username , String password , int expectedStatusCode , String expectedText ) throws Exception {
324
383
loginToApp (username , password , expectedStatusCode , expectedText , true ,
325
384
new URL ("http" , TestSuiteEnvironment .getHttpAddress (), TestSuiteEnvironment .getHttpPort (),
@@ -336,6 +395,10 @@ public static void loginToApp(String appName, String username, String password,
336
395
}
337
396
338
397
public static void loginToApp (String username , String password , int expectedStatusCode , String expectedText , boolean loginToKeycloak , URI requestUri ) throws Exception {
398
+ loginToApp (username , password , expectedStatusCode , expectedText , loginToKeycloak , requestUri , null , false );
399
+ }
400
+
401
+ public static void loginToApp (String username , String password , int expectedStatusCode , String expectedText , boolean loginToKeycloak , URI requestUri , String expectedScope , boolean checkInvalidScope ) throws Exception {
339
402
CookieStore store = new BasicCookieStore ();
340
403
HttpClient httpClient = TestHttpClientUtils .promiscuousCookieHttpClientBuilder ()
341
404
.setDefaultCookieStore (store )
@@ -355,7 +418,22 @@ public static void loginToApp(String username, String password, int expectedStat
355
418
if (expectedText != null ) {
356
419
String responseString = new BasicResponseHandler ().handleResponse (afterLoginClickResponse );
357
420
assertTrue ("Unexpected result " + responseString , responseString .contains (expectedText ));
421
+ if (expectedScope != null ) {
422
+ assertTrue (context .toString ().contains ("scope=" + expectedScope ));
423
+ if (expectedScope .contains ("profile" )) {
424
+ assertTrue (responseString .contains ("profile: " + KeycloakConfiguration .ALICE_FIRST_NAME + " " + KeycloakConfiguration .ALICE_LAST_NAME ));
425
+ }
426
+ if (expectedScope .contains ("email" )) {
427
+ assertTrue (responseString .contains ("email: " + KeycloakConfiguration .ALICE_EMAIL_VERIFIED ));
428
+ }
429
+ if (expectedScope .contains ("microprofile-jwt" )) {
430
+ assertTrue (responseString .contains ("microprofile-jwt: [" + KeycloakConfiguration .JBOSS_ADMIN_ROLE + ", " + KeycloakConfiguration .USER_ROLE + "]" ));
431
+ }
432
+ }
358
433
}
434
+ } else if (checkInvalidScope ) {
435
+ assertTrue (context .toString ().contains ("error_description=Invalid+scopes" ));
436
+ assertTrue ("Expected code == BAD REQUEST but got " + statusCode + " for request=" + requestUri , statusCode == HttpURLConnection .HTTP_BAD_REQUEST );
359
437
} else {
360
438
assertTrue ("Expected code == FORBIDDEN but got " + statusCode + " for request=" + requestUri , statusCode == HttpURLConnection .HTTP_FORBIDDEN );
361
439
}
@@ -653,4 +731,9 @@ public enum Type {
653
731
HIDDEN , SUBMIT
654
732
}
655
733
}
734
+ protected static <T extends OidcBaseTest > void addSystemProperty (ManagementClient client , Class <T > clazz ) throws Exception {
735
+ ModelNode add = Util .createAddOperation (PathAddress .pathAddress (SYSTEM_PROPERTY , OidcBaseTest .class .getName ()));
736
+ add .get (VALUE ).set (clazz .getName ());
737
+ ManagementOperations .executeOperation (client .getControllerClient (), add );
738
+ }
656
739
}
0 commit comments