Search in sources :

Example 1 with CasOAuthWrapperProfile

use of org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile in project pac4j by pac4j.

the class CasOAuthWrapperClientTests method testParsingAttributesCASServerV4_2AndBefore.

@Test
public void testParsingAttributesCASServerV4_2AndBefore() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(KEY, VALUE);
    attributes.put(NAME, TOKEN);
    final StringWriter writer = new StringWriter();
    try (final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer)) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("id", ID);
        jsonGenerator.writeArrayFieldStart("attributes");
        for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeObjectField(entry.getKey(), entry.getValue());
            jsonGenerator.writeEndObject();
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
    }
    final String body = writer.toString();
    final CasOAuthWrapperClient client = new CasOAuthWrapperClient();
    client.setKey(KEY);
    client.setSecret(SECRET);
    client.setCasOAuthUrl(CALLBACK_URL);
    client.setCallbackUrl(CALLBACK_URL);
    client.init();
    final CasOAuthWrapperProfile profile = new CasOAuthWrapperProfileDefinition().extractUserProfile(body);
    assertEquals(ID, profile.getId());
    assertEquals(2, profile.getAttributes().size());
    assertEquals(VALUE, profile.getAttribute(KEY));
    assertEquals(TOKEN, profile.getAttribute(NAME));
}
Also used : CasOAuthWrapperProfileDefinition(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfileDefinition) HashMap(java.util.HashMap) JsonFactory(com.fasterxml.jackson.core.JsonFactory) CasOAuthWrapperProfile(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with CasOAuthWrapperProfile

use of org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile in project pac4j by pac4j.

the class CasOAuthWrapperClientTests method testParsingAttributesCASServerV5.

@Test
public void testParsingAttributesCASServerV5() throws IOException {
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(KEY, VALUE);
    attributes.put(NAME, TOKEN);
    final Map<String, Object> map = new HashMap<>();
    map.put("id", ID);
    map.put("attributes", attributes);
    final String body = new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(map);
    final CasOAuthWrapperClient client = new CasOAuthWrapperClient();
    client.setKey(KEY);
    client.setSecret(SECRET);
    client.setCasOAuthUrl(CALLBACK_URL);
    client.setCallbackUrl(CALLBACK_URL);
    client.init();
    final CasOAuthWrapperProfile profile = new CasOAuthWrapperProfileDefinition().extractUserProfile(body);
    assertEquals(ID, profile.getId());
    assertEquals(2, profile.getAttributes().size());
    assertEquals(VALUE, profile.getAttribute(KEY));
    assertEquals(TOKEN, profile.getAttribute(NAME));
}
Also used : CasOAuthWrapperProfile(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile) CasOAuthWrapperProfileDefinition(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfileDefinition) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with CasOAuthWrapperProfile

use of org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile in project pac4j by pac4j.

the class RunCasOAuthWrapperClient method verifyProfile.

@Override
protected void verifyProfile(CommonProfile userProfile) {
    final CasOAuthWrapperProfile profile = (CasOAuthWrapperProfile) userProfile;
    assertEquals(getLogin(), profile.getId());
    assertEquals(CasOAuthWrapperProfile.class.getName() + CommonProfile.SEPARATOR + getLogin(), profile.getTypedId());
    assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CasOAuthWrapperProfile.class));
    assertTrue(CommonHelper.isNotBlank(profile.getAccessToken()));
    assertTrue(profile.isFromNewLogin());
    assertEquals("AcceptUsersAuthenticationHandler", profile.getAuthenticationMethod());
    assertFalse(profile.isLongTermAuthenticationRequestTokenUsed());
    assertEquals("AcceptUsersAuthenticationHandler", profile.getSuccessfulAuthenticationHandlers());
    assertNotNull(profile.getAuthenticationDate());
    assertTrue(profile.isFromNewLogin());
    assertEquals("groupMembership", profile.getAttribute("affiliation"));
    assertEquals("commonName", profile.getAttribute("cn"));
    assertEquals("uid", profile.getAttribute("uid"));
    assertEquals(11, profile.getAttributes().size());
}
Also used : CasOAuthWrapperProfile(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile)

Aggregations

CasOAuthWrapperProfile (org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 CasOAuthWrapperProfileDefinition (org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfileDefinition)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1