Search in sources :

Example 1 with GenericOAuth20ProfileDefinition

use of org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition in project pac4j by pac4j.

the class GenericOAuth20ClientTest method setProfileAttrs.

@Test
public void setProfileAttrs() throws Exception {
    GenericOAuth20Client client = new GenericOAuth20Client();
    Map map = new HashMap();
    map.put(AGE, "Integer|age");
    // map.put("creation_time", "Date:|creation_time");
    map.put(IS_ADMIN, "Boolean|is_admin");
    map.put(BG_COLOR, "Color|bg_color");
    map.put(GENDER, "Gender|gender");
    map.put(BIRTHDAY, "Locale|birthday");
    map.put(ID, "Long|id");
    map.put(BLOG, "URI|blog");
    client.setProfileAttrs(map);
    client.setCallbackUrl(CALLBACK_URL);
    client.init();
    Field configurationField = OAuth20Client.class.getDeclaredField("configuration");
    configurationField.setAccessible(true);
    OAuth20Configuration configuration = (OAuth20Configuration) configurationField.get(client);
    GenericOAuth20ProfileDefinition profileDefinition = (GenericOAuth20ProfileDefinition) configuration.getProfileDefinition();
    Method getConverters = ProfileDefinition.class.getDeclaredMethod("getConverters");
    getConverters.setAccessible(true);
    Map<String, AttributeConverter<?>> converters = (Map<String, AttributeConverter<?>>) getConverters.invoke(profileDefinition);
    assertTrue(converters.get(AGE) instanceof IntegerConverter);
    assertTrue(converters.get(IS_ADMIN) instanceof BooleanConverter);
    assertTrue(converters.get(BG_COLOR) instanceof ColorConverter);
    assertTrue(converters.get(GENDER) instanceof GenderConverter);
    assertTrue(converters.get(BIRTHDAY) instanceof LocaleConverter);
    assertTrue(converters.get(ID) instanceof LongConverter);
    assertTrue(converters.get(BLOG) instanceof UrlConverter);
}
Also used : HashMap(java.util.HashMap) GenericOAuth20ProfileDefinition(org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Map(java.util.Map) HashMap(java.util.HashMap) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration) Test(org.junit.Test)

Example 2 with GenericOAuth20ProfileDefinition

use of org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition in project pac4j by pac4j.

the class GenericOAuth20Client method clientInit.

@Override
protected void clientInit() {
    LOG.info("InternalInit");
    GenericApi20 genApi = new GenericApi20(authUrl, tokenUrl);
    configuration.setApi(genApi);
    configuration.setCustomParams(customParams);
    GenericOAuth20ProfileDefinition profileDefinition = new GenericOAuth20ProfileDefinition();
    profileDefinition.setFirstNodePath(profilePath);
    profileDefinition.setProfileVerb(profileVerb);
    profileDefinition.setProfileUrl(profileUrl);
    if (profileAttrs != null) {
        for (Map.Entry<String, String> entry : profileAttrs.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            String[] tokens = value.split("\\|");
            if (tokens.length == 2) {
                profileDefinition.profileAttribute(key, tokens[1], getConverter(tokens[0]));
            } else if (tokens.length == 1) {
                profileDefinition.profileAttribute(key, value, null);
            } else {
                LOG.warning("Ignored incorrect attribute value expressions:" + value);
            }
        }
    }
    configuration.setProfileDefinition(profileDefinition);
    super.clientInit();
}
Also used : GenericOAuth20ProfileDefinition(org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition) GenericApi20(org.pac4j.scribe.builder.api.GenericApi20) Map(java.util.Map)

Aggregations

Map (java.util.Map)2 GenericOAuth20ProfileDefinition (org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 OAuth20Configuration (org.pac4j.oauth.config.OAuth20Configuration)1 GenericApi20 (org.pac4j.scribe.builder.api.GenericApi20)1