package org.keycloak.protocol; import org.keycloak.models.KeycloakSession; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.UserModel; import org.keycloak.protocol.oidc.OIDCLoginProtocol; import org.keycloak.protocol.oidc.OIDCLoginProtocolFactory; import org.keycloak.provider.ProviderFactory; import java.lang.reflect.Method; /** * @author Bill Burke * @version $Revision: 1 $ */ public class ProtocolMapperUtils { public static final String USER_ATTRIBUTE = "user.attribute"; public static final String USER_SESSION_NOTE = "user.session.note"; public static final String MULTIVALUED = "multivalued"; public static final String USER_MODEL_PROPERTY_LABEL = "usermodel.prop.label"; public static final String USER_MODEL_PROPERTY_HELP_TEXT = "usermodel.prop.tooltip"; public static final String USER_MODEL_ATTRIBUTE_LABEL = "usermodel.attr.label"; public static final String USER_MODEL_ATTRIBUTE_HELP_TEXT = "usermodel.attr.tooltip"; public static final String USER_SESSION_MODEL_NOTE_LABEL = "userSession.modelNote.label"; public static final String USER_SESSION_MODEL_NOTE_HELP_TEXT = "userSession.modelNote.tooltip"; public static final String MULTIVALUED_LABEL = "multivalued.label"; public static final String MULTIVALUED_HELP_TEXT = "multivalued.tooltip"; public static String getUserModelValue(UserModel user, String propertyName) { String methodName = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); try { Method method = UserModel.class.getMethod(methodName); Object val = method.invoke(user); if (val != null) return val.toString(); } catch (Exception ignore) { } methodName = "is" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); try { Method method = UserModel.class.getMethod(methodName); Object val = method.invoke(user); if (val != null) return val.toString(); } catch (Exception ignore) { } return null; } /** * Find the builtin locale mapper. * * @param session A KeycloakSession * @return The builtin locale mapper. */ public static ProtocolMapperModel findLocaleMapper(KeycloakSession session) { ProtocolMapperModel found = null; for (ProviderFactory p : session.getKeycloakSessionFactory().getProviderFactories(LoginProtocol.class)) { LoginProtocolFactory factory = (LoginProtocolFactory) p; for (ProtocolMapperModel mapper : factory.getBuiltinMappers()) { if (mapper.getName().equals(OIDCLoginProtocolFactory.LOCALE) && mapper.getProtocol().equals(OIDCLoginProtocol.LOGIN_PROTOCOL)) { found = mapper; break; } } if (found != null) break; } return found; } }