View Javadoc

1   /*
2    * Copyright 2009 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.ws.wssecurity.util;
18  
19  import java.util.List;
20  
21  import org.opensaml.ws.wssecurity.IdBearing;
22  import org.opensaml.ws.wssecurity.TokenTypeBearing;
23  import org.opensaml.ws.wssecurity.UsageBearing;
24  import org.opensaml.xml.AttributeExtensibleXMLObject;
25  import org.opensaml.xml.XMLObject;
26  import org.opensaml.xml.util.AttributeMap;
27  import org.opensaml.xml.util.DatatypeHelper;
28  import org.opensaml.xml.util.LazyList;
29  import org.opensaml.xml.util.XMLHelper;
30  
31  /**
32   * Helper methods for working with WS-Security.
33   */
34  public final class WSSecurityHelper {
35  
36      /**
37       * Private constructor.
38       */
39      private WSSecurityHelper() {
40      }
41      
42      /**
43       * Adds a <code>wsu:Id</code> attribute to the given SOAP object.
44       * 
45       * @param soapObject the SOAP object to add the attribute to
46       * @param id the Id value
47       */
48      public static void addWSUId(XMLObject soapObject, String id) {
49          if (soapObject instanceof IdBearing) {
50              ((IdBearing)soapObject).setWSUId(id);
51          } else if (soapObject instanceof AttributeExtensibleXMLObject) {
52              ((AttributeExtensibleXMLObject)soapObject).getUnknownAttributes()
53                  .put(IdBearing.WSU_ID_ATTR_NAME, id);
54          } else {
55              throw new IllegalArgumentException("Specified object was neither IdBearing nor AttributeExtensible");
56          }
57      }
58      
59      /**
60       * Gets the <code>wsu:Id</code> attribute from a given SOAP object.
61       * 
62       * @param soapObject the SOAP object to add the attribute to
63       * 
64       * @return the value of the Id attribute, or null if not present
65       */
66      public static String getWSUId(XMLObject soapObject) {
67          String value = null;
68          if (soapObject instanceof IdBearing) {
69              value = DatatypeHelper.safeTrimOrNullString(((IdBearing)soapObject).getWSUId());
70              if (value != null) {
71                  return value;
72              }
73          }
74          if (soapObject instanceof AttributeExtensibleXMLObject) {
75              value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)soapObject)
76                          .getUnknownAttributes().get(IdBearing.WSU_ID_ATTR_NAME));
77              return value;
78          }
79          return null;
80      }
81      
82      /**
83       * Adds a <code>wsse11:TokenType</code> attribute to the given SOAP object.
84       * 
85       * @param soapObject the SOAP object to add the attribute to
86       * @param tokenType the tokenType value
87       */
88      public static void addWSSE11TokenType(XMLObject soapObject, String tokenType) {
89          if (soapObject instanceof TokenTypeBearing) {
90              ((TokenTypeBearing)soapObject).setWSSE11TokenType(tokenType);
91          } else if (soapObject instanceof AttributeExtensibleXMLObject) {
92              ((AttributeExtensibleXMLObject)soapObject).getUnknownAttributes()
93                  .put(TokenTypeBearing.WSSE11_TOKEN_TYPE_ATTR_NAME, tokenType);
94          } else {
95              throw new IllegalArgumentException("Specified object was neither TokenTypeBearing nor AttributeExtensible");
96          }
97      }
98      
99      /**
100      * Gets the <code>wsse11:TokenType</code> attribute from a given SOAP object.
101      * 
102      * @param soapObject the SOAP object to add the attribute to
103      * 
104      * @return the value of the tokenType attribute, or null if not present
105      */
106     public static String getWSSE11TokenType(XMLObject soapObject) {
107         String value = null;
108         if (soapObject instanceof TokenTypeBearing) {
109             value = DatatypeHelper.safeTrimOrNullString(((TokenTypeBearing)soapObject).getWSSE11TokenType());
110             if (value != null) {
111                 return value;
112             }
113         }
114         if (soapObject instanceof AttributeExtensibleXMLObject) {
115             value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)soapObject)
116                         .getUnknownAttributes().get(TokenTypeBearing.WSSE11_TOKEN_TYPE_ATTR_NAME));
117             return value;
118         }
119         return null;
120     }
121     
122     /**
123      * Adds a single <code>wsse:Usage</code> value to the given SOAP object. If an existing <code>wsse:Usage</code>
124      * attribute is present, the given usage will be added to the existing list.
125      * 
126      * @param soapObject the SOAP object to add the attribute to
127      * @param usage the usage to add
128      */
129     public static void addWSSEUsage(XMLObject soapObject, String usage) {
130         if (soapObject instanceof UsageBearing) {
131             UsageBearing usageBearing = (UsageBearing) soapObject;
132             List<String> list = usageBearing.getWSSEUsages();
133             if (list == null) {
134                 list = new LazyList<String>();
135                 usageBearing.setWSSEUsages(list);
136             }
137             list.add(usage);
138         } else if (soapObject instanceof AttributeExtensibleXMLObject) {
139             AttributeMap am =  ((AttributeExtensibleXMLObject)soapObject).getUnknownAttributes();
140             String list = am.get(UsageBearing.WSSE_USAGE_ATTR_NAME);
141             if (list == null) {
142                 list = usage;
143             } else {
144                 list = list + " " + usage;
145             }
146             am.put(UsageBearing.WSSE_USAGE_ATTR_NAME, list);
147         } else {
148             throw new IllegalArgumentException("Specified object was neither UsageBearing nor AttributeExtensible");
149         }
150     }
151     
152     /**
153      * Adds a <code>wsse:Usage</code> attribute to the given SOAP object.
154      * 
155      * @param soapObject the SOAP object to add the attribute to
156      * @param usages the list of usages to add
157      */
158     public static void addWSSEUsages(XMLObject soapObject, List<String> usages) {
159         if (soapObject instanceof UsageBearing) {
160             ((UsageBearing)soapObject).setWSSEUsages(usages);
161         } else if (soapObject instanceof AttributeExtensibleXMLObject) {
162             ((AttributeExtensibleXMLObject)soapObject).getUnknownAttributes()
163                 .put(UsageBearing.WSSE_USAGE_ATTR_NAME, 
164                         DatatypeHelper.listToStringValue(usages, " "));
165         } else {
166             throw new IllegalArgumentException("Specified object was neither UsageBearing nor AttributeExtensible");
167         }
168     }
169     
170     /**
171      * Gets the list value of the <code>wsse:Usage</code> attribute from the given SOAP object.
172      * 
173      * @param soapObject the SOAP object to add the attribute to
174      * 
175      * @return the list of usages, or null if not present
176      */
177     public static List<String> getWSSEUsages(XMLObject soapObject) {
178         if (soapObject instanceof UsageBearing) {
179             List<String> value = ((UsageBearing)soapObject).getWSSEUsages();
180             if (value != null) {
181                 return value;
182             }
183         }
184         if (soapObject instanceof AttributeExtensibleXMLObject) {
185             String value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)soapObject)
186                     .getUnknownAttributes().get(UsageBearing.WSSE_USAGE_ATTR_NAME));
187             if (value != null) {
188                 DatatypeHelper.stringToList(value, XMLHelper.LIST_DELIMITERS);
189             }
190         }
191         return null;
192     }
193 }