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.
Overview
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
Types of Attribute Mappers
User Property Mapper
Maps basic user properties like email, username, and first/last name.
User Attribute Mapper
Maps custom user attributes stored in Keycloak to protocol claims.
Hardcoded Attribute Mapper
Adds a fixed value to tokens or assertions.
Group Membership Mapper
Includes user’s group memberships in the authentication response.
Role List Mapper
Maps user roles to claims or assertions.
OIDC Attribute Mapping
For OpenID Connect applications, attributes are mapped to JWT claims.
Common OIDC Mappings
email → email
firstName → given_name
lastName → family_name
username → preferred_username
groups → groups
roles → realm_access.rolesCreating OIDC Mappers
- Navigate to your OIDC client in Skycloak
- Go to the Client Scopes tab
- Select the appropriate scope (usually the client’s dedicated scope)
- Click Add Mapper → By configuration
- Choose the mapper type
- Configure the mapping
SAML Attribute Mapping
For SAML applications, attributes are mapped to SAML assertions.
Common SAML Mappings
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/groupsCreating SAML Mappers
- Navigate to your SAML client in Skycloak
- Go to the Mappers tab
- Click Create to add a new mapper
- 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
Advanced Mapping Scenarios
Conditional Mapping
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"));
}Composite Attributes
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);Dynamic Group Mapping
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 Management
Setting User Attributes
User attributes can be set through:
- Admin Console
- Admin REST API
- User registration forms
- Identity provider mappings
Attribute Validation
Implement validation using:
- Regular expressions
- Required field markers
- Custom validators
Protocol-Specific Considerations
OIDC Scopes
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
SAML NameID Formats
Choose appropriate NameID format:
-
email: Uses email as identifier -
persistent: Stable pseudonym -
transient: Temporary identifier -
unspecified: No specific format
Best Practices
- Minimize Exposed Data: Only map necessary attributes
- Use Standard Claims: Follow OIDC/SAML standards when possible
- Document Mappings: Maintain documentation of all attribute mappings
- Test Thoroughly: Verify mappings with actual authentication flows
- Consider Performance: Complex mappers can impact performance
- Secure Sensitive Data: Be cautious with PII and sensitive attributes
Troubleshooting
Attributes Not Appearing
- Check mapper configuration
- Verify user has the attribute set
- Confirm client/scope settings
- Review protocol mapper scope
Wrong Attribute Format
- Check NameFormat (SAML) or claim type (OIDC)
- Verify data transformation logic
- Review application expectations
Performance Issues
- Simplify complex JavaScript mappers
- Cache frequently accessed data
- Reduce number of mapped attributes
Integration Examples
Salesforce SAML
Mapper Type: User Property
Property: email
SAML Attribute Name: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
SAML Attribute NameFormat: URI ReferenceAWS Cognito OIDC
Mapper Type: User Attribute
User Attribute: department
Token Claim Name: custom:department
Add to ID Token: Yes
Add to Access Token: Yes