src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java
@@ -7,7 +7,7 @@ import org.keycloak.models.UserSessionModel; import org.keycloak.protocol.ProtocolMapper; import org.keycloak.protocol.cas.mappers.CASAttributeMapper; import org.keycloak.protocol.cas.representations.CasServiceResponse; import org.keycloak.protocol.cas.representations.CASServiceResponse; import org.keycloak.protocol.cas.utils.CASValidationException; import org.keycloak.protocol.cas.utils.ContentTypeHelper; import org.keycloak.protocol.cas.utils.ServiceResponseHelper; @@ -40,17 +40,17 @@ } } CasServiceResponse serviceResponse = ServiceResponseHelper.createSuccess(userSession.getUser().getUsername(), attributes); CASServiceResponse serviceResponse = ServiceResponseHelper.createSuccess(userSession.getUser().getUsername(), attributes); return prepare(Response.Status.OK, serviceResponse); } @Override protected Response errorResponse(CASValidationException e) { CasServiceResponse serviceResponse = ServiceResponseHelper.createFailure(e.getError(), e.getErrorDescription()); CASServiceResponse serviceResponse = ServiceResponseHelper.createFailure(e.getError(), e.getErrorDescription()); return prepare(e.getStatus(), serviceResponse); } private Response prepare(Response.Status status, CasServiceResponse serviceResponse) { private Response prepare(Response.Status status, CASServiceResponse serviceResponse) { MediaType responseMediaType = new ContentTypeHelper(request, restRequest, uriInfo).selectResponseType(); return ServiceResponseHelper.createResponse(status, responseMediaType, serviceResponse); } src/main/java/org/keycloak/protocol/cas/representations/CASServiceResponse.java
New file @@ -0,0 +1,25 @@ package org.keycloak.protocol.cas.representations; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "serviceResponse") public class CASServiceResponse { private CASServiceResponseAuthenticationFailure authenticationFailure; private CASServiceResponseAuthenticationSuccess authenticationSuccess; public CASServiceResponseAuthenticationFailure getAuthenticationFailure() { return this.authenticationFailure; } public void setAuthenticationFailure(final CASServiceResponseAuthenticationFailure authenticationFailure) { this.authenticationFailure = authenticationFailure; } public CASServiceResponseAuthenticationSuccess getAuthenticationSuccess() { return this.authenticationSuccess; } public void setAuthenticationSuccess(final CASServiceResponseAuthenticationSuccess authenticationSuccess) { this.authenticationSuccess = authenticationSuccess; } } src/main/java/org/keycloak/protocol/cas/representations/CASServiceResponseAuthenticationFailure.java
File was renamed from src/main/java/org/keycloak/protocol/cas/representations/CasServiceResponseAuthenticationFailure.java @@ -6,7 +6,7 @@ import javax.xml.bind.annotation.XmlValue; @XmlAccessorType(XmlAccessType.FIELD) public class CasServiceResponseAuthenticationFailure { public class CASServiceResponseAuthenticationFailure { @XmlAttribute private String code; @XmlValue src/main/java/org/keycloak/protocol/cas/representations/CASServiceResponseAuthenticationSuccess.java
File was renamed from src/main/java/org/keycloak/protocol/cas/representations/CasServiceResponseAuthenticationSuccess.java @@ -8,7 +8,7 @@ import java.util.Map; @XmlAccessorType(XmlAccessType.FIELD) public class CasServiceResponseAuthenticationSuccess { public class CASServiceResponseAuthenticationSuccess { private String user; private String proxyGrantingTicket; @XmlElementWrapper src/main/java/org/keycloak/protocol/cas/representations/CasServiceResponse.java
File was deleted src/main/java/org/keycloak/protocol/cas/utils/AttributesMapAdapter.java
@@ -1,6 +1,6 @@ package org.keycloak.protocol.cas.utils; import org.keycloak.protocol.cas.representations.CasServiceResponse; import org.keycloak.protocol.cas.representations.CASServiceResponse; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; @@ -50,7 +50,7 @@ private void addElement(String name, Object value) { if (value != null) { String namespace = CasServiceResponse.class.getPackage().getAnnotation(XmlSchema.class).namespace(); String namespace = CASServiceResponse.class.getPackage().getAnnotation(XmlSchema.class).namespace(); elements.add(new JAXBElement<>(new QName(namespace, name), String.class, value.toString())); } } src/main/java/org/keycloak/protocol/cas/utils/ServiceResponseHelper.java
@@ -1,9 +1,9 @@ package org.keycloak.protocol.cas.utils; import org.keycloak.protocol.cas.representations.CASErrorCode; import org.keycloak.protocol.cas.representations.CasServiceResponse; import org.keycloak.protocol.cas.representations.CasServiceResponseAuthenticationFailure; import org.keycloak.protocol.cas.representations.CasServiceResponseAuthenticationSuccess; import org.keycloak.protocol.cas.representations.CASServiceResponse; import org.keycloak.protocol.cas.representations.CASServiceResponseAuthenticationFailure; import org.keycloak.protocol.cas.representations.CASServiceResponseAuthenticationSuccess; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @@ -15,14 +15,14 @@ private ServiceResponseHelper() { } public static CasServiceResponse createSuccess(String username, Map<String, Object> attributes) { public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes) { return createSuccess(username, attributes, null, null); } public static CasServiceResponse createSuccess(String username, Map<String, Object> attributes, public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes, String proxyGrantingTicket, List<String> proxies) { CasServiceResponse response = new CasServiceResponse(); CasServiceResponseAuthenticationSuccess success = new CasServiceResponseAuthenticationSuccess(); CASServiceResponse response = new CASServiceResponse(); CASServiceResponseAuthenticationSuccess success = new CASServiceResponseAuthenticationSuccess(); success.setUser(username); success.setProxies(proxies); success.setProxyGrantingTicket(proxyGrantingTicket); @@ -33,9 +33,9 @@ return response; } public static CasServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) { CasServiceResponse response = new CasServiceResponse(); CasServiceResponseAuthenticationFailure failure = new CasServiceResponseAuthenticationFailure(); public static CASServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) { CASServiceResponse response = new CASServiceResponse(); CASServiceResponseAuthenticationFailure failure = new CASServiceResponseAuthenticationFailure(); failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name()); failure.setDescription(errorDescription); response.setAuthenticationFailure(failure); @@ -43,7 +43,7 @@ return response; } public static Response createResponse(Response.Status status, MediaType mediaType, CasServiceResponse serviceResponse) { public static Response createResponse(Response.Status status, MediaType mediaType, CASServiceResponse serviceResponse) { Response.ResponseBuilder builder = Response.status(status) .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8")); if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) { src/main/java/org/keycloak/protocol/cas/utils/ServiceResponseMarshaller.java
@@ -5,7 +5,7 @@ import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; import org.keycloak.protocol.cas.representations.CasServiceResponse; import org.keycloak.protocol.cas.representations.CASServiceResponse; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -23,9 +23,9 @@ private ServiceResponseMarshaller() { } public static String marshalXml(CasServiceResponse serviceResponse) { public static String marshalXml(CASServiceResponse serviceResponse) { try { JAXBContext jaxbContext = JAXBContext.newInstance(CasServiceResponse.class); JAXBContext jaxbContext = JAXBContext.newInstance(CASServiceResponse.class); Marshaller marshaller = jaxbContext.createMarshaller(); //disable xml header marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); @@ -39,7 +39,7 @@ } } public static String marshalJson(CasServiceResponse serviceResponse) { public static String marshalJson(CASServiceResponse serviceResponse) { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //Force newlines to be LF (default is system dependent) src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java
@@ -2,7 +2,7 @@ import org.junit.Test; import org.keycloak.protocol.cas.representations.CASErrorCode; import org.keycloak.protocol.cas.representations.CasServiceResponse; import org.keycloak.protocol.cas.representations.CASServiceResponse; import org.keycloak.protocol.cas.utils.ServiceResponseHelper; import org.keycloak.protocol.cas.utils.ServiceResponseMarshaller; @@ -62,7 +62,7 @@ attributes.put("int", 123); attributes.put("string", "abc"); CasServiceResponse response = ServiceResponseHelper.createSuccess("username", attributes, "PGTIOU-test", CASServiceResponse response = ServiceResponseHelper.createSuccess("username", attributes, "PGTIOU-test", Arrays.asList("https://proxy1/pgtUrl", "https://proxy2/pgtUrl")); assertEquals(EXPECTED_JSON_SUCCESS, ServiceResponseMarshaller.marshalJson(response)); @@ -71,7 +71,7 @@ @Test public void testErrorResponse() throws Exception { CasServiceResponse response = ServiceResponseHelper.createFailure(CASErrorCode.INVALID_REQUEST, "Error description"); CASServiceResponse response = ServiceResponseHelper.createFailure(CASErrorCode.INVALID_REQUEST, "Error description"); assertEquals(EXPECTED_JSON_FAILURE, ServiceResponseMarshaller.marshalJson(response)); assertEquals(EXPECTED_XML_FAILURE, ServiceResponseMarshaller.marshalXml(response));