add test cases
This commit is contained in:
parent
552b9f9fac
commit
dfb13e5397
|
|
@ -4,6 +4,7 @@ import org.jasig.cas.client.validation.Assertion;
|
|||
import org.jasig.cas.client.validation.Cas30ProxyTicketValidator;
|
||||
import org.jasig.cas.client.validation.TicketValidationException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -15,7 +16,7 @@ import java.util.List;
|
|||
public class Cas30JsonProxyTicketValidator extends Cas30ProxyTicketValidator {
|
||||
public Cas30JsonProxyTicketValidator(final String casServerUrlPrefix) {
|
||||
super(casServerUrlPrefix);
|
||||
getCustomParameters().put("format", "JSON");
|
||||
setCustomParameters(Collections.singletonMap("format", "JSON"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -33,9 +34,9 @@ public class Cas30JsonProxyTicketValidator extends Cas30ProxyTicketValidator {
|
|||
protected List<String> parseProxiesFromResponse(final String response) {
|
||||
try {
|
||||
final TicketValidationJsonResponse json = new JsonValidationResponseParser().parse(response);
|
||||
return json.getAuthenticationSuccess().getProxies();
|
||||
return json.getServiceResponse().getAuthenticationSuccess().getProxies();
|
||||
} catch (final Exception e) {
|
||||
logger.warn("Unable to locate proxies from the JSON response");
|
||||
logger.warn("Unable to locate proxies from the JSON response", e);
|
||||
return super.parseProxiesFromResponse(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class Cas30JsonServiceTicketValidator extends Cas30ServiceTicketValidator
|
|||
|
||||
public Cas30JsonServiceTicketValidator(final String casServerUrlPrefix) {
|
||||
super(casServerUrlPrefix);
|
||||
getCustomParameters().put("format", "JSON");
|
||||
setCustomParameters(Collections.singletonMap("format", "JSON"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -28,7 +28,7 @@ public class Cas30JsonServiceTicketValidator extends Cas30ServiceTicketValidator
|
|||
final TicketValidationJsonResponse json = new JsonValidationResponseParser().parse(response);
|
||||
return json.getAssertion(getProxyGrantingTicketStorage(), getProxyRetriever());
|
||||
} catch (final Exception e) {
|
||||
logger.warn("Unable parse the JSON response");
|
||||
logger.warn("Unable parse the JSON response", e);
|
||||
return super.parseResponseFromServer(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,20 +19,26 @@ final class JsonValidationResponseParser {
|
|||
|
||||
public TicketValidationJsonResponse parse(final String response) throws TicketValidationException {
|
||||
try {
|
||||
if (CommonUtils.isBlank(response)) {
|
||||
throw new TicketValidationException("Invalid JSON response; The response is empty");
|
||||
}
|
||||
|
||||
final TicketValidationJsonResponse json = this.objectMapper.readValue(response, TicketValidationJsonResponse.class);
|
||||
|
||||
if (json == null || json.getAuthenticationFailure() != null && json.getAuthenticationSuccess() != null) {
|
||||
throw new TicketValidationException("Invalid JSON response; either the response is empty or it indicates both a success "
|
||||
final TicketValidationJsonResponse.CasServiceResponseAuthentication serviceResponse = json.getServiceResponse();
|
||||
if (serviceResponse.getAuthenticationFailure() != null
|
||||
&& serviceResponse.getAuthenticationSuccess() != null) {
|
||||
throw new TicketValidationException("Invalid JSON response; It indicates both a success "
|
||||
+ "and a failure event, which is indicative of a server error. The actual response is " + response);
|
||||
}
|
||||
|
||||
if (json.getAuthenticationFailure() != null) {
|
||||
final String error = json.getAuthenticationFailure().getDescription()
|
||||
+ " - " + json.getAuthenticationFailure().getDescription();
|
||||
if (serviceResponse.getAuthenticationFailure() != null) {
|
||||
final String error = json.getServiceResponse().getAuthenticationFailure().getCode()
|
||||
+ " - " + serviceResponse.getAuthenticationFailure().getDescription();
|
||||
throw new TicketValidationException(error);
|
||||
}
|
||||
|
||||
final String principal = json.getAuthenticationSuccess().getUser();
|
||||
final String principal = json.getServiceResponse().getAuthenticationSuccess().getUser();
|
||||
if (CommonUtils.isEmpty(principal)) {
|
||||
throw new TicketValidationException("No principal was found in the response from the CAS server.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,28 +17,19 @@ import java.util.Map;
|
|||
* @author Misagh Moayyed
|
||||
*/
|
||||
final class TicketValidationJsonResponse {
|
||||
private CasServiceResponseAuthenticationFailure authenticationFailure;
|
||||
private CasServiceResponseAuthenticationSuccess authenticationSuccess;
|
||||
private CasServiceResponseAuthentication serviceResponse;
|
||||
|
||||
public CasServiceResponseAuthenticationFailure getAuthenticationFailure() {
|
||||
return this.authenticationFailure;
|
||||
public void setServiceResponse(final CasServiceResponseAuthentication serviceResponse) {
|
||||
this.serviceResponse = serviceResponse;
|
||||
}
|
||||
|
||||
public void setAuthenticationFailure(final CasServiceResponseAuthenticationFailure authenticationFailure) {
|
||||
this.authenticationFailure = authenticationFailure;
|
||||
}
|
||||
|
||||
public CasServiceResponseAuthenticationSuccess getAuthenticationSuccess() {
|
||||
return this.authenticationSuccess;
|
||||
}
|
||||
|
||||
public void setAuthenticationSuccess(final CasServiceResponseAuthenticationSuccess authenticationSuccess) {
|
||||
this.authenticationSuccess = authenticationSuccess;
|
||||
public CasServiceResponseAuthentication getServiceResponse() {
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
Assertion getAssertion(final ProxyGrantingTicketStorage proxyGrantingTicketStorage,
|
||||
final ProxyRetriever proxyRetriever) {
|
||||
final String proxyGrantingTicketIou = getAuthenticationSuccess().getProxyGrantingTicket();
|
||||
final String proxyGrantingTicketIou = getServiceResponse().getAuthenticationSuccess().getProxyGrantingTicket();
|
||||
final String proxyGrantingTicket;
|
||||
if (CommonUtils.isBlank(proxyGrantingTicketIou) || proxyGrantingTicketStorage == null) {
|
||||
proxyGrantingTicket = null;
|
||||
|
|
@ -47,8 +38,8 @@ final class TicketValidationJsonResponse {
|
|||
}
|
||||
|
||||
final Assertion assertion;
|
||||
final Map<String, Object> attributes = getAuthenticationSuccess().getAttributes();
|
||||
final String principal = getAuthenticationSuccess().getUser();
|
||||
final Map<String, Object> attributes = getServiceResponse().getAuthenticationSuccess().getAttributes();
|
||||
final String principal = getServiceResponse().getAuthenticationSuccess().getUser();
|
||||
if (CommonUtils.isNotBlank(proxyGrantingTicket)) {
|
||||
final AttributePrincipal attributePrincipal = new AttributePrincipalImpl(principal, attributes,
|
||||
proxyGrantingTicket, proxyRetriever);
|
||||
|
|
@ -59,6 +50,26 @@ final class TicketValidationJsonResponse {
|
|||
return assertion;
|
||||
}
|
||||
|
||||
static class CasServiceResponseAuthentication {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static class CasServiceResponseAuthenticationSuccess {
|
||||
private String user;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
package org.jasig.cas.client.validation.json;
|
||||
|
||||
import org.jasig.cas.client.PublicTestHttpServer;
|
||||
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
|
||||
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
|
||||
import org.jasig.cas.client.proxy.ProxyRetriever;
|
||||
import org.jasig.cas.client.validation.AbstractTicketValidatorTests;
|
||||
import org.jasig.cas.client.validation.Assertion;
|
||||
import org.jasig.cas.client.validation.TicketValidationException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import junit.framework.Assert;
|
||||
|
||||
public class Cas30JsonServiceTicketValidatorTests extends AbstractTicketValidatorTests {
|
||||
private static final PublicTestHttpServer server = PublicTestHttpServer.instance(8088);
|
||||
private ProxyGrantingTicketStorage proxyGrantingTicketStorage;
|
||||
|
||||
private Cas30JsonServiceTicketValidator ticketValidator;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.proxyGrantingTicketStorage = getProxyGrantingTicketStorage();
|
||||
this.ticketValidator = new Cas30JsonServiceTicketValidator(CONST_CAS_SERVER_URL_PREFIX + "8088");
|
||||
this.ticketValidator.setProxyCallbackUrl("test");
|
||||
this.ticketValidator.setProxyGrantingTicketStorage(getProxyGrantingTicketStorage());
|
||||
this.ticketValidator.setProxyRetriever(getProxyRetriever());
|
||||
this.ticketValidator.setRenew(true);
|
||||
}
|
||||
|
||||
private ProxyGrantingTicketStorage getProxyGrantingTicketStorage() {
|
||||
return new ProxyGrantingTicketStorageImpl();
|
||||
}
|
||||
|
||||
private ProxyRetriever getProxyRetriever() {
|
||||
return new ProxyRetriever() {
|
||||
|
||||
/** Unique Id for serialization. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public String getProxyTicketIdFor(final String proxyGrantingTicketId, final String targetService) {
|
||||
return "test";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessfulJsonResponse() throws Exception {
|
||||
final String RESPONSE = "{ " +
|
||||
"\"serviceResponse\" : { " +
|
||||
"\"authenticationSuccess\" : { " +
|
||||
"\"user\" : \"casuser\", " +
|
||||
"\"proxyGrantingTicket\" : \"PGTIOU-84678-8a9d\" ," +
|
||||
"\"attributes\" : { " +
|
||||
"\"cn\" : [ \"Name\" ] " +
|
||||
'}' +
|
||||
'}' +
|
||||
'}' +
|
||||
'}';
|
||||
|
||||
server.content = RESPONSE.getBytes(server.encoding);
|
||||
final Assertion assertion = ticketValidator.validate("test", "test");
|
||||
Assert.assertEquals(assertion.getPrincipal().getName(), "casuser");
|
||||
Assert.assertTrue(assertion.getPrincipal().getAttributes().containsKey("cn"));
|
||||
}
|
||||
|
||||
@Test(expected = TicketValidationException.class)
|
||||
public void testFailingJsonResponse() throws Exception {
|
||||
final String RESPONSE = "{ " +
|
||||
"\"serviceResponse\" : { " +
|
||||
"\"authenticationFailure\" : { " +
|
||||
"\"code\" : \"INVALID_TICKET\", " +
|
||||
"\"description\" : \"Description\" " +
|
||||
'}' +
|
||||
'}' +
|
||||
'}';
|
||||
|
||||
server.content = RESPONSE.getBytes(server.encoding);
|
||||
ticketValidator.validate("test", "test");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSuccessfulXmlResponseWithJson() throws Exception {
|
||||
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>"
|
||||
+ "test</cas:user><cas:proxyGrantingTicket>PGTIOU</cas:proxyGrantingTicket></cas:authenticationSuccess></cas:serviceResponse>";
|
||||
server.content = RESPONSE.getBytes(server.encoding);
|
||||
ticketValidator.validate("test", "test");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue