Author: coheigea
Date: Mon Jun 13 11:48:42 2011
New Revision: 1135101

URL: http://svn.apache.org/viewvc?rev=1135101&view=rev
Log:
Some improvements for SAML2 token creation
 - Added the ability to set the NameFormat on the Subject NameID
 - Skipped adding an empty SubjectConfirmationData element
 - Added the ability to configure an AudienceRestriction URI.

Modified:
    
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/AttributeBean.java
    
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/ConditionsBean.java
    
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/builder/SAML2ComponentBuilder.java

Modified: 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/AttributeBean.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/AttributeBean.java?rev=1135101&r1=1135100&r2=1135101&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/AttributeBean.java
 (original)
+++ 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/AttributeBean.java
 Mon Jun 13 11:48:42 2011
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 public class AttributeBean {
     private String simpleName;
     private String qualifiedName;
+    private String nameFormat;
     private List<String> attributeValues;
 
     /**
@@ -70,6 +71,24 @@ public class AttributeBean {
     public void setSimpleName(String simpleName) {
         this.simpleName = simpleName;
     }
+    
+    /**
+     * Method getNameFormat returns the nameFormat of this SamlAttribute object
+     * 
+     * @return he nameFormat of this SamlAttribute object
+     */
+    public String getNameFormat() {
+        return nameFormat;
+    }
+    
+    /**
+     * Method setNameFormat sets the nameFormat of this SamlAttribute object.
+     *
+     * @param nameFormat the nameFormat of this SamlAttribute object.
+     */
+    public void setNameFormat(String nameFormat) {
+        this.nameFormat = nameFormat;
+    }
 
     /**
      * Method getQualifiedName returns the qualifiedName of this SamlAttribute 
object.
@@ -126,6 +145,12 @@ public class AttributeBean {
             return false;
         }
         
+        if (nameFormat == null && that.nameFormat != null) {
+            return false;
+        } else if (nameFormat != null && !nameFormat.equals(that.nameFormat)) {
+            return false;
+        }
+        
         if (simpleName == null && that.simpleName != null) {
             return false;
         } else if (simpleName != null && !simpleName.equals(that.simpleName)) {
@@ -144,6 +169,9 @@ public class AttributeBean {
         if (qualifiedName != null) {
             result = 31 * result + qualifiedName.hashCode();
         }
+        if (nameFormat != null) {
+            result = 31 * result + nameFormat.hashCode();
+        }
         if (attributeValues != null) {
             result = 31 * result + attributeValues.hashCode();
         }

Modified: 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/ConditionsBean.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/ConditionsBean.java?rev=1135101&r1=1135100&r2=1135101&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/ConditionsBean.java
 (original)
+++ 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/bean/ConditionsBean.java
 Mon Jun 13 11:48:42 2011
@@ -32,6 +32,7 @@ public class ConditionsBean {
     private DateTime notBefore;
     private DateTime notAfter;
     private int tokenPeriodMinutes;
+    private String audienceURI;
 
     /**
      * Constructor ConditionsBean creates a new ConditionsBean instance.
@@ -117,6 +118,24 @@ public class ConditionsBean {
     public void setTokenPeriodMinutes(int tokenPeriodMinutes) {
         this.tokenPeriodMinutes = tokenPeriodMinutes;
     }
+    
+    /**
+     * Get the audienceURI instance
+     *
+     * @return the audienceURI instance
+     */
+    public String getAudienceURI() {
+        return audienceURI;
+    }
+
+    /**
+     * Set the audienceURI instance
+     *
+     * @param audienceURI the audienceURI instance to set
+     */
+    public void setAudienceURI(String audienceURI) {
+        this.audienceURI = audienceURI;
+    }
 
     /**
      * Method equals ...
@@ -144,6 +163,12 @@ public class ConditionsBean {
         } else if (notAfter != null && !notAfter.equals(that.notAfter)) {
             return false; 
         }
+        
+        if (audienceURI == null && that.audienceURI != null) {
+            return false;
+        } else if (audienceURI != null && 
!audienceURI.equals(that.audienceURI)) {
+            return false; 
+        }
 
         return true;
     }
@@ -160,6 +185,9 @@ public class ConditionsBean {
         if (notAfter != null) {
             result = 31 * result + notAfter.hashCode();
         }
+        if (audienceURI != null) {
+            result = 31 * result + audienceURI.hashCode();
+        }
         return result;
     }
 }

Modified: 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/builder/SAML2ComponentBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/builder/SAML2ComponentBuilder.java?rev=1135101&r1=1135100&r2=1135101&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/builder/SAML2ComponentBuilder.java
 (original)
+++ 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/saml/ext/builder/SAML2ComponentBuilder.java
 Mon Jun 13 11:48:42 2011
@@ -195,6 +195,13 @@ public class SAML2ComponentBuilder {
             conditions.setNotBefore(newNotBefore);
             
conditions.setNotOnOrAfter(newNotBefore.plusMinutes(tokenPeriodMinutes));
         }
+        
+        if (conditionsBean.getAudienceURI() != null) {
+            AudienceRestriction audienceRestriction = 
+                createAudienceRestriction(conditionsBean.getAudienceURI());
+            conditions.getAudienceRestrictions().add(audienceRestriction);
+        }
+        
         return conditions;
     }
 
@@ -294,12 +301,28 @@ public class SAML2ComponentBuilder {
      * @param name         of type String
      * @param values       of type ArrayList
      * @return a SAML2 Attribute
+     * @deprecated
      */
     public static Attribute createAttribute(String friendlyName, String name, 
List<String> values) {
+        return createAttribute(friendlyName, name, null, values);
+    }
+    
+    /**
+     * Create a SAML2 Attribute
+     *
+     * @param friendlyName of type String
+     * @param name         of type String
+     * @param nameFormat   of type String
+     * @param values       of type ArrayList
+     * @return a SAML2 Attribute
+     */
+    public static Attribute createAttribute(
+        String friendlyName, String name, String nameFormat, List<String> 
values
+    ) {
         if (stringBuilder == null) {
             stringBuilder = 
(XSStringBuilder)builderFactory.getBuilder(XSString.TYPE_NAME);
         }
-        Attribute attribute = createAttribute(friendlyName, name);
+        Attribute attribute = createAttribute(friendlyName, name, nameFormat);
         for (String value : values) {
             XSString attributeValue = 
                 stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, 
XSString.TYPE_NAME);
@@ -328,13 +351,16 @@ public class SAML2ComponentBuilder {
         NameID nameID = SAML2ComponentBuilder.createNameID(subjectBean);
         subject.setNameID(nameID);
         
-        SubjectConfirmationData subjectConfData = 
-            SAML2ComponentBuilder.createSubjectConfirmationData(
-                null, 
-                null, 
-                null, 
-                subjectBean.getKeyInfo() 
-            );
+        SubjectConfirmationData subjectConfData = null;
+        if (subjectBean.getKeyInfo() != null) {
+            subjectConfData = 
+                SAML2ComponentBuilder.createSubjectConfirmationData(
+                    null, 
+                    null, 
+                    null, 
+                    subjectBean.getKeyInfo() 
+                );
+        }
         
         String confirmationMethodStr = 
subjectBean.getSubjectConfirmationMethod();
         if (confirmationMethodStr == null) {
@@ -476,6 +502,7 @@ public class SAML2ComponentBuilder {
                         createAttribute(
                             values.getSimpleName(), 
                             values.getQualifiedName(),
+                            values.getNameFormat(),
                             values.getAttributeValues()
                         );
                     attributeStatement.getAttributes().add(samlAttribute);
@@ -497,9 +524,22 @@ public class SAML2ComponentBuilder {
      * @param friendlyName of type String
      * @param name of type String
      * @return an Attribute object
+     * @deprecated
      */
-    @SuppressWarnings("unchecked")
     public static Attribute createAttribute(String friendlyName, String name) {
+        return createAttribute(friendlyName, name, (String)null);
+    }
+    
+    /**
+     * Create an Attribute object.
+     *
+     * @param friendlyName of type String
+     * @param name of type String
+     * @param nameFormat of type String
+     * @return an Attribute object
+     */
+    @SuppressWarnings("unchecked")
+    public static Attribute createAttribute(String friendlyName, String name, 
String nameFormat) {
         if (attributeBuilder == null) {
             attributeBuilder = (SAMLObjectBuilder<Attribute>)
                 builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
@@ -507,7 +547,11 @@ public class SAML2ComponentBuilder {
         
         Attribute attribute = attributeBuilder.buildObject();
         attribute.setFriendlyName(friendlyName);
-        attribute.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_URI);
+        if (nameFormat == null) {
+            attribute.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_URI);
+        } else {
+            attribute.setNameFormat(nameFormat);
+        }
         attribute.setName(name);
         return attribute;
     }


Reply via email to