use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.
the class UserProfileTest method testCreateAndUpdateUser.
private static void testCreateAndUpdateUser(KeycloakSession session) throws IOException {
UserProfileProvider provider = getDynamicUserProfileProvider(session);
UPConfig config = JsonSerialization.readValue(provider.getConfiguration(), UPConfig.class);
UPAttribute attribute = new UPAttribute();
attribute.setName("address");
UPAttributePermissions permissions = new UPAttributePermissions();
permissions.setEdit(new HashSet<>(Arrays.asList("admin", "user")));
attribute.setPermissions(permissions);
config.addAttribute(attribute);
attribute = new UPAttribute();
attribute.setName("business.address");
permissions = new UPAttributePermissions();
permissions.setEdit(new HashSet<>(Arrays.asList("admin", "user")));
attribute.setPermissions(permissions);
config.addAttribute(attribute);
provider.setConfiguration(JsonSerialization.writeValueAsString(config));
Map<String, Object> attributes = new HashMap<>();
String userName = org.keycloak.models.utils.KeycloakModelUtils.generateId();
attributes.put(UserModel.USERNAME, userName);
attributes.put(UserModel.FIRST_NAME, "Joe");
attributes.put(UserModel.LAST_NAME, "Doe");
attributes.put("address", "fixed-address");
UserProfile profile = provider.create(UserProfileContext.ACCOUNT, attributes);
UserModel user = profile.create();
assertEquals(userName, user.getUsername());
assertEquals("fixed-address", user.getFirstAttribute("address"));
attributes.put(UserModel.FIRST_NAME, "Alice");
attributes.put(UserModel.LAST_NAME, "In Chains");
attributes.put(UserModel.EMAIL, "alice@keycloak.org");
profile = provider.create(UserProfileContext.ACCOUNT, attributes, user);
Set<String> attributesUpdated = new HashSet<>();
Map<String, String> attributesUpdatedOldValues = new HashMap<>();
attributesUpdatedOldValues.put(UserModel.FIRST_NAME, "Joe");
attributesUpdatedOldValues.put(UserModel.LAST_NAME, "Doe");
profile.update((attributeName, userModel, oldValue) -> {
assertTrue(attributesUpdated.add(attributeName));
assertEquals(attributesUpdatedOldValues.get(attributeName), getSingleValue(oldValue));
assertEquals(attributes.get(attributeName), userModel.getFirstAttribute(attributeName));
});
assertThat(attributesUpdated, containsInAnyOrder(UserModel.FIRST_NAME, UserModel.LAST_NAME, UserModel.EMAIL));
configureAuthenticationSession(session);
attributes.put("business.address", "fixed-business-address");
profile = provider.create(UserProfileContext.ACCOUNT, attributes, user);
attributesUpdated.clear();
profile.update((attributeName, userModel, oldValue) -> assertTrue(attributesUpdated.add(attributeName)));
assertThat(attributesUpdated, containsInAnyOrder("business.address"));
assertEquals("fixed-business-address", user.getFirstAttribute("business.address"));
}
use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.
the class UserProfileTest method testOptionalAttributes.
private static void testOptionalAttributes(KeycloakSession session) throws IOException {
DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
ComponentModel component = provider.getComponentModel();
assertNotNull(component);
UPConfig config = new UPConfig();
UPAttribute attribute = new UPAttribute();
attribute.setName(UserModel.FIRST_NAME);
Map<String, Object> validatorConfig = new HashMap<>();
validatorConfig.put(LengthValidator.KEY_MAX, 4);
attribute.addValidation(LengthValidator.ID, validatorConfig);
config.addAttribute(attribute);
attribute = new UPAttribute();
attribute.setName(UserModel.LAST_NAME);
attribute.addValidation(LengthValidator.ID, validatorConfig);
config.addAttribute(attribute);
provider.setConfiguration(JsonSerialization.writeValueAsString(config));
Map<String, Object> attributes = new HashMap<>();
attributes.put(UserModel.USERNAME, "user");
// not present attributes are OK
UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
profile.validate();
// empty attributes are OK
attributes.put(UserModel.FIRST_NAME, "");
attributes.put(UserModel.LAST_NAME, "");
profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
profile.validate();
// filled attributes are OK
attributes.put(UserModel.FIRST_NAME, "John");
attributes.put(UserModel.LAST_NAME, "Doe");
profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
profile.validate();
// fails due to additional length validation so it is executed correctly
attributes.put(UserModel.FIRST_NAME, "JohnTooLong");
attributes.put(UserModel.LAST_NAME, "DoeTooLong");
profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
try {
profile.validate();
fail("Should fail validation");
} catch (ValidationException ve) {
assertTrue(ve.isAttributeOnError(UserModel.FIRST_NAME));
assertTrue(ve.isAttributeOnError(UserModel.LAST_NAME));
}
}
use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.
the class UPConfigParserTest method parseConfigurationFile_OK.
@Test
public void parseConfigurationFile_OK() throws IOException {
UPConfig config = loadValidConfig();
Assert.assertNotNull(config);
// assert *** attributes ***
Assert.assertEquals(5, config.getAttributes().size());
UPAttribute att = config.getAttributes().get(1);
Assert.assertNotNull(att);
Assert.assertEquals("email", att.getName());
// validation
Assert.assertEquals(3, att.getValidations().size());
Assert.assertEquals(1, att.getValidations().get("length").size());
Assert.assertEquals(255, att.getValidations().get("length").get("max"));
// annotations
Assert.assertEquals("userEmailFormFieldHint", att.getAnnotations().get("formHintKey"));
att = config.getAttributes().get(4);
// required
Assert.assertNotNull(att.getRequired());
Assert.assertFalse(att.getRequired().isAlways());
Assert.assertNotNull(att.getRequired().getScopes());
Assert.assertNotNull(att.getRequired().getRoles());
Assert.assertEquals(2, att.getRequired().getRoles().size());
att = config.getAttributes().get(3);
Assert.assertTrue(att.getRequired().isAlways());
// permissions
Assert.assertNotNull(att.getPermissions());
Assert.assertNotNull(att.getPermissions().getEdit());
Assert.assertEquals(1, att.getPermissions().getEdit().size());
Assert.assertTrue(att.getPermissions().getEdit().contains("admin"));
Assert.assertNotNull(att.getPermissions().getView());
Assert.assertEquals(2, att.getPermissions().getView().size());
Assert.assertTrue(att.getPermissions().getView().contains("admin"));
Assert.assertTrue(att.getPermissions().getView().contains("user"));
// selector
att = config.getAttributes().get(4);
Assert.assertNotNull(att.getSelector().getScopes());
Assert.assertEquals(3, att.getSelector().getScopes().size());
Assert.assertTrue(att.getSelector().getScopes().contains("phone-3-sel"));
// displayName
att = config.getAttributes().get(4);
Assert.assertEquals("${profile.phone}", att.getDisplayName());
// group
Assert.assertEquals("contact", att.getGroup());
// assert *** groups ***
Assert.assertEquals(1, config.getGroups().size());
UPGroup group = config.getGroups().get(0);
Assert.assertEquals("contact", group.getName());
Assert.assertEquals("Contact information", group.getDisplayHeader());
Assert.assertEquals("Required to contact you in case of emergency", group.getDisplayDescription());
Assert.assertEquals(1, group.getAnnotations().size());
Assert.assertEquals("value1", group.getAnnotations().get("contactanno1"));
}
use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.
the class UPConfigParserTest method validateConfiguration_attributeNameErrors.
public static void validateConfiguration_attributeNameErrors(KeycloakSession session) throws IOException {
UPConfig config = loadValidConfig();
// we run this test without KeycloakSession so validator configs are not validated here
UPAttribute attConfig = config.getAttributes().get(1);
attConfig.setName(null);
List<String> errors = validate(session, config);
Assert.assertEquals(1, errors.size());
attConfig.setName(" ");
errors = validate(session, config);
Assert.assertEquals(1, errors.size());
// duplicate attribute name
attConfig.setName("firstName");
errors = validate(session, config);
Assert.assertEquals(1, errors.size());
// attribute name format error - unallowed character
attConfig.setName("ema il");
errors = validate(session, config);
Assert.assertEquals(1, errors.size());
}
use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.
the class UPConfigParserTest method validateConfiguration_attributePermissionsErrors.
public static void validateConfiguration_attributePermissionsErrors(KeycloakSession session) throws IOException {
UPConfig config = loadValidConfig();
// we run this test without KeycloakSession so validator configs are not validated here
UPAttribute attConfig = config.getAttributes().get(1);
// no permissions configures at all
attConfig.setPermissions(null);
List<String> errors = validate(session, config);
Assert.assertEquals(0, errors.size());
// no permissions structure fields configured
UPAttributePermissions permsConfig = new UPAttributePermissions();
attConfig.setPermissions(permsConfig);
errors = validate(session, config);
Assert.assertTrue(errors.isEmpty());
// valid if both are present, even empty
permsConfig.setEdit(Collections.emptySet());
permsConfig.setView(Collections.emptySet());
attConfig.setPermissions(permsConfig);
errors = validate(session, config);
Assert.assertEquals(0, errors.size());
Set<String> withInvRole = Collections.singleton("invalid");
// invalid role used for view
permsConfig.setView(withInvRole);
errors = validate(session, config);
Assert.assertEquals(1, errors.size());
// invalid role used for edit also
permsConfig.setEdit(withInvRole);
errors = validate(session, config);
Assert.assertEquals(2, errors.size());
}
Aggregations