File: SecurityHeader.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,276 kB
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (190 lines) | stat: -rw-r--r-- 6,244 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt 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.ws.extensions.security.element;

import java.util.HashMap;
import java.util.LinkedList;

import org.jboss.ws.extensions.security.BinarySecurityTokenValidator;
import org.jboss.ws.extensions.security.Constants;
import org.jboss.ws.extensions.security.KeyResolver;
import org.jboss.ws.extensions.security.SecurityStore;
import org.jboss.ws.extensions.security.Util;
import org.jboss.ws.extensions.security.exception.UnsupportedSecurityTokenException;
import org.jboss.ws.extensions.security.exception.WSSecurityException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


/**
 * <code>SecurityHeader</code> represents the wsse:security element of WS-Security,
 * and is responsible for storing the processing state of a message.
 *
 * @author Jason T. Greene
 * @version $Id: SecurityHeader.java 5034 2007-11-12 14:08:23Z alessio.soldano@jboss.com $
 */
public class SecurityHeader implements SecurityElement
{
   private Document document;

   private Timestamp timestamp;

   private LinkedList<Token> tokens = new LinkedList<Token>();

   private HashMap<Object, Token> sharedTokens = new HashMap<Object, Token>();

   private LinkedList<SecurityProcess> securityProcesses = new LinkedList<SecurityProcess>();

   // Looks like this is only for embedded tokens
   private LinkedList<SecurityTokenReference> securityTokenReferences = new LinkedList<SecurityTokenReference>();

   public SecurityHeader(Document document)
   {
      this.document = document;
   }

   public SecurityHeader(Element element, SecurityStore store) throws WSSecurityException
   {
      document = element.getOwnerDocument();
      KeyResolver resolver = new KeyResolver(store);
      BinarySecurityTokenValidator validator = new BinarySecurityTokenValidator(store);
      Element child = Util.getFirstChildElement(element);
      while  (child != null)
      {
         String tag = child.getLocalName();

         if (tag.equals("BinarySecurityToken"))
         {
            BinarySecurityToken token = BinarySecurityToken.createBinarySecurityToken(child);
            validator.validateToken(token);
            resolver.cacheToken(token);
            tokens.add(token);
         }
         else if (tag.equals("UsernameToken"))
            tokens.add(new UsernameToken(child));
         else if (tag.equals("Timestamp"))
            timestamp = new Timestamp(child);
         else if (tag.equals("Signature"))
            securityProcesses.add(new Signature(child, resolver));
         else if (tag.equals("EncryptedKey"))
            securityProcesses.add(new EncryptedKey(child, resolver));
         else if (tag.equals("ReferenceList"))
            throw new UnsupportedSecurityTokenException("ReferenceLists outside of encrypted keys (shared secrets) are not supported.");

         child = Util.getNextSiblingElement(child);
      }
   }

   public Timestamp getTimestamp()
   {
      return timestamp;
   }

   public void setTimestamp(Timestamp timestamp)
   {
      this.timestamp = timestamp;
   }

   /**
    * @return Returns the securityTokenReferences.
    */
   public LinkedList getSecurityTokenReferences()
   {
      return securityTokenReferences;
   }
   /**
    * @param securityTokenReferences The securityTokenReferences to set.
    */
   public void setSecurityTokenReferences(LinkedList<SecurityTokenReference> securityTokenReferences)
   {
      this.securityTokenReferences = securityTokenReferences;
   }
   /**
    * @return Returns the securityProcesses.
    */
   public LinkedList<SecurityProcess> getSecurityProcesses()
   {
      return securityProcesses;
   }

   /**
    * @param securityProcesses The securityProcesses to set.
    */
   public void setSecurityProcesses(LinkedList<SecurityProcess> securityProcesses)
   {
      this.securityProcesses = securityProcesses;
   }
   /**
    * @return the tokens.
    */
   public LinkedList<Token> getTokens()
   {
      return tokens;
   }

   public void addToken(Token token)
   {
      tokens.addFirst(token);
      Object content = token.getUniqueContent();
      if (content != null)
         sharedTokens.put(content, token);
   }

   public Token getSharedToken(Object uniqueContent)
   {
      if (uniqueContent == null)
         return null;

      return sharedTokens.get(uniqueContent);
   }

   public void addSecurityProcess(SecurityProcess process)
   {
      securityProcesses.addFirst(process);
   }

   public void addSecurityTokenReference(SecurityTokenReference reference)
   {
      securityTokenReferences.addFirst(reference);
   }

   public Element getElement() throws WSSecurityException
   {
      Element element = document.createElementNS(Constants.WSSE_NS, Constants.WSSE_HEADER);
      Util.addNamespace(element, Constants.WSSE_PREFIX, Constants.WSSE_NS);
      Util.addNamespace(element, Constants.WSU_PREFIX, Constants.WSU_NS);

      if (timestamp != null)
         element.appendChild(timestamp.getElement());

      for (Token t : tokens)
         element.appendChild(t.getElement());

      for (SecurityTokenReference r : securityTokenReferences)
         element.appendChild(r.getElement());

      for (SecurityProcess p : securityProcesses)
         element.appendChild(p.getElement());

      return element;
   }
}