View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.webservices;
20  
21  import org.apache.chemistry.opencmis.commons.server.CallContext;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.xml.bind.JAXBContext;
31  import javax.xml.bind.JAXBElement;
32  import javax.xml.bind.JAXBException;
33  import javax.xml.bind.annotation.XmlAccessType;
34  import javax.xml.bind.annotation.XmlAccessorType;
35  import javax.xml.bind.annotation.XmlAnyAttribute;
36  import javax.xml.bind.annotation.XmlAnyElement;
37  import javax.xml.bind.annotation.XmlAttribute;
38  import javax.xml.bind.annotation.XmlElement;
39  import javax.xml.bind.annotation.XmlElementDecl;
40  import javax.xml.bind.annotation.XmlID;
41  import javax.xml.bind.annotation.XmlRegistry;
42  import javax.xml.bind.annotation.XmlSchemaType;
43  import javax.xml.bind.annotation.XmlSeeAlso;
44  import javax.xml.bind.annotation.XmlType;
45  import javax.xml.bind.annotation.XmlValue;
46  import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
47  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
48  import javax.xml.namespace.QName;
49  
50  
51  public class AbstractUsernameTokenAuthHandler
52  {
53      protected static final JAXBContext WSSE_CONTEXT;
54  
55      static
56      {
57          JAXBContext jc = null;
58  
59          try
60          {
61              jc = JAXBContext.newInstance( ObjectFactory.class );
62          }
63          catch ( JAXBException e )
64          {
65              e.printStackTrace(  );
66          }
67  
68          WSSE_CONTEXT = jc;
69      }
70  
71      protected static final String WSSE_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
72      protected static final QName WSSE_SECURITY = new QName( WSSE_NS, "Security" );
73      protected static final QName WSSE_USERNAME_TOKEN = new QName( WSSE_NS, "UsernameToken" );
74      protected static final QName WSSE_PASSWORD = new QName( WSSE_NS, "Password" );
75      protected static final Set<QName> HEADERS = new HashSet<QName>(  );
76  
77      static
78      {
79          HEADERS.add( WSSE_SECURITY );
80      }
81  
82      @SuppressWarnings( "unchecked" )
83      protected Map<String, String> extractUsernamePassword( JAXBElement<SecurityHeaderType> sht )
84      {
85          String username = null;
86          String password = null;
87  
88          for ( Object uno : sht.getValue(  ).getAny(  ) )
89          {
90              if ( ( uno instanceof JAXBElement ) && ( (JAXBElement<?>) uno ).getValue(  ) instanceof UsernameTokenType )
91              {
92                  UsernameTokenType utt = ( (JAXBElement<UsernameTokenType>) uno ).getValue(  );
93                  username = utt.getUsername(  ).getValue(  );
94  
95                  for ( Object po : utt.getAny(  ) )
96                  {
97                      if ( ( po instanceof JAXBElement ) &&
98                              ( (JAXBElement<?>) po ).getValue(  ) instanceof PasswordString )
99                      {
100                         password = ( (JAXBElement<PasswordString>) po ).getValue(  ).getValue(  );
101 
102                         break;
103                     }
104                 }
105 
106                 break;
107             }
108         }
109 
110         Map<String, String> result = null;
111 
112         if ( username != null )
113         {
114             result = new HashMap<String, String>(  );
115             result.put( CallContext.USERNAME, username );
116             result.put( CallContext.PASSWORD, password );
117         }
118 
119         return result;
120     }
121 
122     // --- JAXB classes ---
123     @XmlRegistry
124     public static class ObjectFactory
125     {
126         public ObjectFactory(  )
127         {
128         }
129 
130         public SecurityHeaderType createSecurityHeaderType(  )
131         {
132             return new SecurityHeaderType(  );
133         }
134 
135         public UsernameTokenType createUsernameTokenType(  )
136         {
137             return new UsernameTokenType(  );
138         }
139 
140         public PasswordString createPasswordString(  )
141         {
142             return new PasswordString(  );
143         }
144 
145         public AttributedString createAttributedString(  )
146         {
147             return new AttributedString(  );
148         }
149 
150         @XmlElementDecl( namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "Security" )
151         public JAXBElement<SecurityHeaderType> createSecurity( SecurityHeaderType value )
152         {
153             return new JAXBElement<SecurityHeaderType>( WSSE_SECURITY, SecurityHeaderType.class, null, value );
154         }
155 
156         @XmlElementDecl( namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "UsernameToken" )
157         public JAXBElement<UsernameTokenType> createUsernameToken( UsernameTokenType value )
158         {
159             return new JAXBElement<UsernameTokenType>( WSSE_USERNAME_TOKEN, UsernameTokenType.class, null, value );
160         }
161 
162         @XmlElementDecl( namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "Password" )
163         public JAXBElement<PasswordString> createPassword( PasswordString value )
164         {
165             return new JAXBElement<PasswordString>( WSSE_PASSWORD, PasswordString.class, null, value );
166         }
167     }
168 
169     @XmlAccessorType( XmlAccessType.FIELD )
170     @XmlType( name = "SecurityHeaderType", propOrder = 
171     {
172         "any"}
173      )
174     public static class SecurityHeaderType
175     {
176         @XmlAnyElement( lax = true )
177         protected List<Object> any;
178         @XmlAnyAttribute
179         private final Map<QName, String> otherAttributes = new HashMap<QName, String>(  );
180 
181         public List<Object> getAny(  )
182         {
183             if ( any == null )
184             {
185                 any = new ArrayList<Object>(  );
186             }
187 
188             return this.any;
189         }
190 
191         public Map<QName, String> getOtherAttributes(  )
192         {
193             return otherAttributes;
194         }
195     }
196 
197     @XmlAccessorType( XmlAccessType.FIELD )
198     @XmlType( name = "UsernameTokenType", propOrder = 
199     {
200         "username", "any"}
201      )
202     public static class UsernameTokenType
203     {
204         @XmlElement( name = "Username", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", required = true )
205         protected AttributedString username;
206         @XmlAnyElement( lax = true )
207         protected List<Object> any;
208         @XmlAttribute( name = "Id", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" )
209         @XmlJavaTypeAdapter( CollapsedStringAdapter.class )
210         @XmlID
211         @XmlSchemaType( name = "ID" )
212         protected String id;
213         @XmlAnyAttribute
214         private final Map<QName, String> otherAttributes = new HashMap<QName, String>(  );
215 
216         public AttributedString getUsername(  )
217         {
218             return username;
219         }
220 
221         public void setUsername( AttributedString value )
222         {
223             this.username = value;
224         }
225 
226         public List<Object> getAny(  )
227         {
228             if ( any == null )
229             {
230                 any = new ArrayList<Object>(  );
231             }
232 
233             return this.any;
234         }
235 
236         public String getId(  )
237         {
238             return id;
239         }
240 
241         public void setId( String value )
242         {
243             this.id = value;
244         }
245 
246         public Map<QName, String> getOtherAttributes(  )
247         {
248             return otherAttributes;
249         }
250     }
251 
252     @XmlAccessorType( XmlAccessType.FIELD )
253     @XmlType( name = "PasswordString" )
254     public static class PasswordString extends AttributedString
255     {
256         @XmlAttribute( name = "Type" )
257         @XmlSchemaType( name = "anyURI" )
258         protected String type;
259 
260         public String getType(  )
261         {
262             return type;
263         }
264 
265         public void setType( String value )
266         {
267             this.type = value;
268         }
269     }
270 
271     @XmlAccessorType( XmlAccessType.FIELD )
272     @XmlType( name = "AttributedString", propOrder = 
273     {
274         "value"}
275      )
276     @XmlSeeAlso( {PasswordString.class
277     } )
278     public static class AttributedString
279     {
280         @XmlValue
281         protected String value;
282         @XmlAttribute( name = "Id", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" )
283         @XmlJavaTypeAdapter( CollapsedStringAdapter.class )
284         @XmlID
285         @XmlSchemaType( name = "ID" )
286         protected String id;
287         @XmlAnyAttribute
288         private final Map<QName, String> otherAttributes = new HashMap<QName, String>(  );
289 
290         public String getValue(  )
291         {
292             return value;
293         }
294 
295         public void setValue( String value )
296         {
297             this.value = value;
298         }
299 
300         public String getId(  )
301         {
302             return id;
303         }
304 
305         public void setId( String value )
306         {
307             this.id = value;
308         }
309 
310         public Map<QName, String> getOtherAttributes(  )
311         {
312             return otherAttributes;
313         }
314     }
315 }