Skip to content

Commit 080c66c

Browse files
committed
Merge pull request apereo#153 from serac/jetty
CAS Jetty Container Authentication Support
2 parents 30d2e5d + 74859cb commit 080c66c

12 files changed

Lines changed: 760 additions & 0 deletions

File tree

cas-client-core/src/main/java/org/jasig/cas/client/util/ReflectUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.beans.IntrospectionException;
2323
import java.beans.Introspector;
2424
import java.beans.PropertyDescriptor;
25+
import java.lang.reflect.Field;
2526
import java.lang.reflect.InvocationTargetException;
2627

2728
/**
@@ -148,4 +149,35 @@ public static void setProperty(final String propertyName, final Object value, fi
148149
throw new RuntimeException("Error setting property " + propertyName, e);
149150
}
150151
}
152+
153+
/**
154+
* Gets the value of the given declared field on the target object or any of its superclasses.
155+
*
156+
* @param fieldName Name of field to get.
157+
* @param target Target object that possesses field.
158+
*
159+
* @return Field value.
160+
*/
161+
public static Object getField(final String fieldName, final Object target) {
162+
Class<?> clazz = target.getClass();
163+
Field field = null;
164+
do {
165+
try {
166+
field = clazz.getDeclaredField(fieldName);
167+
} catch (NoSuchFieldException e) {
168+
clazz = clazz.getSuperclass();
169+
}
170+
} while (field == null && clazz != null);
171+
if (field == null) {
172+
throw new IllegalArgumentException(fieldName + " does not exist on " + target);
173+
}
174+
try {
175+
if (!field.isAccessible()) {
176+
field.setAccessible(true);
177+
}
178+
return field.get(target);
179+
} catch (Exception e) {
180+
throw new IllegalArgumentException("Error getting field " + fieldName, e);
181+
}
182+
}
151183
}

cas-client-core/src/test/java/org/jasig/cas/client/util/ReflectUtilsTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public void testSetPropertyStringObjectObject() {
5454
assertTrue(bean.isFlag());
5555
}
5656

57+
public void testGetField() {
58+
final TestBean bean = new TestBean();
59+
bean.setName("bob");
60+
assertEquals(bean.getName(), ReflectUtils.getField("name", bean));
61+
}
62+
63+
public void testGetFieldSuperclass() {
64+
final TestSubBean bean = new TestSubBean();
65+
bean.setName("bob");
66+
assertEquals(bean.getName(), ReflectUtils.getField("name", bean));
67+
}
68+
5769
static class TestBean {
5870
private int count;
5971
private boolean flag;
@@ -102,4 +114,16 @@ public void setFlag(boolean flag) {
102114
}
103115

104116
}
117+
118+
static class TestSubBean extends TestBean {
119+
private String state;
120+
121+
public String getState() {
122+
return state;
123+
}
124+
125+
public void setState(String state) {
126+
this.state = state;
127+
}
128+
}
105129
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<parent>
4+
<artifactId>cas-client</artifactId>
5+
<groupId>org.jasig.cas.client</groupId>
6+
<version>3.4.2-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<groupId>org.jasig.cas.client</groupId>
11+
<artifactId>cas-client-integration-jetty</artifactId>
12+
<packaging>jar</packaging>
13+
<name>Jasig CAS Client for Java - Jetty Container Integration</name>
14+
15+
<properties>
16+
<!-- Note Jetty 9.2.x is the last version to support Java SE 7 -->
17+
<jetty.version>9.2.14.v20151106</jetty.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.jasig.cas.client</groupId>
23+
<artifactId>cas-client-core</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.eclipse.jetty</groupId>
28+
<artifactId>jetty-security</artifactId>
29+
<version>${jetty.version}</version>
30+
</dependency>
31+
32+
<!-- Test dependencies -->
33+
<dependency>
34+
<groupId>org.jasig.cas.client</groupId>
35+
<artifactId>cas-client-core</artifactId>
36+
<version>${project.version}</version>
37+
<type>test-jar</type>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.eclipse.jetty</groupId>
42+
<artifactId>jetty-webapp</artifactId>
43+
<version>${jetty.version}</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.eclipse.jetty</groupId>
48+
<artifactId>jetty-plus</artifactId>
49+
<version>${jetty.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.eclipse.jetty</groupId>
54+
<artifactId>jetty-annotations</artifactId>
55+
<version>${jetty.version}</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.eclipse.jetty</groupId>
60+
<artifactId>apache-jsp</artifactId>
61+
<version>${jetty.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>javax.servlet</groupId>
66+
<artifactId>javax.servlet-api</artifactId>
67+
<version>3.1.0</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
73+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.jasig.cas.client.jetty;
2+
3+
import org.eclipse.jetty.security.UserAuthentication;
4+
import org.jasig.cas.client.util.CommonUtils;
5+
import org.jasig.cas.client.validation.Assertion;
6+
7+
/**
8+
* CAS-specific user authentication.
9+
*
10+
* @author Marvin S. Addison
11+
*/
12+
public class CasAuthentication extends UserAuthentication {
13+
14+
/** CAS authenticator that produced this authentication. */
15+
private final CasAuthenticator authenticator;
16+
17+
/** CAS ticket that was successfully validated to permit authentication. */
18+
private final String ticket;
19+
20+
21+
/**
22+
* Creates a new instance.
23+
*
24+
* @param authenticator The authenticator that produced this authentication.
25+
* @param ticket The CAS ticket that was successfully validated to permit authentication.
26+
* @param assertion The CAS assertion produced from successful ticket validation.
27+
*/
28+
public CasAuthentication(final CasAuthenticator authenticator, final String ticket, final Assertion assertion) {
29+
super(authenticator.getAuthMethod(), new CasUserIdentity(assertion, authenticator.getRoleAttribute()));
30+
CommonUtils.assertNotNull(ticket, "Ticket cannot be null");
31+
CommonUtils.assertNotNull(authenticator, "CasAuthenticator cannot be null");
32+
this.authenticator = authenticator;
33+
this.ticket = ticket;
34+
}
35+
36+
/** @return The CAS ticket that was successfully validated to permit authentication. */
37+
public String getTicket() {
38+
return ticket;
39+
}
40+
41+
@Override
42+
public void logout() {
43+
super.logout();
44+
this.authenticator.clearCachedAuthentication(ticket);
45+
}
46+
}

0 commit comments

Comments
 (0)