2015-02-28 16:17:06 +00:00
package org.keycloak.protocol ;
2015-03-08 00:47:34 +00:00
import org.keycloak.models.KeycloakSessionFactory ;
import org.keycloak.models.ProtocolMapperModel ;
2015-02-28 16:17:06 +00:00
import org.keycloak.models.UserModel ;
2015-03-11 13:59:56 +00:00
import org.keycloak.representations.AccessToken ;
2015-02-28 16:17:06 +00:00
import java.lang.reflect.Method ;
2015-03-08 00:47:34 +00:00
import java.util.List ;
2015-02-28 16:17:06 +00:00
/ * *
* @author < a href = " mailto:bill@burkecentral.com " > Bill Burke < / a >
* @version $Revision : 1 $
* /
public class ProtocolMapperUtils {
2015-03-01 00:48:50 +00:00
public static final String USER_ATTRIBUTE = " user.attribute " ;
2015-03-04 19:02:53 +00:00
public static final String USER_SESSION_NOTE = " user.session.note " ;
2015-03-01 00:48:50 +00:00
public static final String USER_MODEL_PROPERTY_LABEL = " User Property " ;
2015-02-28 16:17:06 +00:00
public static final String USER_MODEL_PROPERTY_HELP_TEXT = " Name of the property method in the UserModel interface. For example, a value of 'email' would reference the UserModel.getEmail() method. " ;
2015-03-01 00:48:50 +00:00
public static final String USER_MODEL_ATTRIBUTE_LABEL = " User Attribute " ;
2015-02-28 16:17:06 +00:00
public static final String USER_MODEL_ATTRIBUTE_HELP_TEXT = " Name of stored user attribute which is the name of an attribute within the UserModel.attribute map. " ;
2015-03-04 19:02:53 +00:00
public static final String USER_SESSION_MODEL_NOTE_LABEL = " User Session Note " ;
public static final String USER_SESSION_MODEL_NOTE_HELP_TEXT = " Name of stored user session note within the UserSessionModel.note map. " ;
2015-02-28 16:17:06 +00:00
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 ;
}
2015-03-11 13:59:56 +00:00
public static String [ ] parseRole ( String role ) {
int scopeIndex = role . indexOf ( '.' ) ;
if ( scopeIndex > - 1 ) {
String appName = role . substring ( 0 , scopeIndex ) ;
role = role . substring ( scopeIndex + 1 ) ;
String [ ] rtn = { appName , role } ;
return rtn ;
} else {
String [ ] rtn = { null , role } ;
return rtn ;
}
}
2015-02-28 16:17:06 +00:00
}