Author: anil.saldhana(a)jboss.com
Date: 2008-12-11 10:27:48 -0500 (Thu, 11 Dec 2008)
New Revision: 144
Added:
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnResponseFactory.java
Modified:
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnRequestFactory.java
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLBaseFactory.java
Log:
factories
Modified:
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnRequestFactory.java
===================================================================
---
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnRequestFactory.java 2008-12-11
15:26:54 UTC (rev 143)
+++
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnRequestFactory.java 2008-12-11
15:27:48 UTC (rev 144)
@@ -22,11 +22,14 @@
package org.jboss.identity.federation.api.saml.v2.factories;
import java.io.InputStream;
+import java.io.OutputStream;
import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.jboss.identity.federation.saml.v2.protocol.AuthnRequestType;
+import org.jboss.identity.federation.saml.v2.protocol.ObjectFactory;
/**
* Factory for SAML2 AuthnRequest
@@ -37,6 +40,8 @@
{
private static String pkgName =
"org.jboss.identity.federation.saml.v2.protocol";
private static String schemaLocation =
"schema/saml-schema-protocol-2.0.xsd";
+
+ private static ObjectFactory protocolObjectFactory = new ObjectFactory();
/**
* Get AuthnRequestType from a file
@@ -46,18 +51,36 @@
* @throws IllegalArgumentException if the input fileName is null
* IllegalStateException if the InputStream from the fileName is null
*/
- @SuppressWarnings("unchecked")
public static AuthnRequestType getAuthnRequestType(String fileName) throws Exception
{
if(fileName == null)
throw new IllegalArgumentException("fileName is null");
-
- Unmarshaller un = JBossSAMLBaseFactory.getValidatingUnmarshaller(pkgName,
schemaLocation);
ClassLoader tcl = SecurityActions.getContextClassLoader();
InputStream is = tcl.getResourceAsStream(fileName);
+ return getAuthnRequestType(is);
+ }
+
+ /**
+ * Get the AuthnRequestType from an input stream
+ * @param is Inputstream containing the AuthnRequest
+ * @return
+ * @throws Exception
+ * @throws IllegalArgumentException inputstream is null
+ */
+ @SuppressWarnings("unchecked")
+ public static AuthnRequestType getAuthnRequestType(InputStream is) throws Exception
+ {
if(is == null)
- throw new IllegalStateException("InputStream is null for "+
fileName);
+ throw new IllegalStateException("InputStream is null");
+ Unmarshaller un = JBossSAMLBaseFactory.getValidatingUnmarshaller(pkgName,
schemaLocation);
JAXBElement<AuthnRequestType> jaxbAuthnRequestType =
(JAXBElement<AuthnRequestType>) un.unmarshal(is);
- return jaxbAuthnRequestType.getValue();
+ return jaxbAuthnRequestType.getValue();
+ }
+
+ public static void marshall(AuthnRequestType requestType, OutputStream os) throws
Exception
+ {
+ Marshaller marshaller = JBossSAMLBaseFactory.getValidatingMarshaller(pkgName,
schemaLocation);
+ JAXBElement<AuthnRequestType> jaxb =
protocolObjectFactory.createAuthnRequest(requestType);
+ marshaller.marshal(jaxb, os);
}
-}
+}
\ No newline at end of file
Added:
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnResponseFactory.java
===================================================================
---
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnResponseFactory.java
(rev 0)
+++
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLAuthnResponseFactory.java 2008-12-11
15:27:48 UTC (rev 144)
@@ -0,0 +1,161 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.identity.federation.api.saml.v2.factories;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+import org.jboss.identity.federation.saml.v2.assertion.AssertionType;
+import org.jboss.identity.federation.saml.v2.assertion.NameIDType;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectConfirmationDataType;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectConfirmationType;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectType;
+import org.jboss.identity.federation.saml.v2.jboss.IDPInfoHolder;
+import org.jboss.identity.federation.saml.v2.jboss.IssuerInfoHolder;
+import org.jboss.identity.federation.saml.v2.jboss.SPInfoHolder;
+import org.jboss.identity.federation.saml.v2.protocol.ObjectFactory;
+import org.jboss.identity.federation.saml.v2.protocol.ResponseType;
+import org.jboss.identity.federation.saml.v2.protocol.StatusCodeType;
+import org.jboss.identity.federation.saml.v2.protocol.StatusType;
+
+/**
+ * Factory for the SAML v2 Authn Response
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Dec 9, 2008
+ */
+public class JBossSAMLAuthnResponseFactory
+{
+ private static String pkgName =
"org.jboss.identity.federation.saml.v2.protocol";
+ private static String schemaLocation =
"schema/saml-schema-protocol-2.0.xsd";
+
+ private static ObjectFactory protocolObjectFactory = new ObjectFactory();
+
+ public static StatusType createStatusType(String statusCodeURI)
+ {
+ StatusCodeType sct = protocolObjectFactory.createStatusCodeType();
+ sct.setValue(statusCodeURI);
+
+ StatusType statusType = protocolObjectFactory.createStatusType();
+ statusType.setStatusCode(sct);
+ return statusType;
+ }
+
+ public static ResponseType createResponseType(String ID, SPInfoHolder sp,
IDPInfoHolder idp, IssuerInfoHolder issuerInfo) throws Exception
+ {
+ ResponseType responseType = protocolObjectFactory.createResponseType();
+ responseType.setVersion(issuerInfo.getSamlVersion());
+
+ //ID
+ responseType.setID(ID);
+ //InResponseTo ID
+ responseType.setInResponseTo(sp.getRequestID());
+ //Destination
+ String responseDestinationURI = sp.getResponseDestinationURI();
+ responseType.setDestination(responseDestinationURI);
+
+ //Issuer
+ NameIDType issuer = issuerInfo.getIssuer();
+ responseType.setIssuer(issuer);
+
+ //Status
+ String statusCode = issuerInfo.getStatusCode();
+ if(statusCode == null)
+ throw new IllegalArgumentException("issuerInfo missing status code");
+
+ responseType.setStatus(createStatusType(statusCode) );
+
+ XMLGregorianCalendar issueInstant = JBossSAMLBaseFactory.getIssueInstant();
+
+ //IssueInstant
+ responseType.setIssueInstant(issueInstant);
+
+ //Create an assertion
+ AssertionType assertionType = JBossSAMLBaseFactory.createAssertion();
+ assertionType.setID("ID_" + JBossSAMLBaseFactory.createUUID());
+ assertionType.setVersion(issuerInfo.getSamlVersion());
+ assertionType.setIssueInstant(issueInstant);
+
+ assertionType.setIssuer(issuer);
+
+ //Create assertion -> subject
+ SubjectType subjectType = JBossSAMLBaseFactory.createSubject();
+
+ //subject -> nameid
+ NameIDType nameIDType = JBossSAMLBaseFactory.createNameID();
+ nameIDType.setFormat(idp.getNameIDFormat());
+ nameIDType.setValue(idp.getNameIDFormatValue());
+
+ JAXBElement<NameIDType> jaxbNameIDType =
JBossSAMLBaseFactory.createNameID(nameIDType);
+ subjectType.getContent().add(jaxbNameIDType);
+
+ SubjectConfirmationType subjectConfirmation =
+
JBossSAMLBaseFactory.createSubjectConfirmation(idp.getSubjectConfirmationMethod());
+ SubjectConfirmationDataType subjectConfirmationData =
+ JBossSAMLBaseFactory.createSubjectConfirmationData(sp.getRequestID(),
+ responseDestinationURI, issueInstant);
+ subjectConfirmation.setSubjectConfirmationData(subjectConfirmationData);
+
+ JAXBElement<SubjectConfirmationType> jaxbSubjectConfirmationType =
+ JBossSAMLBaseFactory.createSubjectConfirmation(subjectConfirmation);
+
+ subjectType.getContent().add(jaxbSubjectConfirmationType);
+
+ assertionType.setSubject(subjectType);
+
+
+ responseType.getAssertionOrEncryptedAssertion().add(assertionType);
+ return responseType;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static ResponseType getResponseType(InputStream is) throws Exception
+ {
+ if(is == null)
+ throw new IllegalArgumentException("inputstream is null");
+
+ Unmarshaller un = JBossSAMLBaseFactory.getValidatingUnmarshaller(pkgName,
schemaLocation);
+ JAXBElement<ResponseType> jaxbAuthnRequestType =
(JAXBElement<ResponseType>) un.unmarshal(is);
+ return jaxbAuthnRequestType.getValue();
+ }
+
+ /**
+ * Marshall the response type to the output stream
+ * <p> <b>Note:</b> JAXB marshaller by default picks up arbitrary
namespace
+ * prefixes (ns2,ns3 etc). The NamespacePrefixMapper is a Sun RI customization
+ * that may be needed (this is a TODO) to get a prefix such as saml, samlp </b>
+ *
+ * @param responseType
+ * @param os
+ * @throws Exception
+ */
+ public static void marshall(ResponseType responseType, OutputStream os) throws
Exception
+ {
+ Marshaller marshaller = JBossSAMLBaseFactory.getValidatingMarshaller(pkgName,
schemaLocation);
+ JAXBElement<ResponseType> jaxb =
protocolObjectFactory.createResponse(responseType);
+ marshaller.marshal(jaxb, os);
+ }
+}
\ No newline at end of file
Modified:
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLBaseFactory.java
===================================================================
---
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLBaseFactory.java 2008-12-11
15:26:54 UTC (rev 143)
+++
identity-federation/trunk/identity-fed-api/src/main/java/org/jboss/identity/federation/api/saml/v2/factories/JBossSAMLBaseFactory.java 2008-12-11
15:27:48 UTC (rev 144)
@@ -22,13 +22,26 @@
package org.jboss.identity.federation.api.saml.v2.factories;
import java.net.URL;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
+import org.jboss.identity.federation.saml.v2.assertion.AssertionType;
+import org.jboss.identity.federation.saml.v2.assertion.NameIDType;
+import org.jboss.identity.federation.saml.v2.assertion.ObjectFactory;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectConfirmationDataType;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectConfirmationType;
+import org.jboss.identity.federation.saml.v2.assertion.SubjectType;
+
/**
* Base methods for the factories
* @author Anil.Saldhana(a)redhat.com
@@ -36,7 +49,130 @@
*/
public class JBossSAMLBaseFactory
{
+ private static ObjectFactory assertionObjectFactory = new ObjectFactory();
+
+ public static AssertionType createAssertion()
+ {
+ return assertionObjectFactory.createAssertionType();
+ }
+
+ public static NameIDType createNameID()
+ {
+ return assertionObjectFactory.createNameIDType();
+ }
+
+ public static JAXBElement<NameIDType> createNameID(NameIDType nameIDType)
+ {
+ return assertionObjectFactory.createNameID(nameIDType);
+ }
+
+ public static SubjectType createSubject()
+ {
+ SubjectType subjectType = assertionObjectFactory.createSubjectType();
+ return subjectType;
+ }
+
+ public static SubjectConfirmationType createSubjectConfirmation(String method)
+ {
+ SubjectConfirmationType sct =
assertionObjectFactory.createSubjectConfirmationType();
+ sct.setMethod(method);
+ return sct;
+ }
+
+ public static JAXBElement<SubjectConfirmationType>
createSubjectConfirmation(SubjectConfirmationType sct)
+ {
+ return assertionObjectFactory.createSubjectConfirmation(sct);
+ }
+
+ public static SubjectConfirmationDataType createSubjectConfirmationData(String
inResponseTo,
+ String destinationURI, XMLGregorianCalendar issueInstant)
+ {
+ SubjectConfirmationDataType subjectConfirmationData =
assertionObjectFactory.createSubjectConfirmationDataType();
+ subjectConfirmationData.setInResponseTo(inResponseTo);
+ subjectConfirmationData.setRecipient(destinationURI);
+ subjectConfirmationData.setNotBefore(issueInstant);
+ subjectConfirmationData.setNotOnOrAfter(issueInstant);
+
+ return subjectConfirmationData;
+ }
+
/**
+ * Get a UUID String
+ * @return
+ */
+ public static String createUUID()
+ {
+ return java.util.UUID.randomUUID().toString();
+ }
+
+ /**
+ * Return the NameIDType for the issuer
+ * @param issuerID
+ * @return
+ */
+ public static NameIDType getIssuer(String issuerID)
+ {
+ NameIDType nid = assertionObjectFactory.createNameIDType();
+ nid.setValue(issuerID);
+ return nid;
+ }
+
+ /**
+ * Returns a XMLGregorianCalendar in the timezone specified.
+ * If the timezone is not valid, then the timezone falls back
+ * to "GMT"
+ * @param timezone
+ * @return
+ * @throws Exception
+ */
+ public static XMLGregorianCalendar getIssueInstant(String timezone) throws Exception
+ {
+ TimeZone tz = TimeZone.getTimeZone(timezone);
+ DatatypeFactory dtf = DatatypeFactory.newInstance();
+
+ GregorianCalendar gc = new GregorianCalendar(tz);
+ XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar(gc);
+
+ return xgc;
+ }
+
+ /**
+ * Get the current instant of time
+ * @return
+ */
+ public static XMLGregorianCalendar getIssueInstant() throws Exception
+ {
+ return getIssueInstant(TimeZone.getDefault().getID());
+ }
+
+ /**
+ * Get the JAXB Marshaller
+ * @param pkgName The package name for the jaxb context
+ * @param schemaLocation location of the schema to validate against
+ * @return Marshaller
+ * @throws Exception
+ */
+ public static Marshaller getValidatingMarshaller(String pkgName, String
schemaLocation)
+ throws Exception
+ {
+ if(pkgName == null)
+ throw new IllegalArgumentException("pkgName is null");
+
+ JAXBContext jc = JAXBContext.newInstance(pkgName);
+ Marshaller marshaller = jc.createMarshaller();
+ //Validate against schema
+ ClassLoader tcl = SecurityActions.getContextClassLoader();
+ URL schemaURL = tcl.getResource(schemaLocation);
+ if(schemaURL == null)
+ throw new IllegalStateException("Schema URL is null:" +
schemaLocation);
+ SchemaFactory scFact =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Schema schema = scFact.newSchema(schemaURL);
+ marshaller.setSchema(schema);
+
+ return marshaller;
+ }
+
+ /**
* Get the JAXB Unmarshaller
* @param pkgName The package name for the jaxb context
* @param schemaLocation location of the schema to validate against
@@ -63,5 +199,4 @@
return unmarshaller;
}
-
}
\ No newline at end of file