001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.wsrp.wssso.common;
015:
016: import java.net.MalformedURLException;
017: import java.net.URL;
018: import java.net.URLEncoder;
019: import java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Properties;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import com.iplanet.sso.SSOToken;
028:
029: import com.sun.ssoadapter.SSOAdapterException;
030: import com.sun.ssoadapter.SSOAdapterFactory;
031: import com.sun.ssoadapter.SSOAdapterSession;
032: import com.sun.ssoadapter.SSOAdapter;
033:
034: import com.sun.portal.log.common.PortalLogger;
035:
036: import com.sun.portal.wsrp.common.IdentityPropagationConstants;
037: import com.sun.portal.wsrp.common.OASISUsernameTokenProfile;
038:
039: public class UserConfiguration implements WSSSOConstants {
040: private SSOAdapterSession ssoSession = null;
041: private List configurations = null;
042: private SSOAdapterFactory factory = null;
043: //private SSOAdapter ssoAdapter = null;
044: private SSOToken token = null;
045:
046: //String to be passed to SSOAdapter to create a new SSOAdapter WSSSOConfiguration
047: // private static final String CHANNEL_NAME = "channelName";
048: public static final int WEBSERVICE_URL = 1;
049: private static final int MARKUP_URL = 0;
050: private String portalId = null;
051:
052: private static Logger debugLogger = PortalLogger
053: .getLogger(UserConfiguration.class);
054:
055: public UserConfiguration(SSOToken userToken) {
056: token = userToken;
057: portalId = System.getProperty("com.sun.portal.portal.id");
058: ssoSession = new SSOAdapterSession(userToken);
059: factory = SSOAdapterFactory.getInstance();
060: configurations = new ArrayList();
061: loadConfigurations();
062:
063: }
064:
065: public void reinit(SSOToken userToken) {
066: if (userToken.equals(token)) {
067: return;
068: }
069: token = userToken;
070: ssoSession = new SSOAdapterSession(userToken);
071: factory = SSOAdapterFactory.getInstance();
072: configurations = new ArrayList();
073: loadConfigurations();
074:
075: }
076:
077: public OASISUsernameTokenProfile getOASISTokenProfile(
078: String markupURL) {
079: SSOAdapter adapter = null;
080: /*if (ssoAdapter != null){
081: adapter = ssoAdapter;
082: }else{*/
083: adapter = searchConfiguration(markupURL, MARKUP_URL);
084: //ssoAdapter = adapter;
085: //}
086: String username = null;
087: String password = null;
088:
089: Properties props = adapter.getProperties();
090: markupURL = props.get(WEBSERVICE_WSDL_URL).toString();
091: Object temp = props.get(WEBSERVICE_USERNAME);
092: if (temp != null) {
093: username = temp.toString();
094: } else {
095: //No username hence there is no Token Profile
096: return null;
097: }
098: temp = props.get(WEBSERVICE_PASSWORD);
099: if (temp != null) {
100: password = temp.toString();
101: }
102: return new OASISUsernameTokenProfile(username, password);
103: }
104:
105: public String getIdentityPropagationType(String markupURL) {
106: SSOAdapter adapter = null;
107: /*if (ssoAdapter != null){
108: adapter = ssoAdapter;
109: }else{*/
110: adapter = searchConfiguration(markupURL, MARKUP_URL);
111: //ssoAdapter = adapter;
112: //}
113: if (adapter == null) {
114: return IdentityPropagationConstants.NO_IDENTITY_PROPAGATION;
115: }
116: String password = null;
117:
118: Properties props = adapter.getProperties();
119: Object idType = props.get(WEBSERVICE_IDENTITY_TYPE);
120:
121: if (idType == null) {
122: //For backward compatability between 7.0 and 7.1
123: //Knock this code when making WSSSO as a separate module
124: WSSSOMigrator consumer = new WSSSOMigrator(token);
125: return consumer.getIdentityPropagationType(adapter,
126: markupURL);
127: }
128: return idType.toString();
129: }
130:
131: //TODO: We need to check the org to which the Consumer belongs too
132: // To avoid consumers at org being merged with consumers at suborg.
133: // See SSOAdapter issue 6415117
134: //TODO: Get the SSOAdapter at the Org and merge with that of the users
135: public List listConfigurations() {
136: List result = new ArrayList();
137: Iterator it = configurations.iterator();
138: String configName = null;
139: SSOAdapter adapter = null;
140: WSSSOConfiguration con = null;
141: while (it.hasNext()) {
142: configName = it.next().toString();
143: try {
144: adapter = factory.getSSOAdapter(configName, ssoSession);
145: } catch (SSOAdapterException ex) {
146: debugLogger.log(Level.SEVERE, "", ex);
147: continue;
148: }
149: con = getConfiguration(adapter);
150: if (con != null) {
151: result.add(con);
152: }
153: }
154: return result;
155:
156: }
157:
158: // Note : This method does a search based on webserviceURL
159: public void updateConfiguration(String wsURL, String userName,
160: String password) throws WSSSOConfigException {
161: SSOAdapter adapter = null;
162: try {
163: URL url = new URL(wsURL);
164: adapter = getSSOAdapter(url);
165: } catch (MalformedURLException me) {
166: throw new WSSSOConfigException(me);
167: }
168:
169: Properties props = adapter.getProperties();
170: props.put(WEBSERVICE_WSDL_URL, wsURL);
171: props.put(WEBSERVICE_USERNAME, userName);
172: props.put(WEBSERVICE_PASSWORD, password);
173: try {
174: SSOAdapter newssoAdapter = (SSOAdapter) factory
175: .setSSOAdapter(adapter, props, ssoSession);
176: Properties props1 = newssoAdapter.getProperties();
177: String markupURL = props1.get(WEBSERVICE_WSDL_URL)
178: .toString();
179: String username = props1.get(WEBSERVICE_USERNAME)
180: .toString();
181: Object password1 = props1.get(WEBSERVICE_PASSWORD);
182: if (password1 != null) {
183: password = password1.toString();
184: }
185: } catch (Exception e) {
186: throw new WSSSOConfigException(e);
187: }
188:
189: }
190:
191: // Note : This method does a search based on webserviceURL
192: public void deleteConfiguration(String wsURL)
193: throws WSSSOConfigException {
194:
195: try {
196: URL url = new URL(wsURL);
197: SSOAdapter adapter = getSSOAdapter(url);
198: factory.removeConfiguration(adapter.getName(), ssoSession);
199: } catch (SSOAdapterException se) {
200: throw new WSSSOConfigException(se);
201: } catch (MalformedURLException me) {
202: throw new WSSSOConfigException(me);
203: }
204:
205: }
206:
207: private SSOAdapter getSSOAdapter(URL wsURL)
208: throws WSSSOConfigException {
209: String configName = null;
210: SSOAdapter adapter = null;
211:
212: int port = wsURL.getPort();
213: if (port == -1) {
214: port = wsURL.getDefaultPort();
215: }
216: String newURL = wsURL.getProtocol() + "://" + wsURL.getHost()
217: + ":" + port + wsURL.getPath();
218: try {
219: String encoded = URLEncoder.encode(newURL, "UTF-8");
220: configName = CONFIG_NAME + SEPARATOR + portalId + SEPARATOR
221: + encoded;
222: adapter = factory.getSSOAdapter(configName, ssoSession);
223: return adapter;
224: } catch (Exception e) {
225: throw new WSSSOConfigException(e);
226: }
227: }
228:
229: private WSSSOConfiguration getConfiguration(SSOAdapter adapter) {
230: WSSSOConfiguration config = null;
231: Object temp = null;
232:
233: if (adapter != null) {
234: Properties props = adapter.getProperties();
235: config = new WSSSOConfiguration();
236:
237: temp = props.get(WEBSERVICE_IDENTITY_TYPE);
238: if (temp != null) {
239: config.setIdentityType(temp.toString());
240: }
241:
242: temp = props.get(WEBSERVICE_NAME);
243: if (temp != null) {
244: config.setWSName(temp.toString());
245: }
246:
247: temp = props.get(WEBSERVICE_ENDPOINT_URLS);
248: if (temp != null) {
249: config.setEndPointURLs(temp.toString());
250: }
251:
252: temp = props.get(WEBSERVICE_WSDL_URL);
253: if (temp != null) {
254: config.setWebServiceURL(temp.toString());
255: }
256:
257: temp = props.get(WEBSERVICE_USERNAME);
258: if (temp != null) {
259: config.setUserName(temp.toString());
260: }
261:
262: temp = props.get(WEBSERVICE_PASSWORD);
263: if (temp != null) {
264: config.setPassword(temp.toString());
265: }
266:
267: return config;
268: }
269: return null;
270: }
271:
272: private SSOAdapter searchConfiguration(String url, int urlType) {
273: Iterator it = configurations.iterator();
274: String configName = null;
275: SSOAdapter adapter = null;
276: while (it.hasNext()) {
277: configName = it.next().toString();
278: try {
279: adapter = factory.getSSOAdapter(configName, ssoSession);
280: } catch (SSOAdapterException ex) {
281: debugLogger.log(Level.SEVERE, "", ex);
282: }
283: if (match(adapter, url, urlType)) {
284: return adapter;
285: }
286: }
287: return null;
288: }
289:
290: private boolean match(SSOAdapter adapter, String url, int urlType) {
291: Properties props = adapter.getProperties();
292: List userPropertiesList = adapter.getUserPropertiesList();
293: if (urlType == MARKUP_URL) {
294: String endPointURL = null;
295: //TODO: Revisit end point URLS parse and find the URLS
296: Object temp = props.get(WEBSERVICE_ENDPOINT_URLS);
297: if (temp != null) {
298: endPointURL = temp.toString();
299: if (endPointURL.indexOf(url) != -1) {
300: return true;
301: }
302: }
303: } else if (urlType == WEBSERVICE_URL) {
304: String wsURL = null;
305: //TODO: Revisit URL comparision
306: Object temp = props.get(WEBSERVICE_WSDL_URL);
307: if (temp != null) {
308: wsURL = temp.toString();
309: if (wsURL.indexOf(url) != -1) {
310: return true;
311: }
312: }
313:
314: }
315: return false;
316: }
317:
318: private void loadConfigurations() {
319:
320: Enumeration names = factory.getConfigurationNames(ssoSession);
321: String name = null;
322: while (names.hasMoreElements()) {
323: name = names.nextElement().toString();
324: if (name.startsWith(CONFIG_MATCH_STRING)) {
325: configurations.add(name);
326: }
327: }
328: }
329: }
|