|
61 | 61 | import org.keycloak.representations.userprofile.config.UPConfig.UnmanagedAttributePolicy; |
62 | 62 | import org.keycloak.representations.userprofile.config.UPGroup; |
63 | 63 | import org.keycloak.services.messages.Messages; |
| 64 | +import org.keycloak.testsuite.arquillian.annotation.ModelTest; |
64 | 65 | import org.keycloak.testsuite.runonserver.RunOnServer; |
65 | 66 | import org.keycloak.testsuite.util.LDAPRule; |
66 | 67 | import org.keycloak.userprofile.AttributeGroupMetadata; |
@@ -98,8 +99,10 @@ public void configureTestRealm(RealmRepresentation testRealm) { |
98 | 99 | testRealm.setClientScopes(new ArrayList<>()); |
99 | 100 | testRealm.getClientScopes().add(ClientScopeBuilder.create().name("customer").protocol("openid-connect").build()); |
100 | 101 | testRealm.getClientScopes().add(ClientScopeBuilder.create().name("client-a").protocol("openid-connect").build()); |
| 102 | + testRealm.getClientScopes().add(ClientScopeBuilder.create().name("some-optional-scope").protocol("openid-connect").build()); |
101 | 103 | ClientRepresentation client = KeycloakModelUtils.createClient(testRealm, "client-a"); |
102 | 104 | client.setDefaultClientScopes(Collections.singletonList("customer")); |
| 105 | + client.setOptionalClientScopes(Collections.singletonList("some-optional-scope")); |
103 | 106 | KeycloakModelUtils.createClient(testRealm, "client-b"); |
104 | 107 | } |
105 | 108 |
|
@@ -1429,6 +1432,74 @@ private static void testRequiredByClientScope(KeycloakSession session) { |
1429 | 1432 |
|
1430 | 1433 | } |
1431 | 1434 |
|
| 1435 | + @Test |
| 1436 | + @ModelTest |
| 1437 | + public void testRequiredByOptionalClientScope(KeycloakSession session) { |
| 1438 | + RealmModel realm = session.realms().getRealmByName("test"); |
| 1439 | + session.getContext().setRealm(realm); |
| 1440 | + |
| 1441 | + UserProfileProvider provider = getUserProfileProvider(session); |
| 1442 | + UPConfig config = parseDefaultConfig(); |
| 1443 | + config.addOrReplaceAttribute(new UPAttribute(ATT_ADDRESS, new UPAttributePermissions(Set.of(), Set.of(ROLE_ADMIN, ROLE_USER)), new UPAttributeRequired(Set.of(ROLE_ADMIN, ROLE_USER), Set.of("some-optional-scope")))); |
| 1444 | + provider.setConfiguration(config); |
| 1445 | + |
| 1446 | + Map<String, Object> attributes = new HashMap<>(); |
| 1447 | + |
| 1448 | + attributes.put(UserModel.USERNAME, "user"); |
| 1449 | + attributes.put(UserModel.FIRST_NAME, "John"); |
| 1450 | + attributes.put(UserModel.LAST_NAME, "Doe"); |
| 1451 | + attributes. put( UserModel. EMAIL, "[email protected]"); |
| 1452 | + |
| 1453 | + // client with default scopes. No address scope included |
| 1454 | + configureAuthenticationSession(session, "client-a", null); |
| 1455 | + |
| 1456 | + // No fail on admin and account console as they do not have scopes |
| 1457 | + UserProfile profile = provider.create(UserProfileContext.USER_API, attributes); |
| 1458 | + profile.validate(); |
| 1459 | + profile = provider.create(UserProfileContext.ACCOUNT, attributes); |
| 1460 | + profile.validate(); |
| 1461 | + |
| 1462 | + // no fail on auth flow scopes when scope is not required |
| 1463 | + profile = provider.create(UserProfileContext.REGISTRATION, attributes); |
| 1464 | + profile.validate(); |
| 1465 | + profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes); |
| 1466 | + profile.validate(); |
| 1467 | + profile = provider.create(UserProfileContext.IDP_REVIEW, attributes); |
| 1468 | + profile.validate(); |
| 1469 | + |
| 1470 | + // client with default scopes for which is attribute NOT configured as required |
| 1471 | + configureAuthenticationSession(session, "client-a", Set.of("some-optional-scope")); |
| 1472 | + |
| 1473 | + // No fail on admin and account console as they do not have scopes |
| 1474 | + profile = provider.create(UserProfileContext.USER_API, attributes); |
| 1475 | + profile.validate(); |
| 1476 | + profile = provider.create(UserProfileContext.ACCOUNT, attributes); |
| 1477 | + profile.validate(); |
| 1478 | + |
| 1479 | + // fail on auth flow scopes when scope is required |
| 1480 | + try { |
| 1481 | + profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes); |
| 1482 | + profile.validate(); |
| 1483 | + fail("Should fail validation"); |
| 1484 | + } catch (ValidationException ve) { |
| 1485 | + assertTrue(ve.isAttributeOnError(ATT_ADDRESS)); |
| 1486 | + } |
| 1487 | + try { |
| 1488 | + profile = provider.create(UserProfileContext.REGISTRATION, attributes); |
| 1489 | + profile.validate(); |
| 1490 | + fail("Should fail validation"); |
| 1491 | + } catch (ValidationException ve) { |
| 1492 | + assertTrue(ve.isAttributeOnError(ATT_ADDRESS)); |
| 1493 | + } |
| 1494 | + try { |
| 1495 | + profile = provider.create(UserProfileContext.IDP_REVIEW, attributes); |
| 1496 | + profile.validate(); |
| 1497 | + fail("Should fail validation"); |
| 1498 | + } catch (ValidationException ve) { |
| 1499 | + assertTrue(ve.isAttributeOnError(ATT_ADDRESS)); |
| 1500 | + } |
| 1501 | + } |
| 1502 | + |
1432 | 1503 | @Test |
1433 | 1504 | public void testConfigurationInvalidScope() { |
1434 | 1505 | getTestingClient().server(TEST_REALM_NAME).run((RunOnServer) UserProfileTest::testConfigurationInvalidScope); |
|
0 commit comments