Class DefaultAttributes

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<String,List<String>>
org.keycloak.userprofile.DefaultAttributes
All Implemented Interfaces:
Serializable, Cloneable, Map<String,List<String>>, org.keycloak.userprofile.Attributes

public class DefaultAttributes extends HashMap<String,List<String>> implements org.keycloak.userprofile.Attributes

The default implementation for Attributes. Should be reused as much as possible by the different implementations of UserProfileProvider.

One of the main aspects of this implementation is to allow normalizing attributes accordingly to the profile configuration and current context. As such, it provides some common normalization to common profile attributes (e.g.: username, email, first and last names, dynamic read-only attributes).

This implementation is not specific to any user profile implementation.

Author:
Pedro Igor
See Also:
  • Field Details

    • READ_ONLY_ATTRIBUTE_KEY

      public static final String READ_ONLY_ATTRIBUTE_KEY
      To reference dynamic attributes that can be configured as read-only when setting up the provider. We should probably remove that once we remove the legacy provider, because this will come from the configuration.
      See Also:
    • DEFAULT_MAX_LENGTH_ATTRIBUTES

      public static final String DEFAULT_MAX_LENGTH_ATTRIBUTES
      See Also:
    • context

      protected final org.keycloak.userprofile.UserProfileContext context
    • session

      protected final org.keycloak.models.KeycloakSession session
    • user

      protected final org.keycloak.models.UserModel user
  • Constructor Details

    • DefaultAttributes

      public DefaultAttributes(org.keycloak.userprofile.UserProfileContext context, Map<String,?> attributes, org.keycloak.models.UserModel user, org.keycloak.userprofile.UserProfileMetadata profileMetadata, org.keycloak.models.KeycloakSession session)
  • Method Details

    • isReadOnly

      public boolean isReadOnly(String name)
      Specified by:
      isReadOnly in interface org.keycloak.userprofile.Attributes
    • isReadOnlyFromMetadata

      protected boolean isReadOnlyFromMetadata(String attributeName)
      Checks whether an attribute is marked as read only by looking at its metadata.
      Parameters:
      attributeName - the attribute name
      Returns:
    • isRequired

      public boolean isRequired(String name)
      Specified by:
      isRequired in interface org.keycloak.userprofile.Attributes
    • validate

      public boolean validate(String name, Consumer<org.keycloak.validate.ValidationError>... listeners)
      Specified by:
      validate in interface org.keycloak.userprofile.Attributes
    • addDefaultValidators

      protected void addDefaultValidators(String name, List<org.keycloak.userprofile.AttributeMetadata> metadatas)
    • get

      public List<String> get(String name)
      Specified by:
      get in interface org.keycloak.userprofile.Attributes
    • contains

      public boolean contains(String name)
      Specified by:
      contains in interface org.keycloak.userprofile.Attributes
    • nameSet

      public Set<String> nameSet()
      Specified by:
      nameSet in interface org.keycloak.userprofile.Attributes
    • getWritable

      public Map<String,List<String>> getWritable()
      Specified by:
      getWritable in interface org.keycloak.userprofile.Attributes
    • getMetadata

      public org.keycloak.userprofile.AttributeMetadata getMetadata(String name)
      Specified by:
      getMetadata in interface org.keycloak.userprofile.Attributes
    • getReadable

      public Map<String,List<String>> getReadable()
      Specified by:
      getReadable in interface org.keycloak.userprofile.Attributes
    • toMap

      public Map<String,List<String>> toMap()
      Specified by:
      toMap in interface org.keycloak.userprofile.Attributes
    • isDefaultAttribute

      public boolean isDefaultAttribute(String name)
      Specified by:
      isDefaultAttribute in interface org.keycloak.userprofile.Attributes
    • createAttributeContext

      protected org.keycloak.userprofile.AttributeContext createAttributeContext(org.keycloak.userprofile.AttributeMetadata metadata)
    • normalizeAttributeValues

      protected List<String> normalizeAttributeValues(String name, Object value)
      Intentionally kept to protected visibility to allow for custom normalization logic while clients adopt User Profile
    • isAllowUnmanagedAttribute

      protected boolean isAllowUnmanagedAttribute()
    • setUserName

      protected void setUserName(Map<String,List<String>> newAttributes, List<String> values)
    • isIncludeAttributeIfNotProvided

      protected boolean isIncludeAttributeIfNotProvided(org.keycloak.userprofile.AttributeMetadata metadata)
    • isSupportedAttribute

      protected boolean isSupportedAttribute(String name)

      Checks whether an attribute is support by the profile configuration and the current context.

      This method can be used to avoid unexpected attributes from being added as an attribute because the attribute source is a regular Map and not normalized.

      Parameters:
      name - the name of the attribute
      Returns:
    • isReadOnlyInternalAttribute

      protected boolean isReadOnlyInternalAttribute(String attributeName)

      Returns whether an attribute is read only based on the provider configuration (using provider config), usually related to internal attributes managed by the server.

      For user-defined attributes, it should be preferable to use the user profile configuration.

      Parameters:
      attributeName - the attribute name
      Returns:
      true if the attribute is readonly. Otherwise, returns false
    • getUnmanagedAttributes

      public Map<String,List<String>> getUnmanagedAttributes()
      Specified by:
      getUnmanagedAttributes in interface org.keycloak.userprofile.Attributes
    • getAnnotations

      public Map<String,Object> getAnnotations(String name)
      Specified by:
      getAnnotations in interface org.keycloak.userprofile.Attributes
    • createUnmanagedAttributeMetadata

      protected org.keycloak.userprofile.AttributeMetadata createUnmanagedAttributeMetadata(String name)