Skip to content

Commit 557de80

Browse files
committed
Merge pull request #303 from arjantijms/jaspic
Added JASPIC tests that test whether a SAM can forward and include
2 parents f9fdd13 + f845342 commit 557de80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1686
-6
lines changed

jaspic/common/src/main/java/org/javaee7/jaspic/common/ArquillianBase.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ public class ArquillianBase {
2626

2727
private static final String WEBAPP_SRC = "src/main/webapp";
2828
private WebClient webClient;
29-
29+
3030
public static Archive<?> defaultArchive() {
31-
32-
WebArchive webArchive =
31+
return tryWrapEAR(defaultWebArchive());
32+
}
33+
34+
public static WebArchive defaultWebArchive() {
35+
return
3336
create(WebArchive.class, "test.war")
3437
.addPackages(true, "org.javaee7.jaspic")
38+
.deleteClass(ArquillianBase.class)
3539
.addAsWebInfResource(resource("web.xml"))
3640
.addAsWebInfResource(resource("jboss-web.xml"))
3741
.addAsWebInfResource(resource("glassfish-web.xml"));
38-
42+
}
43+
44+
public static Archive<?> tryWrapEAR(WebArchive webArchive) {
3945
if (getBoolean("useEarForJaspic")) {
4046
return
4147
// EAR archive
@@ -48,15 +54,19 @@ public static Archive<?> defaultArchive() {
4854
// This is needed to prevent Arquillian generating an illegal application.xml
4955
.addAsModule(
5056
webArchive
51-
);
57+
);
5258
} else {
5359
return webArchive;
5460
}
5561
}
5662

57-
private static File resource(String name) {
63+
public static File resource(String name) {
5864
return new File(WEBAPP_SRC + "/WEB-INF", name);
5965
}
66+
67+
public static File web(String name) {
68+
return new File(WEBAPP_SRC, name);
69+
}
6070

6171
@ArquillianResource
6272
private URL base;

jaspic/custom-principal/pom.xml

Lines changed: 23 additions & 0 deletions
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+
12+
<artifactId>jaspic-custom-principal</artifactId>
13+
<packaging>war</packaging>
14+
<name>Java EE 7 Sample: jaspic - custom principal</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>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.jaspic.customprincipal.sam;
2+
3+
import java.security.Principal;
4+
5+
/**
6+
*
7+
* @author Arjan Tijms
8+
*
9+
*/
10+
public class MyPrincipal implements Principal {
11+
12+
private final String name;
13+
14+
public MyPrincipal(String name) {
15+
this.name = name;
16+
}
17+
18+
@Override
19+
public String getName() {
20+
return name;
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.jaspic.customprincipal.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+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.javaee7.jaspic.customprincipal.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+
* Variant of the SAM used by the basic-authentication test, where the so-called "Principal form" of the
26+
* CallerPrincipalCallback is used. Here we pass in a custom Principal instead of a string.
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 *** VIA A CUSTOM PRINCIPAL ***.
57+
// This is the main variant of this test vs basic-authentication
58+
new CallerPrincipalCallback(clientSubject, new MyPrincipal("test")),
59+
// the roles of the authenticated user
60+
new GroupPrincipalCallback(clientSubject, new String[] { "architect" })
61+
};
62+
} else {
63+
64+
// The JASPIC protocol for "do nothing"
65+
callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
66+
}
67+
68+
try {
69+
70+
// Communicate the details of the authenticated user to the container. In many
71+
// cases the handler will just store the details and the container will actually handle
72+
// the login after we return from this method.
73+
handler.handle(callbacks);
74+
75+
} catch (IOException | UnsupportedCallbackException e) {
76+
throw (AuthException) new AuthException().initCause(e);
77+
}
78+
79+
return SUCCESS;
80+
}
81+
82+
@Override
83+
public Class<?>[] getSupportedMessageTypes() {
84+
return supportedMessageTypes;
85+
}
86+
87+
@Override
88+
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
89+
return SEND_SUCCESS;
90+
}
91+
92+
@Override
93+
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
94+
95+
}
96+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.javaee7.jaspic.customprincipal.servlet;
2+
3+
import java.io.IOException;
4+
import java.security.Principal;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
import org.javaee7.jaspic.customprincipal.sam.MyPrincipal;
13+
14+
/**
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@WebServlet(urlPatterns = "/protected/servlet")
20+
public class ProtectedServlet extends HttpServlet {
21+
22+
private static final long serialVersionUID = 1L;
23+
24+
@Override
25+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26+
27+
response.getWriter().write("This is a protected servlet \n");
28+
29+
String webName = null;
30+
boolean isCustomPrincipal = false;
31+
if (request.getUserPrincipal() != null) {
32+
Principal principal = request.getUserPrincipal();
33+
isCustomPrincipal = principal instanceof MyPrincipal;
34+
webName = request.getUserPrincipal().getName();
35+
}
36+
37+
boolean webHasRole = request.isUserInRole("architect");
38+
39+
response.getWriter().write("isCustomPrincipal: " + isCustomPrincipal + "\n");
40+
response.getWriter().write("web username: " + webName + "\n");
41+
response.getWriter().write("web user has role \"architect\": " + webHasRole + "\n");
42+
43+
}
44+
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.javaee7.jaspic.customprincipal.servlet;
2+
3+
import java.io.IOException;
4+
import java.security.Principal;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
import org.javaee7.jaspic.customprincipal.sam.MyPrincipal;
13+
14+
/**
15+
*
16+
* @author Arjan Tijms
17+
*
18+
*/
19+
@WebServlet(urlPatterns = "/public/servlet")
20+
public class PublicServlet extends HttpServlet {
21+
22+
private static final long serialVersionUID = 1L;
23+
24+
@Override
25+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26+
27+
response.getWriter().write("This is a public servlet \n");
28+
29+
String webName = null;
30+
boolean isCustomPrincipal = false;
31+
if (request.getUserPrincipal() != null) {
32+
Principal principal = request.getUserPrincipal();
33+
isCustomPrincipal = principal instanceof MyPrincipal;
34+
webName = principal.getName();
35+
}
36+
37+
boolean webHasRole = request.isUserInRole("architect");
38+
39+
response.getWriter().write("isCustomPrincipal: " + isCustomPrincipal + "\n");
40+
response.getWriter().write("web username: " + webName + "\n");
41+
response.getWriter().write("web user has role \"architect\": " + webHasRole + "\n");
42+
}
43+
44+
}
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: 11 additions & 0 deletions
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>
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>

0 commit comments

Comments
 (0)