001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.security.secconv.impl;
038:
039: import com.sun.xml.ws.runtime.util.Session;
040: import com.sun.xml.ws.runtime.util.SessionManager;
041: import com.sun.xml.ws.security.IssuedTokenContext;
042: import com.sun.xml.ws.security.SecurityContextToken;
043: import com.sun.xml.ws.security.SecurityContextTokenInfo;
044: import com.sun.xml.ws.security.secconv.WSSCConstants;
045: import com.sun.xml.ws.security.impl.IssuedTokenContextImpl;
046: import com.sun.xml.ws.security.secconv.WSSCElementFactory;
047:
048: import com.sun.xml.ws.security.trust.elements.str.Reference;
049: import com.sun.xml.ws.security.trust.elements.str.SecurityTokenReference;
050:
051: import java.net.URI;
052: import java.net.URISyntaxException;
053: import java.util.Date;
054: import java.util.HashMap;
055: import java.util.Map;
056: import java.util.Set;
057:
058: /**
059: * The </code>SecurityContextTokenInfo</code> class represents security parameters
060: * which will be saved in the <code>Session</code> object so that whenever the endpoint
061: * crashes the security negotiations can be resumed from its original state and no new
062: * negotiations need to be done.
063: *
064: * @author Manveen Kaur (manveen.kaur@sun.com)
065: */
066: public class SecurityContextTokenInfoImpl implements
067: SecurityContextTokenInfo {
068:
069: String identifier = null;
070: String extId = null;
071: byte[] secret = null;
072: Map<String, byte[]> secretMap = new HashMap<String, byte[]>();
073: Date creationTime = null;
074: Date expirationTime = null;
075:
076: private static WSSCElementFactory factory = WSSCElementFactory
077: .newInstance();
078:
079: // default constructor
080: public SecurityContextTokenInfoImpl() {
081: //empty constructor
082: }
083:
084: public String getIdentifier() {
085: return identifier;
086: }
087:
088: public void setIdentifier(final String identifier) {
089: this .identifier = identifier;
090: }
091:
092: /*
093: * external Id corresponds to the wsu Id on the token.
094: */
095: public String getExternalId() {
096: return extId;
097: }
098:
099: public void setExternalId(final String externalId) {
100: this .extId = externalId;
101: }
102:
103: public byte[] getSecret() {
104: byte[] newSecret = new byte[secret.length];
105: System.arraycopy(secret, 0, newSecret, 0, secret.length);
106: return newSecret;
107: }
108:
109: public byte[] getInstanceSecret(final String instance) {
110: return secretMap.get(instance);
111: }
112:
113: public void addInstance(final String instance, final byte[] key) {
114: byte[] newKey = new byte[key.length];
115: System.arraycopy(key, 0, newKey, 0, key.length);
116: if (instance == null) {
117: this .secret = newKey;
118: } else {
119: secretMap.put(instance, newKey);
120: }
121: }
122:
123: public Date getCreationTime() {
124: return new Date(creationTime.getTime());
125: }
126:
127: public void setCreationTime(final Date creationTime) {
128: this .creationTime = new Date(creationTime.getTime());
129: }
130:
131: public Date getExpirationTime() {
132: return new Date(expirationTime.getTime());
133: }
134:
135: public void setExpirationTime(final Date expirationTime) {
136: this .expirationTime = new Date(expirationTime.getTime());
137: }
138:
139: public Set getInstanceKeys() {
140: return null;
141: // TBD
142: //return secretMap.keySet();
143: }
144:
145: public IssuedTokenContext getIssuedTokenContext() {
146:
147: final IssuedTokenContext itc = new IssuedTokenContextImpl();
148: itc.setCreationTime(this .getCreationTime());
149: itc.setExpirationTime(this .getExpirationTime());
150: itc.setProofKey(this .getSecret());
151: itc.setSecurityContextTokenInfo(this );
152:
153: // create security token based on id and extId
154: URI uri = URI.create(this .getIdentifier());
155:
156: final SecurityContextToken token = factory
157: .createSecurityContextToken(uri, null, this
158: .getExternalId());
159: itc.setSecurityToken(token);
160:
161: // Create references
162: final SecurityTokenReference attachedReference = createSecurityTokenReference(
163: token.getWsuId(), false);
164: //RequestedAttachedReference rar = factory.createRequestedAttachedReference(attachedReference);
165: final SecurityTokenReference unattachedRef = createSecurityTokenReference(
166: token.getIdentifier().toString(), true);
167: //RequestedUnattachedReference rur = factory.createRequestedUnattachedReference(unattachedRef);
168:
169: itc.setAttachedSecurityTokenReference(attachedReference);
170: itc.setUnAttachedSecurityTokenReference(unattachedRef);
171:
172: return itc;
173: }
174:
175: private SecurityTokenReference createSecurityTokenReference(
176: final String id, final boolean unattached) {
177: final String uri = (unattached ? id : "#" + id);
178: final Reference ref = factory.createDirectReference(
179: WSSCConstants.SECURITY_CONTEXT_TOKEN_TYPE, uri);
180: return factory.createSecurityTokenReference(ref);
181: }
182:
183: //public static IssuedTokenContext getIssuedTokenContext(SecurityTokenReference reference) {
184: public IssuedTokenContext getIssuedTokenContext(
185: final com.sun.xml.ws.security.SecurityTokenReference reference) {
186: // get str id -> get Session corresponding to id
187: // from session get corresponding SCTInfo ->
188: // return sctinfo's IssuedTokenContext.
189: final String id = reference.getId();
190: final Session session = SessionManager.getSessionManager()
191: .getSession(id);
192: return session.getSecurityInfo().getIssuedTokenContext();
193: }
194:
195: }
|