Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ public class ArquillianBase {

private static final String WEBAPP_SRC = "src/main/webapp";
private WebClient webClient;

public static Archive<?> defaultArchive() {

WebArchive webArchive =
return tryWrapEAR(defaultWebArchive());
}

public static WebArchive defaultWebArchive() {
return
create(WebArchive.class, "test.war")
.addPackages(true, "org.javaee7.jaspic")
.deleteClass(ArquillianBase.class)
.addAsWebInfResource(resource("web.xml"))
.addAsWebInfResource(resource("jboss-web.xml"))
.addAsWebInfResource(resource("glassfish-web.xml"));

}

public static Archive<?> tryWrapEAR(WebArchive webArchive) {
if (getBoolean("useEarForJaspic")) {
return
// EAR archive
Expand All @@ -48,15 +54,19 @@ public static Archive<?> defaultArchive() {
// This is needed to prevent Arquillian generating an illegal application.xml
.addAsModule(
webArchive
);
);
} else {
return webArchive;
}
}

private static File resource(String name) {
public static File resource(String name) {
return new File(WEBAPP_SRC + "/WEB-INF", name);
}

public static File web(String name) {
return new File(WEBAPP_SRC, name);
}

@ArquillianResource
private URL base;
Expand Down
23 changes: 23 additions & 0 deletions jaspic/custom-principal/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.javaee7</groupId>
<artifactId>jaspic</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>jaspic-custom-principal</artifactId>
<packaging>war</packaging>
<name>Java EE 7 Sample: jaspic - custom principal</name>

<dependencies>
<dependency>
<groupId>org.javaee7</groupId>
<artifactId>jaspic-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.jaspic.customprincipal.sam;

import java.security.Principal;

/**
*
* @author Arjan Tijms
*
*/
public class MyPrincipal implements Principal {

private final String name;

public MyPrincipal(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.javaee7.jaspic.customprincipal.sam;

import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;

import org.javaee7.jaspic.common.BaseServletContextListener;
import org.javaee7.jaspic.common.JaspicUtils;

/**
*
* @author Arjan Tijms
*
*/
@WebListener
public class SamAutoRegistrationListener extends BaseServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
JaspicUtils.registerSAM(sce.getServletContext(), new TestServerAuthModule());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.javaee7.jaspic.customprincipal.sam;

import static javax.security.auth.message.AuthStatus.SEND_SUCCESS;
import static javax.security.auth.message.AuthStatus.SUCCESS;

import java.io.IOException;
import java.security.Principal;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.message.AuthException;
import javax.security.auth.message.AuthStatus;
import javax.security.auth.message.MessageInfo;
import javax.security.auth.message.MessagePolicy;
import javax.security.auth.message.callback.CallerPrincipalCallback;
import javax.security.auth.message.callback.GroupPrincipalCallback;
import javax.security.auth.message.module.ServerAuthModule;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Variant of the SAM used by the basic-authentication test, where the so-called "Principal form" of the
* CallerPrincipalCallback is used. Here we pass in a custom Principal instead of a string.
*
* @author Arjan Tijms
*
*/
public class TestServerAuthModule implements ServerAuthModule {

private CallbackHandler handler;
private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };

@Override
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler,
@SuppressWarnings("rawtypes") Map options) throws AuthException {
this.handler = handler;
}

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject)
throws AuthException {

HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();

Callback[] callbacks;

if (request.getParameter("doLogin") != null) {

// For the test perform a login by directly "returning" the details of the authenticated user.
// Normally credentials would be checked and the details fetched from some repository

callbacks = new Callback[] {
// The name of the authenticated user *** VIA A CUSTOM PRINCIPAL ***.
// This is the main variant of this test vs basic-authentication
new CallerPrincipalCallback(clientSubject, new MyPrincipal("test")),
// the roles of the authenticated user
new GroupPrincipalCallback(clientSubject, new String[] { "architect" })
};
} else {

// The JASPIC protocol for "do nothing"
callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
}

try {

// Communicate the details of the authenticated user to the container. In many
// cases the handler will just store the details and the container will actually handle
// the login after we return from this method.
handler.handle(callbacks);

} catch (IOException | UnsupportedCallbackException e) {
throw (AuthException) new AuthException().initCause(e);
}

return SUCCESS;
}

@Override
public Class<?>[] getSupportedMessageTypes() {
return supportedMessageTypes;
}

@Override
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {
return SEND_SUCCESS;
}

@Override
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.javaee7.jaspic.customprincipal.servlet;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javaee7.jaspic.customprincipal.sam.MyPrincipal;

/**
*
* @author Arjan Tijms
*
*/
@WebServlet(urlPatterns = "/protected/servlet")
public class ProtectedServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.getWriter().write("This is a protected servlet \n");

String webName = null;
boolean isCustomPrincipal = false;
if (request.getUserPrincipal() != null) {
Principal principal = request.getUserPrincipal();
isCustomPrincipal = principal instanceof MyPrincipal;
webName = request.getUserPrincipal().getName();
}

boolean webHasRole = request.isUserInRole("architect");

response.getWriter().write("isCustomPrincipal: " + isCustomPrincipal + "\n");
response.getWriter().write("web username: " + webName + "\n");
response.getWriter().write("web user has role \"architect\": " + webHasRole + "\n");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.javaee7.jaspic.customprincipal.servlet;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javaee7.jaspic.customprincipal.sam.MyPrincipal;

/**
*
* @author Arjan Tijms
*
*/
@WebServlet(urlPatterns = "/public/servlet")
public class PublicServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.getWriter().write("This is a public servlet \n");

String webName = null;
boolean isCustomPrincipal = false;
if (request.getUserPrincipal() != null) {
Principal principal = request.getUserPrincipal();
isCustomPrincipal = principal instanceof MyPrincipal;
webName = principal.getName();
}

boolean webHasRole = request.isUserInRole("architect");

response.getWriter().write("isCustomPrincipal: " + isCustomPrincipal + "\n");
response.getWriter().write("web username: " + webName + "\n");
response.getWriter().write("web user has role \"architect\": " + webHasRole + "\n");
}

}
12 changes: 12 additions & 0 deletions jaspic/custom-principal/src/main/webapp/WEB-INF/glassfish-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<glassfish-web-app>

<security-role-mapping>
<role-name>architect</role-name>
<group-name>architect</group-name>
</security-role-mapping>

<parameter-encoding default-charset="UTF-8" />

</glassfish-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<application-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-bnd_1_2.xsd"
xmlns="http://websphere.ibm.com/xml/ns/javaee"
version="1.2">

<security-role name="architect">
<group name="architect" />
</security-role>

</application-bnd>
5 changes: 5 additions & 0 deletions jaspic/custom-principal/src/main/webapp/WEB-INF/jboss-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>

<jboss-web>
<security-domain>jaspitest</security-domain>
</jboss-web>
20 changes: 20 additions & 0 deletions jaspic/custom-principal/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
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"
version="3.0">

<security-constraint>
<web-resource-collection>
<web-resource-name>Test</web-resource-name>
<url-pattern>/protected/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>architect</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<role-name>architect</role-name>
</security-role>

</web-app>
Loading