Package org.keycloak.adapters.saml
Class PropertiesBasedRoleMapper
- java.lang.Object
-
- org.keycloak.adapters.saml.PropertiesBasedRoleMapper
-
- All Implemented Interfaces:
RoleMappingsProvider
public class PropertiesBasedRoleMapper extends Object implements RoleMappingsProvider
ARoleMappingsProviderimplementation that uses apropertiesfile to determine the mappings that should be applied to the SAML principal and roles. It is always identified by the idproperties-based-role-mapperinkeycloak-saml.xml. This provider relies on two configuration properties that can be used to specify the location of thepropertiesfile that will be used. First, it checks if theproperties.file.locationproperty has been specified, using the configured value to locate thepropertiesfile in the filesystem. If the configured file is not located, the provider throws aRuntimeException. The following snippet shows an example of provider using theproperties.file.configurationoption to load theroles.propertiesfile from the/opt/mappers/directory in the filesystem:If theproperties.file.locationconfiguration property is not present, the provider checks theproperties.resource.locationproperty, using the configured value to load thepropertiesfile from the WAR resource. If no value is found, it finally attempts to load a file namedrole-mappings.propertiesfrom theWEB-INFdirectory of the application. Failure to load the file from the resource will result in the provider throwing aRuntimeException. The following snippet shows an example of provider using theproperties.resource.locationto load theroles.propertiesfile from the application's/WEB-INF/conf/directory:Thepropertiesfile can contain both roles and principals as keys, and a list of zero or more roles separated by comma as values. When the{@link #map(String, Set)}method is called, the implementation iterates through the set of roles that were extracted from the assertion and checks, for eache role, if a mapping exists. If the role maps to an empty role, it is discarded. If it maps to a set of one ore more different roles, then these roles are set in the result set. If no mapping is found for the role then it is included as is in the result set. Once the roles have been processed, the implementation checks if the principal extracted from the assertion contains an entry in thepropertiesfile. If a mapping for the principal exists, any roles listed as value are added to the result set. This allows the assignment of extra roles to a principal. For example, consider the followingpropertiesfile:# role to roles mappings samlRoleA=jeeRoleX,jeeRoleY samlRoleB= # principal to roles mappings kc-user=jeeRoleZIf the{@link #map(String, Set)}method is called withkc-useras principal and a set containing rolessamlRoleA,samlRoleB,samlRoleC, the result set will be formed by the rolesjeeRoleX,jeeRoleY,samlRoleC,jeeRoleZ. In this case,samlRoleAis mapped to two roles (jeeRoleX,jeeRoleY),samlRoleBis discarded as it is mapped to an empty role,samlRoleCis used as is and the principal is also assignedjeeRoleZ.- Author:
- Stefan Guilhen
-
-
Field Summary
Fields Modifier and Type Field Description static StringPROVIDER_ID
-
Constructor Summary
Constructors Constructor Description PropertiesBasedRoleMapper()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetId()Obtains the provider's identifier.voidinit(SamlDeployment deployment, ResourceLoader loader, Properties config)Initializes the provider.Set<String>map(String principalName, Set<String> roles)Produces the final set of roles that should be assigned to the specified principal.
-
-
-
Field Detail
-
PROVIDER_ID
public static final String PROVIDER_ID
- See Also:
- Constant Field Values
-
-
Method Detail
-
getId
public String getId()
Description copied from interface:RoleMappingsProviderObtains the provider's identifier. This id is specified inkeycloak-saml.xmlto identify the provider implementation to be used.- Specified by:
getIdin interfaceRoleMappingsProvider- Returns:
- a
Stringrepresenting the provider's id.
-
init
public void init(SamlDeployment deployment, ResourceLoader loader, Properties config)
Description copied from interface:RoleMappingsProviderInitializes the provider. This method is called by the adapter in deployment time after the contents ofkeycloak-saml.xmlhave been parsed and a provider whose id matches the one in the descriptor is successfully loaded.- Specified by:
initin interfaceRoleMappingsProvider- Parameters:
deployment- a reference to the constructedSamlDeployment.loader- a reference to aResourceLoaderthat can be used to load additional resources from the WAR.config- aPropertiesobject containing the provider config as read fromkeycloak-saml.xml
-
map
public Set<String> map(String principalName, Set<String> roles)
Description copied from interface:RoleMappingsProviderProduces the final set of roles that should be assigned to the specified principal. This method makes the principal and roles that were read from the SAML assertion available to implementations so they can apply their specific logic to produce the final set of roles for the principal. This method imposes no restrictions on the kind of mappings that can be performed. A simple implementation may, for example, just use a properties file to map some of the assertion roles into JEE roles while a more complex implementation may also connect to external databases or LDAP servers to retrieve extra roles and add those roles to the set of roles already extracted from the assertion.- Specified by:
mapin interfaceRoleMappingsProvider- Parameters:
principalName- the principal name as extracted from the SAML assertion.roles- the set of roles extracted from the SAML assertion.- Returns:
- a
Setcontaining the final set of roles that are to be assigned to the principal.
-
-