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
Section titled “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
Section titled “Types of Attribute Mappers”User Property Mapper
Section titled “User Property Mapper”Maps basic user properties like email, username, and first/last name.
User Attribute Mapper
Section titled “User Attribute Mapper”Maps custom user attributes stored in Keycloak to protocol claims.
Hardcoded Attribute Mapper
Section titled “Hardcoded Attribute Mapper”Adds a fixed value to tokens or assertions.
Group Membership Mapper
Section titled “Group Membership Mapper”Includes user’s group memberships in the authentication response.
Role List Mapper
Section titled “Role List Mapper”Maps user roles to claims or assertions.
OIDC Attribute Mapping
Section titled “OIDC Attribute Mapping”For OpenID Connect applications, attributes are mapped to JWT claims.
Common OIDC Mappings
Section titled “Common OIDC Mappings”email → email
firstName → given_name
lastName → family_name
username → preferred_username
groups → groups
roles → realm_access.roles
Creating OIDC Mappers
Section titled “Creating 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
Section titled “SAML Attribute Mapping”For SAML applications, attributes are mapped to SAML assertions.
Common SAML Mappings
Section titled “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/groups
Creating SAML Mappers
Section titled “Creating 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
Section titled “Advanced Mapping Scenarios”Conditional Mapping
Section titled “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
Section titled “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
Section titled “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
Section titled “User Attributes Management”Setting User Attributes
Section titled “Setting User Attributes”User attributes can be set through:
- Admin Console
- Admin REST API
- User registration forms
- Identity provider mappings
Attribute Validation
Section titled “Attribute Validation”Implement validation using:
- Regular expressions
- Required field markers
- Custom validators
Protocol-Specific Considerations
Section titled “Protocol-Specific Considerations”OIDC Scopes
Section titled “OIDC Scopes”Attributes can be grouped into scopes:
profile: Basic profile informationemail: Email address and verification statusaddress: Physical addressphone: Phone number- Custom scopes for domain-specific attributes
SAML NameID Formats
Section titled “SAML NameID Formats”Choose appropriate NameID format:
email: Uses email as identifierpersistent: Stable pseudonymtransient: Temporary identifierunspecified: No specific format
Best Practices
Section titled “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
Section titled “Troubleshooting”Attributes Not Appearing
Section titled “Attributes Not Appearing”- Check mapper configuration
- Verify user has the attribute set
- Confirm client/scope settings
- Review protocol mapper scope
Wrong Attribute Format
Section titled “Wrong Attribute Format”- Check NameFormat (SAML) or claim type (OIDC)
- Verify data transformation logic
- Review application expectations
Performance Issues
Section titled “Performance Issues”- Simplify complex JavaScript mappers
- Cache frequently accessed data
- Reduce number of mapped attributes
Integration Examples
Section titled “Integration Examples”Salesforce SAML
Section titled “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 Reference
AWS Cognito OIDC
Section titled “AWS Cognito OIDC”Mapper Type: User Attribute
User Attribute: department
Token Claim Name: custom:department
Add to ID Token: Yes
Add to Access Token: Yes