Skip to content

Commit b925063

Browse files
committed
Merge pull request #332 from arjantijms/master
Added test to see if SAM can set a status code into the response
2 parents 78b5668 + 64e9774 commit b925063

File tree

12 files changed

+297
-1
lines changed

12 files changed

+297
-1
lines changed

jaspic/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@
3636

3737
<!-- JASPIC is normally stateless but with a new option introduced in JASPIC 1.1 it can semi-transparently
3838
remember an authenticated identity (semi, because the SAM will still be called and has to explicitly
39-
indidate it wants to continue this remembered session). This tests that remembering a session indeed works.
39+
indicate it wants to continue this remembered session). This tests that remembering a session indeed works.
4040
-->
4141
<module>register-session</module>
4242

4343
<!-- Tests behavior of authentication involving asynchronous requests -->
4444
<module>async-authentication</module>
4545

46+
<!-- Tests that a SAM is able to set a status code into the response -->
47+
<module>status-codes</module>
48+
4649
<!-- Like a Servlet a JASPIC SAM for the Servlet Profile can dispatch a request via a forward or include. This tests that
4750
this is indeed possible by using plain Servlets and nothing else. -->
4851
<module>dispatching</module>

jaspic/status-codes/pom.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.javaee7</groupId>
7+
<artifactId>jaspic</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>jaspic-status-codes</artifactId>
12+
<packaging>war</packaging>
13+
14+
<name>Java EE 7 Sample: jaspic - Status codes </name>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.javaee7</groupId>
19+
<artifactId>jaspic-common</artifactId>
20+
<version>1.0-SNAPSHOT</version>
21+
</dependency>
22+
</dependencies>
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.jaspic.statuscodes.sam;
2+
3+
import javax.servlet.ServletContextEvent;
4+
import javax.servlet.annotation.WebListener;
5+
6+
import org.javaee7.jaspic.common.BaseServletContextListener;
7+
import org.javaee7.jaspic.common.JaspicUtils;
8+
9+
/**
10+
*
11+
* @author Arjan Tijms
12+
*
13+
*/
14+
@WebListener
15+
public class SamAutoRegistrationListener extends BaseServletContextListener {
16+
17+
@Override
18+
public void contextInitialized(ServletContextEvent sce) {
19+
JaspicUtils.registerSAM(sce.getServletContext(), new TestServerAuthModule());
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javaee7.jaspic.statuscodes.sam;
2+
3+
import static javax.security.auth.message.AuthStatus.SEND_FAILURE;
4+
import static javax.security.auth.message.AuthStatus.SEND_SUCCESS;
5+
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
6+
7+
import java.io.IOException;
8+
import java.util.Map;
9+
10+
import javax.security.auth.Subject;
11+
import javax.security.auth.callback.CallbackHandler;
12+
import javax.security.auth.message.AuthException;
13+
import javax.security.auth.message.AuthStatus;
14+
import javax.security.auth.message.MessageInfo;
15+
import javax.security.auth.message.MessagePolicy;
16+
import javax.security.auth.message.module.ServerAuthModule;
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
20+
/**
21+
* Very basic SAM that just sets an HTTP status code into the response and then returns SEND_FAILURE.
22+
* <code>doLogin</code> is present.
23+
*
24+
* @author Arjan Tijms
25+
*
26+
*/
27+
public class TestServerAuthModule implements ServerAuthModule {
28+
29+
private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };
30+
31+
@Override
32+
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler, @SuppressWarnings("rawtypes") Map options) throws AuthException {
33+
}
34+
35+
@Override
36+
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
37+
38+
HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
39+
40+
try {
41+
response.sendError(SC_NOT_FOUND);
42+
return SEND_FAILURE;
43+
} catch (IOException e) {
44+
throw (AuthException) new AuthException().initCause(e);
45+
}
46+
}
47+
48+
@Override
49+
public Class<?>[] getSupportedMessageTypes() {
50+
return supportedMessageTypes;
51+
}
52+
53+
@Override
54+
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
55+
return SEND_SUCCESS;
56+
}
57+
58+
@Override
59+
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
60+
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.javaee7.jaspic.statuscodes.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
*
13+
* @author Arjan Tijms
14+
*
15+
*/
16+
@WebServlet(urlPatterns = "/protected/servlet")
17+
public class ProtectedServlet extends HttpServlet {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
@Override
22+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23+
response.getWriter().write("This is a protected servlet \n");
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.javaee7.jaspic.statuscodes.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
*
13+
* @author Arjan Tijms
14+
*
15+
*/
16+
@WebServlet(urlPatterns = "/public/servlet")
17+
public class PublicServlet extends HttpServlet {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
@Override
22+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23+
response.getWriter().write("This is a public servlet \n");
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
3+
<glassfish-web-app>
4+
5+
<security-role-mapping>
6+
<role-name>architect</role-name>
7+
<group-name>architect</group-name>
8+
</security-role-mapping>
9+
10+
<parameter-encoding default-charset="UTF-8" />
11+
12+
</glassfish-web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<application-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-bnd_1_2.xsd"
4+
xmlns="http://websphere.ibm.com/xml/ns/javaee"
5+
version="1.2">
6+
7+
<security-role name="architect">
8+
<group name="architect" />
9+
</security-role>
10+
11+
</application-bnd>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
3+
<jboss-web>
4+
<security-domain>jaspitest</security-domain>
5+
</jboss-web>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
4+
version="3.0">
5+
6+
<security-constraint>
7+
<web-resource-collection>
8+
<web-resource-name>Test</web-resource-name>
9+
<url-pattern>/protected/*</url-pattern>
10+
</web-resource-collection>
11+
<auth-constraint>
12+
<role-name>architect</role-name>
13+
</auth-constraint>
14+
</security-constraint>
15+
16+
<security-role>
17+
<role-name>architect</role-name>
18+
</security-role>
19+
20+
</web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javaee7.jaspic.statuscodes;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.javaee7.jaspic.common.ArquillianBase;
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.Archive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
15+
/**
16+
* This tests that a SAM can set a 404 response code when a protected resource is requested.
17+
* Note the resource is not actual invoked, as the SAM returns SEND_FAILURE.
18+
*
19+
* @author Arjan Tijms
20+
*
21+
*/
22+
@RunWith(Arquillian.class)
23+
public class ProtectedStatusCodesTest extends ArquillianBase {
24+
25+
@Deployment(testable = false)
26+
public static Archive<?> createDeployment() {
27+
return defaultArchive();
28+
}
29+
30+
@Test
31+
public void test404inResponse() throws IOException {
32+
33+
int code = getWebClient().getPage(getBase() + "protected/servlet")
34+
.getWebResponse()
35+
.getStatusCode();
36+
37+
assertEquals(
38+
"Response should have 404 not found as status code, but did not.",
39+
404, code
40+
);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javaee7.jaspic.statuscodes;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.javaee7.jaspic.common.ArquillianBase;
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.Archive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
15+
/**
16+
* This tests that a SAM can set a 404 response code when a public resource is requested.
17+
* Note the resource is not actual invoked, as the SAM returns SEND_FAILURE.
18+
*
19+
* @author Arjan Tijms
20+
*
21+
*/
22+
@RunWith(Arquillian.class)
23+
public class PublicStatusCodesTest extends ArquillianBase {
24+
25+
@Deployment(testable = false)
26+
public static Archive<?> createDeployment() {
27+
return defaultArchive();
28+
}
29+
30+
@Test
31+
public void test404inResponse() throws IOException {
32+
33+
int code = getWebClient().getPage(getBase() + "public/servlet")
34+
.getWebResponse()
35+
.getStatusCode();
36+
37+
assertEquals(
38+
"Response should have 404 not found as status code, but did not.",
39+
404, code
40+
);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)