This project has retired. For details please refer to its Attic page.
AbstractUsernameTokenAuthHandler xref

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 java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.xml.bind.JAXBContext;
29  import javax.xml.bind.JAXBElement;
30  import javax.xml.bind.JAXBException;
31  import javax.xml.bind.annotation.XmlAccessType;
32  import javax.xml.bind.annotation.XmlAccessorType;
33  import javax.xml.bind.annotation.XmlAnyAttribute;
34  import javax.xml.bind.annotation.XmlAnyElement;
35  import javax.xml.bind.annotation.XmlAttribute;
36  import javax.xml.bind.annotation.XmlElement;
37  import javax.xml.bind.annotation.XmlElementDecl;
38  import javax.xml.bind.annotation.XmlID;
39  import javax.xml.bind.annotation.XmlRegistry;
40  import javax.xml.bind.annotation.XmlSchemaType;
41  import javax.xml.bind.annotation.XmlSeeAlso;
42  import javax.xml.bind.annotation.XmlType;
43  import javax.xml.bind.annotation.XmlValue;
44  import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
45  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
46  import javax.xml.namespace.QName;
47  
48  import org.apache.chemistry.opencmis.commons.server.CallContext;
49  
50  public class AbstractUsernameTokenAuthHandler {
51  
52      protected static final JAXBContext WSSE_CONTEXT;
53      static {
54          JAXBContext jc = null;
55          try {
56              jc = JAXBContext.newInstance(ObjectFactory.class);
57          } catch (JAXBException e) {
58              e.printStackTrace();
59          }
60          WSSE_CONTEXT = jc;
61      }
62  
63      protected static final String WSSE_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
64      protected static final QName WSSE_SECURITY = new QName(WSSE_NS, "Security");
65      protected static final QName WSSE_USERNAME_TOKEN = new QName(WSSE_NS, "UsernameToken");
66      protected static final QName WSSE_PASSWORD = new QName(WSSE_NS, "Password");
67  
68      protected static final Set<QName> HEADERS = new HashSet<QName>();
69      static {
70          HEADERS.add(WSSE_SECURITY);
71      }
72  
73      @SuppressWarnings("unchecked")
74      protected Map<String, String> extractUsernamePassword(JAXBElement<SecurityHeaderType> sht) {
75          String username = null;
76          String password = null;
77  
78          for (Object uno : sht.getValue().getAny()) {
79              if ((uno instanceof JAXBElement) && ((JAXBElement<?>) uno).getValue() instanceof UsernameTokenType) {
80                  UsernameTokenType utt = ((JAXBElement<UsernameTokenType>) uno).getValue();
81                  username = utt.getUsername().getValue();
82  
83                  for (Object po : utt.getAny()) {
84                      if ((po instanceof JAXBElement) && ((JAXBElement<?>) po).getValue() instanceof PasswordString) {
85                          password = ((JAXBElement<PasswordString>) po).getValue().getValue();
86                          break;
87                      }
88                  }
89  
90                  break;
91              }
92          }
93  
94          Map<String, String> result = null;
95  
96          if (username != null) {
97              result = new HashMap<String, String>();
98              result.put(CallContext.USERNAME, username);
99              result.put(CallContext.PASSWORD, password);
100         }
101 
102         return result;
103     }
104 
105     // --- JAXB classes ---
106 
107     @XmlRegistry
108     public static class ObjectFactory {
109 
110         public ObjectFactory() {
111         }
112 
113         public SecurityHeaderType createSecurityHeaderType() {
114             return new SecurityHeaderType();
115         }
116 
117         public UsernameTokenType createUsernameTokenType() {
118             return new UsernameTokenType();
119         }
120 
121         public PasswordString createPasswordString() {
122             return new PasswordString();
123         }
124 
125         public AttributedString createAttributedString() {
126             return new AttributedString();
127         }
128 
129         @XmlElementDecl(namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "Security")
130         public JAXBElement<SecurityHeaderType> createSecurity(SecurityHeaderType value) {
131             return new JAXBElement<SecurityHeaderType>(WSSE_SECURITY, SecurityHeaderType.class, null, value);
132         }
133 
134         @XmlElementDecl(namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "UsernameToken")
135         public JAXBElement<UsernameTokenType> createUsernameToken(UsernameTokenType value) {
136             return new JAXBElement<UsernameTokenType>(WSSE_USERNAME_TOKEN, UsernameTokenType.class, null, value);
137         }
138 
139         @XmlElementDecl(namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", name = "Password")
140         public JAXBElement<PasswordString> createPassword(PasswordString value) {
141             return new JAXBElement<PasswordString>(WSSE_PASSWORD, PasswordString.class, null, value);
142         }
143 
144     }
145 
146     @XmlAccessorType(XmlAccessType.FIELD)
147     @XmlType(name = "SecurityHeaderType", propOrder = { "any" })
148     public static class SecurityHeaderType {
149 
150         @XmlAnyElement(lax = true)
151         protected List<Object> any;
152         @XmlAnyAttribute
153         private final Map<QName, String> otherAttributes = new HashMap<QName, String>();
154 
155         public List<Object> getAny() {
156             if (any == null) {
157                 any = new ArrayList<Object>();
158             }
159             return this.any;
160         }
161 
162         public Map<QName, String> getOtherAttributes() {
163             return otherAttributes;
164         }
165 
166     }
167 
168     @XmlAccessorType(XmlAccessType.FIELD)
169     @XmlType(name = "UsernameTokenType", propOrder = { "username", "any" })
170     public static class UsernameTokenType {
171 
172         @XmlElement(name = "Username", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", required = true)
173         protected AttributedString username;
174         @XmlAnyElement(lax = true)
175         protected List<Object> any;
176         @XmlAttribute(name = "Id", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
177         @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
178         @XmlID
179         @XmlSchemaType(name = "ID")
180         protected String id;
181         @XmlAnyAttribute
182         private final Map<QName, String> otherAttributes = new HashMap<QName, String>();
183 
184         public AttributedString getUsername() {
185             return username;
186         }
187 
188         public void setUsername(AttributedString value) {
189             this.username = value;
190         }
191 
192         public List<Object> getAny() {
193             if (any == null) {
194                 any = new ArrayList<Object>();
195             }
196             return this.any;
197         }
198 
199         public String getId() {
200             return id;
201         }
202 
203         public void setId(String value) {
204             this.id = value;
205         }
206 
207         public Map<QName, String> getOtherAttributes() {
208             return otherAttributes;
209         }
210     }
211 
212     @XmlAccessorType(XmlAccessType.FIELD)
213     @XmlType(name = "PasswordString")
214     public static class PasswordString extends AttributedString {
215 
216         @XmlAttribute(name = "Type")
217         @XmlSchemaType(name = "anyURI")
218         protected String type;
219 
220         public String getType() {
221             return type;
222         }
223 
224         public void setType(String value) {
225             this.type = value;
226         }
227     }
228 
229     @XmlAccessorType(XmlAccessType.FIELD)
230     @XmlType(name = "AttributedString", propOrder = { "value" })
231     @XmlSeeAlso({ PasswordString.class })
232     public static class AttributedString {
233 
234         @XmlValue
235         protected String value;
236         @XmlAttribute(name = "Id", namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
237         @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
238         @XmlID
239         @XmlSchemaType(name = "ID")
240         protected String id;
241         @XmlAnyAttribute
242         private final Map<QName, String> otherAttributes = new HashMap<QName, String>();
243 
244         public String getValue() {
245             return value;
246         }
247 
248         public void setValue(String value) {
249             this.value = value;
250         }
251 
252         public String getId() {
253             return id;
254         }
255 
256         public void setId(String value) {
257             this.id = value;
258         }
259 
260         public Map<QName, String> getOtherAttributes() {
261             return otherAttributes;
262         }
263     }
264 }