Skip to content

Attribute Mapping

Attribute mapping allows you to transform and map user attributes between different systems and protocols in Keycloak. This guide covers the fundamentals of attribute mapping for OIDC, SAML, and custom applications.

Attribute mapping is essential for:

  • Transforming user data between different formats
  • Exposing specific user attributes to applications
  • Maintaining data consistency across systems
  • Implementing custom business logic for attributes

Maps basic user properties like email, username, and first/last name.

Maps custom user attributes stored in Keycloak to protocol claims.

Adds a fixed value to tokens or assertions.

Includes user’s group memberships in the authentication response.

Maps user roles to claims or assertions.

For OpenID Connect applications, attributes are mapped to JWT claims.

email → email
firstName → given_name
lastName → family_name
username → preferred_username
groups → groups
roles → realm_access.roles
  1. Navigate to your OIDC client in Skycloak
  2. Go to the Client Scopes tab
  3. Select the appropriate scope (usually the client’s dedicated scope)
  4. Click Add Mapper → By configuration
  5. Choose the mapper type
  6. Configure the mapping

For SAML applications, attributes are mapped to SAML assertions.

email → http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
firstName → http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname
lastName → http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname
groups → http://schemas.microsoft.com/ws/2008/06/identity/claims/groups
  1. Navigate to your SAML client in Skycloak
  2. Go to the Mappers tab
  3. Click Create to add a new mapper
  4. Configure:
    • Name: Descriptive name for the mapper
    • Mapper Type: Select appropriate type
    • Property: Source attribute
    • SAML Attribute Name: Target attribute name
    • SAML Attribute NameFormat: Format specification

Use JavaScript mappers for conditional logic:

// Only include department for internal users
var email = user.getEmail();
if (email && email.endsWith("@company.com")) {
    token.setOtherClaims("department", user.getAttribute("department"));
}

Combine multiple attributes into one:

// Create display name from first and last name
var firstName = user.getFirstName() || "";
var lastName = user.getLastName() || "";
token.setOtherClaims("displayName", firstName + " " + lastName);

Map groups with prefix filtering:

// Only include groups starting with "app-"
var groups = user.getGroups();
var appGroups = [];
for (var i = 0; i < groups.size(); i++) {
    var group = groups.get(i);
    if (group.startsWith("/app-")) {
        appGroups.push(group.substring(1)); // Remove leading slash
    }
}
token.setOtherClaims("app_groups", appGroups);

User attributes can be set through:

  • Admin Console
  • Admin REST API
  • User registration forms
  • Identity provider mappings

Implement validation using:

  • Regular expressions
  • Required field markers
  • Custom validators

Attributes can be grouped into scopes:

  • profile: Basic profile information
  • email: Email address and verification status
  • address: Physical address
  • phone: Phone number
  • Custom scopes for domain-specific attributes

Choose appropriate NameID format:

  • email: Uses email as identifier
  • persistent: Stable pseudonym
  • transient: Temporary identifier
  • unspecified: No specific format
  1. Minimize Exposed Data: Only map necessary attributes
  2. Use Standard Claims: Follow OIDC/SAML standards when possible
  3. Document Mappings: Maintain documentation of all attribute mappings
  4. Test Thoroughly: Verify mappings with actual authentication flows
  5. Consider Performance: Complex mappers can impact performance
  6. Secure Sensitive Data: Be cautious with PII and sensitive attributes
  1. Check mapper configuration
  2. Verify user has the attribute set
  3. Confirm client/scope settings
  4. Review protocol mapper scope
  1. Check NameFormat (SAML) or claim type (OIDC)
  2. Verify data transformation logic
  3. Review application expectations
  1. Simplify complex JavaScript mappers
  2. Cache frequently accessed data
  3. Reduce number of mapped attributes
Mapper Type: User Property
Property: email
SAML Attribute Name: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
SAML Attribute NameFormat: URI Reference
Mapper Type: User Attribute
User Attribute: department
Token Claim Name: custom:department
Add to ID Token: Yes
Add to Access Token: Yes