Skip to content

Commit 7eff059

Browse files
committed
Refactored and added unit test.
1 parent 173fa91 commit 7eff059

3 files changed

Lines changed: 214 additions & 8 deletions

File tree

cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas30ServiceTicketValidator.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.w3c.dom.Node;
2525
import org.w3c.dom.NodeList;
2626

27+
import java.util.ArrayList;
2728
import java.util.HashMap;
29+
import java.util.LinkedList;
30+
import java.util.List;
2831
import java.util.Map;
2932

3033
/**
@@ -54,21 +57,35 @@ protected String getUrlSuffix() {
5457
@Override
5558
protected Map<String, Object> extractCustomAttributes(String xml) {
5659
final Document document = XmlUtils.newDocument(xml);
57-
final HashMap<String, Object> attributes = new HashMap<String, Object>();
5860

59-
NodeList attributeList = document.getElementsByTagName("cas:attribute");
61+
// Check if attributes are inlined. If not return default super method results
62+
final NodeList attributeList = document.getElementsByTagName("cas:attribute");
63+
if (attributeList.getLength() == 0) {
64+
return super.extractCustomAttributes(xml);
65+
}
66+
67+
final HashMap<String, Object> attributes = new HashMap<String, Object>();
6068

6169
for (int i = 0; i < attributeList.getLength(); i++) {
6270
final Node casAttributeNode = attributeList.item(i);
63-
final NamedNodeMap casAttributes = casAttributeNode.getAttributes();
64-
if (casAttributes.getLength() > 0) {
65-
attributes.put(casAttributes.getNamedItem("name").getNodeValue(),
66-
casAttributes.getNamedItem("value").getNodeValue());
71+
final NamedNodeMap nodeAttributes = casAttributeNode.getAttributes();
72+
final String name = nodeAttributes.getNamedItem("name").getNodeValue();
73+
final String value = nodeAttributes.getNamedItem("value").getTextContent();
74+
final Object mapValue = attributes.get(name);
75+
if (mapValue != null) {
76+
if (mapValue instanceof List) {
77+
((List) mapValue).add(value);
78+
} else {
79+
final LinkedList<Object> list = new LinkedList<Object>();
80+
list.add(mapValue);
81+
list.add(value);
82+
attributes.put(name, list);
83+
}
6784
} else {
68-
attributes.put(casAttributeNode.getLocalName(), casAttributeNode.getNodeValue());
85+
attributes.put(name, value);
6986
}
7087
}
71-
7288
return attributes;
7389
}
90+
7491
}

cas-client-core/src/test/java/org/jasig/cas/client/validation/Cas20ServiceTicketValidatorTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public void testGetAttributes() throws TicketValidationException, UnsupportedEnc
146146
//assertEquals(PGT, assertion.getProxyGrantingTicketId());
147147
}
148148

149+
150+
149151
@Test
150152
public void testInvalidResponse() throws Exception {
151153
final String RESPONSE = "<root />";
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Licensed to Jasig under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work
4+
* for additional information regarding copyright ownership.
5+
* Jasig licenses this file to you under the Apache License,
6+
* Version 2.0 (the "License"); you may not use this file
7+
* except in compliance with the License. You may obtain a
8+
* copy of the License at the following location:
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jasig.cas.client.validation;
20+
21+
import org.jasig.cas.client.PublicTestHttpServer;
22+
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
23+
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
24+
import org.jasig.cas.client.proxy.ProxyRetriever;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
import java.io.UnsupportedEncodingException;
29+
import java.util.List;
30+
31+
import static org.junit.Assert.*;
32+
33+
/**
34+
* Test cases for the {@link Cas20ServiceTicketValidator}.
35+
*
36+
* @author Scott Battaglia
37+
* @version $Revision: 11737 $ $Date: 2007-10-03 09:14:02 -0400 (Tue, 03 Oct 2007) $
38+
* @since 3.0
39+
*/
40+
public final class Cas30ServiceTicketValidatorTests extends AbstractTicketValidatorTests {
41+
42+
private static final PublicTestHttpServer server = PublicTestHttpServer.instance(8088);
43+
44+
private Cas30ServiceTicketValidator ticketValidator;
45+
46+
private ProxyGrantingTicketStorage proxyGrantingTicketStorage;
47+
48+
public Cas30ServiceTicketValidatorTests() {
49+
super();
50+
}
51+
52+
/*@AfterClass
53+
public static void classCleanUp() {
54+
server.shutdown();
55+
} */
56+
57+
@Before
58+
public void setUp() throws Exception {
59+
this.proxyGrantingTicketStorage = getProxyGrantingTicketStorage();
60+
this.ticketValidator = new Cas30ServiceTicketValidator(CONST_CAS_SERVER_URL_PREFIX + "8088");
61+
this.ticketValidator.setProxyCallbackUrl("test");
62+
this.ticketValidator.setProxyGrantingTicketStorage(getProxyGrantingTicketStorage());
63+
this.ticketValidator.setProxyRetriever(getProxyRetriever());
64+
this.ticketValidator.setRenew(true);
65+
}
66+
67+
private ProxyGrantingTicketStorage getProxyGrantingTicketStorage() {
68+
return new ProxyGrantingTicketStorageImpl();
69+
}
70+
71+
private ProxyRetriever getProxyRetriever() {
72+
return new ProxyRetriever() {
73+
74+
/** Unique Id for serialization. */
75+
private static final long serialVersionUID = 1L;
76+
77+
public String getProxyTicketIdFor(String proxyGrantingTicketId, String targetService) {
78+
return "test";
79+
}
80+
};
81+
}
82+
83+
@Test
84+
public void testNoResponse() throws UnsupportedEncodingException {
85+
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationFailure code=\"INVALID_TICKET\">Ticket ST-1856339-aA5Yuvrxzpv8Tau1cYQ7 not recognized</cas:authenticationFailure></cas:serviceResponse>";
86+
server.content = RESPONSE.getBytes(server.encoding);
87+
try {
88+
this.ticketValidator.validate("test", "test");
89+
fail("ValidationException expected due to 'no' response");
90+
} catch (final TicketValidationException e) {
91+
// expected
92+
}
93+
}
94+
95+
@Test
96+
public void testYesResponseButNoPgt() throws TicketValidationException, UnsupportedEncodingException {
97+
final String USERNAME = "username";
98+
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>"
99+
+ USERNAME + "</cas:user></cas:authenticationSuccess></cas:serviceResponse>";
100+
server.content = RESPONSE.getBytes(server.encoding);
101+
102+
final Assertion assertion = this.ticketValidator.validate("test", "test");
103+
assertEquals(USERNAME, assertion.getPrincipal().getName());
104+
105+
}
106+
107+
@Test
108+
public void testYesResponseWithPgt() throws TicketValidationException, UnsupportedEncodingException {
109+
final String USERNAME = "username";
110+
final String PGTIOU = "testPgtIou";
111+
final String PGT = "test";
112+
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>"
113+
+ USERNAME
114+
+ "</cas:user><cas:proxyGrantingTicket>"
115+
+ PGTIOU
116+
+ "</cas:proxyGrantingTicket></cas:authenticationSuccess></cas:serviceResponse>";
117+
118+
server.content = RESPONSE.getBytes(server.encoding);
119+
this.proxyGrantingTicketStorage.save(PGTIOU, PGT);
120+
121+
final Assertion assertion = this.ticketValidator.validate("test", "test");
122+
assertEquals(USERNAME, assertion.getPrincipal().getName());
123+
// assertEquals(PGT, assertion.getProxyGrantingTicketId());
124+
}
125+
126+
@Test
127+
public void testGetAttributes() throws TicketValidationException, UnsupportedEncodingException {
128+
final String USERNAME = "username";
129+
final String PGTIOU = "testPgtIou";
130+
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>"
131+
+ USERNAME
132+
+ "</cas:user><cas:proxyGrantingTicket>"
133+
+ PGTIOU
134+
+ "</cas:proxyGrantingTicket><cas:attributes><cas:password>test</cas:password><cas:eduPersonId>id</cas:eduPersonId><cas:longAttribute>test1\n\ntest</cas:longAttribute><cas:multivaluedAttribute>value1</cas:multivaluedAttribute><cas:multivaluedAttribute>value2</cas:multivaluedAttribute></cas:attributes></cas:authenticationSuccess></cas:serviceResponse>";
135+
136+
server.content = RESPONSE.getBytes(server.encoding);
137+
final Assertion assertion = this.ticketValidator.validate("test", "test");
138+
assertEquals(USERNAME, assertion.getPrincipal().getName());
139+
assertEquals("test", assertion.getPrincipal().getAttributes().get("password"));
140+
assertEquals("id", assertion.getPrincipal().getAttributes().get("eduPersonId"));
141+
assertEquals("test1\n\ntest", assertion.getPrincipal().getAttributes().get("longAttribute"));
142+
try {
143+
List<?> multivalued = (List<?>) assertion.getPrincipal().getAttributes().get("multivaluedAttribute");
144+
assertArrayEquals(new String[] { "value1", "value2" }, multivalued.toArray());
145+
} catch (Exception e) {
146+
fail("'multivaluedAttribute' attribute expected as List<Object> object.");
147+
}
148+
//assertEquals(PGT, assertion.getProxyGrantingTicketId());
149+
}
150+
151+
@Test
152+
public void testGetInlinedAttributes() throws TicketValidationException, UnsupportedEncodingException {
153+
final String USERNAME = "username";
154+
final String PGTIOU = "testPgtIou";
155+
final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>"
156+
+ USERNAME
157+
+ "</cas:user><cas:proxyGrantingTicket>"
158+
+ PGTIOU
159+
+ "</cas:proxyGrantingTicket><cas:attributes><cas:attribute name=\"password\" value=\"test\"/><cas:attribute name=\"eduPersonId\" value=\"id\"/><cas:attribute name=\"longAttribute\" value=\"test1&#10;&#10;test\"/><cas:attribute name=\"multivaluedAttribute\" value=\"value1\"/><cas:attribute name=\"multivaluedAttribute\" value=\"value2\"/></cas:attributes></cas:authenticationSuccess></cas:serviceResponse>";
160+
161+
server.content = RESPONSE.getBytes(server.encoding);
162+
final Assertion assertion = this.ticketValidator.validate("test", "test");
163+
assertEquals(USERNAME, assertion.getPrincipal().getName());
164+
assertEquals("test", assertion.getPrincipal().getAttributes().get("password"));
165+
assertEquals("id", assertion.getPrincipal().getAttributes().get("eduPersonId"));
166+
assertEquals("test1\n\ntest", assertion.getPrincipal().getAttributes().get("longAttribute"));
167+
try {
168+
List<?> multivalued = (List<?>) assertion.getPrincipal().getAttributes().get("multivaluedAttribute");
169+
assertArrayEquals(new String[] { "value1", "value2" }, multivalued.toArray());
170+
} catch (Exception e) {
171+
fail("'multivaluedAttribute' attribute expected as List<Object> object.");
172+
}
173+
//assertEquals(PGT, assertion.getProxyGrantingTicketId());
174+
}
175+
176+
@Test
177+
public void testInvalidResponse() throws Exception {
178+
final String RESPONSE = "<root />";
179+
server.content = RESPONSE.getBytes(server.encoding);
180+
try {
181+
this.ticketValidator.validate("test", "test");
182+
fail("ValidationException expected due to invalid response.");
183+
} catch (final TicketValidationException e) {
184+
// expected
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)