Skip to content
Permalink
Browse files
Updated Websphere sample application to fit the system test requireme…
…ntd. Added deployment project to build ear file for websphere deployment.
  • Loading branch information
cschmuelling authored and coheigea committed Aug 13, 2015
1 parent 92db813 commit 34d160e3bf74679ecf3cc9f976ed138430ad2a9f
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 45 deletions.
@@ -37,4 +37,14 @@
<module>spring2Webapp</module>
<module>springPreauthWebapp</module>
</modules>

<profiles>
<profile>
<id>websphere</id>
<modules>
<module>websphereWebapp</module>
<module>websphereWebappEar</module>
</modules>
</profile>
</profiles>
</project>
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.cxf.fediz</groupId>
<artifactId>examples</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>
</parent>

<groupId>org.apache.cxf.fediz.examples</groupId>
@@ -54,10 +54,31 @@
<artifactId>commons-lang3</artifactId>
<version>${commons.lang.version}</version>
</dependency>

<dependency>
<groupId>com.ibm.websphere</groupId>
<artifactId>com.ibm.websphere.security</artifactId>
<version>1.0.3</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibm.ws.security</groupId>
<artifactId>com.ibm.ws.security.authentication.tai</artifactId>
<version>1.0.3</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibm.ws.security</groupId>
<artifactId>com.ibm.ws.security.token</artifactId>
<version>1.0.2</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<!-- Name of the generated WAR file -->
<finalName>fedizhelloworld</finalName>
</build>

@@ -21,95 +21,106 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.Principal;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.security.auth.Subject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;

import org.apache.commons.lang3.StringEscapeUtils;
import com.ibm.websphere.security.WSSecurityException;
import com.ibm.websphere.security.auth.WSSubject;

import org.apache.cxf.fediz.core.Claim;
import org.apache.cxf.fediz.core.ClaimCollection;
import org.apache.cxf.fediz.core.FedizPrincipal;
import org.apache.cxf.fediz.core.SecurityTokenThreadLocal;

import org.apache.cxf.fediz.core.processor.FedizResponse;

public class FederationServlet extends HttpServlet {

/**
*
*/
private static final String SUBJECT_TOKEN_KEY = "_security.token";

private static final long serialVersionUID = -9019993850246851112L;

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

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head><title>WS Federation Example</title></head>");
out.println("<head><title>WS Federation Systests Examples</title></head>");
out.println("<body>");
out.println("<h1>Hello World</h1>");
out.println("Request url: " + request.getRequestURL().toString() + "<p>");

out.println("<p>Request url: " + request.getRequestURL().toString() + "</p>");

out.println("<br><b>User</b><p>");
out.print("<p>userPrincipal=");
Principal p = request.getUserPrincipal();
if (p != null) {
out.println("Principal: " + p.getName() + "<p>");
out.print(p.getName());
}
out.println("</p>");

out.println("<br><b>Roles</b><p>");
List<String> roleListToCheck = Arrays.asList("Admin", "Manager", "User", "Authenticated");
for (String item: roleListToCheck) {
out.println("Has role '" + item + "': " + ((request.isUserInRole(item)) ? "<b>yes</b>" : "no") + "<p>");
for (String item : roleListToCheck) {
out.println("<p>role:" + item + "=" + ((request.isUserInRole(item)) ? "true" : "false") + "</p>");
}

if (p instanceof FedizPrincipal) {
FedizPrincipal fp = (FedizPrincipal)p;

out.println("<br><b>Claims</b><p>");
ClaimCollection claims = fp.getClaims();
for (Claim c: claims) {
out.println(c.getClaimType().toString() + ": " + c.getValue() + "<p>");
for (Claim c : claims) {
out.println("<p>" + c.getClaimType().toString() + "=" + c.getValue() + "</p>");
}

Element el = fp.getLoginToken();
if (el != null) {
out.println("loginToken=FOUND{FedizPrincipal}<p>");
}
} else {
out.println("<br>Principal is not instance of FedizPrincipal<p>");
}

Element el = SecurityTokenThreadLocal.getToken();
if (el != null) {
out.println("<br><b>Bootstrap token</b><p>");
String token = null;
try {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(el),
new StreamResult(buffer));
token = buffer.toString();
out.println("<p>" + StringEscapeUtils.escapeXml11(token));
} catch (Exception ex) {
out.println("<p>Failed to transform cached element to string: " + ex.toString());
el = SecurityTokenThreadLocal.getToken();
if (el != null) {
out.println("loginToken=FOUND{SecurityTokenThreadLocal}<p>");
}
} else {
out.println("<p>Bootstrap token not cached in thread local storage");

}

out.println("</body>");
try {
Subject subject = WSSubject.getCallerSubject();
if (subject != null) {
FedizResponse fedResponse = getCachedFederationResponse(subject);

ClaimCollection claims = new ClaimCollection(fedResponse.getClaims());
for (Claim c : claims) {
out.println("<p>" + c.getClaimType().toString() + "=" + c.getValue() + "</p>");
}
}
} catch (WSSecurityException e) {
out.println("<p>Exception=" + e.getMessage() + "</p>");
}
}

private FedizResponse getCachedFederationResponse(Subject subject) {
Iterator<?> i = subject.getPublicCredentials().iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof Hashtable) {
Map<?, ?> table = (Hashtable<?, ?>)o;
return (FedizResponse)table.get(SUBJECT_TOKEN_KEY);
}
}
return null;
}
}
@@ -0,0 +1,14 @@
Simple Web Application Demo
===========================

This demo shows how to build and deploy an SSO protected using Apache CXF Fediz
web application.

Running this sample consists of four steps:

- Configure the Tomcat-IDP and Tomcat or Jetty-RP instances
- Building the demo using Maven
- Deploying the demo to the RP instance
- Testing the demo

Please review the README in the samples main directory before continuing.
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.cxf.fediz</groupId>
<artifactId>examples</artifactId>
<version>1.2.1-SNAPSHOT</version>
</parent>

<groupId>org.apache.cxf.fediz.examples</groupId>
<artifactId>websphereWebappEar</artifactId>
<name>Fediz Example: WebsphereWebappEar</name>
<packaging>ear</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.cxf.fediz.examples</groupId>
<artifactId>websphereWebapp</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<displayName>Fediz Hello World</displayName>
<description>Fediz Hello World demo application.</description>
<finalName>fedizhelloworld</finalName>
<modules>
<webModule>
<groupId>org.apache.cxf.fediz.examples</groupId>
<artifactId>websphereWebapp</artifactId>
<contextRoot>fedizhelloworld</contextRoot>
<uri>fedizhelloworld.war</uri>
</webModule>
</modules>
<security>
<security-role id="SecurityRole_1311175300750">
<role-name>User</role-name>
</security-role>
<security-role id="SecurityRole_1311175300751">
<role-name>Manager</role-name>
</security-role>
<security-role id="SecurityRole_1311175300752">
<role-name>Admin</role-name>
</security-role>
<security-role id="SecurityRole_1425914835358">
<role-name>Authenticated</role-name>
</security-role>
</security>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit 34d160e

Please sign in to comment.