Skip to content

Commit 8cf60e3

Browse files
committed
Basic test for async requests and JASPIC
1 parent ea534ab commit 8cf60e3

File tree

10 files changed

+298
-0
lines changed

10 files changed

+298
-0
lines changed

jaspic/async-authentication/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.javaee7.jaspic</groupId>
8+
<artifactId>jaspic-samples</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<groupId>org.javaee7.jaspic</groupId>
14+
<artifactId>async-authentication</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<packaging>war</packaging>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee7.jaspic</groupId>
21+
<artifactId>common</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
26+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.javaee7.jaspic.asyncauthentication.bean;
2+
3+
import static java.lang.Thread.interrupted;
4+
import static java.lang.Thread.sleep;
5+
6+
import java.io.IOException;
7+
8+
import javax.ejb.Asynchronous;
9+
import javax.ejb.Stateless;
10+
import javax.servlet.AsyncContext;
11+
12+
/**
13+
*
14+
* @author Arjan Tijms
15+
*
16+
*/
17+
@Stateless
18+
public class AsyncBean {
19+
20+
@Asynchronous
21+
public void doAsync(AsyncContext asyncContext) {
22+
23+
try {
24+
sleep(1000);
25+
} catch (InterruptedException e) {
26+
interrupted();
27+
}
28+
29+
try {
30+
asyncContext.getResponse().getWriter().write("async response");
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
35+
asyncContext.complete();
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.jaspic.asyncauthentication.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,95 @@
1+
package org.javaee7.jaspic.asyncauthentication.sam;
2+
3+
import static javax.security.auth.message.AuthStatus.SEND_SUCCESS;
4+
import static javax.security.auth.message.AuthStatus.SUCCESS;
5+
6+
import java.io.IOException;
7+
import java.security.Principal;
8+
import java.util.Map;
9+
10+
import javax.security.auth.Subject;
11+
import javax.security.auth.callback.Callback;
12+
import javax.security.auth.callback.CallbackHandler;
13+
import javax.security.auth.callback.UnsupportedCallbackException;
14+
import javax.security.auth.message.AuthException;
15+
import javax.security.auth.message.AuthStatus;
16+
import javax.security.auth.message.MessageInfo;
17+
import javax.security.auth.message.MessagePolicy;
18+
import javax.security.auth.message.callback.CallerPrincipalCallback;
19+
import javax.security.auth.message.callback.GroupPrincipalCallback;
20+
import javax.security.auth.message.module.ServerAuthModule;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
/**
25+
* Very basic SAM that returns a single hardcoded user named "test" with role "architect" when the request parameter
26+
* <code>doLogin</code> is present.
27+
*
28+
* @author Arjan Tijms
29+
*
30+
*/
31+
public class TestServerAuthModule implements ServerAuthModule {
32+
33+
private CallbackHandler handler;
34+
private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };
35+
36+
@Override
37+
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler,
38+
@SuppressWarnings("rawtypes") Map options) throws AuthException {
39+
this.handler = handler;
40+
}
41+
42+
@Override
43+
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject)
44+
throws AuthException {
45+
46+
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
47+
48+
Callback[] callbacks;
49+
50+
if (request.getParameter("doLogin") != null) {
51+
52+
// For the test perform a login by directly "returning" the details of the authenticated user.
53+
// Normally credentials would be checked and the details fetched from some repository
54+
55+
callbacks = new Callback[] {
56+
// The name of the authenticated user
57+
new CallerPrincipalCallback(clientSubject, "test"),
58+
// the roles of the authenticated user
59+
new GroupPrincipalCallback(clientSubject, new String[] { "architect" })
60+
};
61+
} else {
62+
63+
// The JASPIC protocol for "do nothing"
64+
callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
65+
}
66+
67+
try {
68+
69+
// Communicate the details of the authenticated user to the container. In many
70+
// cases the handler will just store the details and the container will actually handle
71+
// the login after we return from this method.
72+
handler.handle(callbacks);
73+
74+
} catch (IOException | UnsupportedCallbackException e) {
75+
throw (AuthException) new AuthException().initCause(e);
76+
}
77+
78+
return SUCCESS;
79+
}
80+
81+
@Override
82+
public Class<?>[] getSupportedMessageTypes() {
83+
return supportedMessageTypes;
84+
}
85+
86+
@Override
87+
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
88+
return SEND_SUCCESS;
89+
}
90+
91+
@Override
92+
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
93+
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javaee7.jaspic.asyncauthentication.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.ejb.EJB;
6+
import javax.servlet.AsyncContext;
7+
import javax.servlet.ServletException;
8+
import javax.servlet.annotation.WebServlet;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
import org.javaee7.jaspic.asyncauthentication.bean.AsyncBean;
14+
15+
/**
16+
*
17+
* @author Arjan Tijms
18+
*
19+
*/
20+
@WebServlet(urlPatterns = "/public/asyncServlet", asyncSupported = true)
21+
public class AsyncServlet extends HttpServlet {
22+
23+
private static final long serialVersionUID = 1L;
24+
25+
@EJB
26+
private AsyncBean asyncBean;
27+
28+
@Override
29+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30+
31+
AsyncContext asyncContext = request.startAsync();
32+
asyncContext.setTimeout(5000);
33+
34+
asyncBean.doAsync(asyncContext);
35+
}
36+
37+
}
Lines changed: 12 additions & 0 deletions
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>
Lines changed: 5 additions & 0 deletions
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>
Lines changed: 20 additions & 0 deletions
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,40 @@
1+
package org.javaee7.jaspic.asyncauthentication;
2+
3+
import static org.junit.Assert.assertTrue;
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.spec.WebArchive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.xml.sax.SAXException;
14+
15+
/**
16+
*
17+
*
18+
* @author Arjan Tijms
19+
*
20+
*/
21+
@RunWith(Arquillian.class)
22+
public class AsyncAuthenticationPublicTest extends ArquillianBase {
23+
24+
@Deployment(testable = false)
25+
public static WebArchive createDeployment() {
26+
return defaultArchive();
27+
}
28+
29+
/**
30+
* This tests that an async response works at all in the mere presence of
31+
* a JASPIC SAM (that does nothing)
32+
*/
33+
@Test
34+
public void testBasicAsync() throws IOException, SAXException {
35+
36+
String response = getFromServerPath("public/asyncServlet");
37+
assertTrue(response.contains("async response"));
38+
}
39+
40+
}

jaspic/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<modules>
2020
<!-- Not a module with tests, but contains common code for the other modules -->
2121
<module>common</module>
22+
23+
<!-- Tests behavior of authentication involving asynchronous requests -->
24+
<module>async-authentication</module>
2225

2326
<!-- Tests a simple authentication from both public and protected resources -->
2427
<module>basic-authentication</module>

0 commit comments

Comments
 (0)