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.producer;
015:
016: import com.iplanet.sso.SSOException;
017: import com.iplanet.sso.SSOToken;
018: import com.iplanet.sso.SSOTokenManager;
019: import com.sun.portal.log.common.PortalLogger;
020: import com.sun.portal.util.SSOUtil;
021: import com.sun.portal.wsrp.common.OASISUsernameTokenProfile;
022: import com.sun.portal.wsrp.common.IdentityPropagationConstants;
023: import com.sun.portal.wsrp.common.WSRPSpecKeys;
024: import com.sun.portal.wsrp.common.stubs.Extension;
025: import com.sun.portal.wsrp.common.stubs.UserContext;
026: import com.sun.portal.wsrp.producer.filter.ProducerThreadLocalizer;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import javax.xml.soap.SOAPElement;
030:
031: //TODO : Logging
032:
033: public class UserIdentityDetector {
034:
035: private UserContext userContext = null;
036: private String userKey = null;
037: private SSOToken token = null;
038: private static String ROOT_AUTH_CONTEXT = "/";
039:
040: private static Logger logger = PortalLogger
041: .getLogger(UserIdentityDetector.class);
042:
043: public UserIdentityDetector(UserContext userContext) {
044: this .userContext = userContext;
045: detectUserIdentity();
046: }
047:
048: public String getUserKey() {
049: return userKey;
050: }
051:
052: public SSOToken getSSOToken() {
053: return token;
054: }
055:
056: private void detectUserIdentity() {
057: token = getSSOTokenFromExtension();
058:
059: if (token != null) {
060: processSunSSOTokenRequest();
061: return;
062: }
063:
064: token = getSSOTokenFromProfile();
065:
066: if (token != null) {
067: processOASISTokenRequest();
068: return;
069: }
070:
071: processDefaultRequest();
072: }
073:
074: private void processSunSSOTokenRequest() {
075: userKey = getUserKeyFromSSOToken();
076: }
077:
078: private void processOASISTokenRequest() {
079: userKey = getUserKeyFromSSOToken();
080: }
081:
082: /**
083: * Gets the user key of this request. Returns null if the request is
084: * from an anonymous user.
085: **/
086:
087: private void processDefaultRequest() {
088: if (userContext != null) {
089: userKey = userContext.getUserContextKey();
090: if (userKey != null
091: && userKey.equals(WSRPSpecKeys.WSRP_GUEST_KEY)) {
092: userKey = null;
093: }
094:
095: }
096: }
097:
098: private String getUserKeyFromSSOToken() {
099:
100: try {
101: if (token != null) {
102: return token.getPrincipal().getName();
103: }
104: } catch (SSOException se) {
105: if (logger.isLoggable(Level.SEVERE)) {
106: logger.log(Level.SEVERE, "PSWS_CSPWPMI0006", se);
107: }
108: }
109: return null;
110: }
111:
112: private SSOToken getSSOTokenFromExtension() {
113: String stringToken = null;
114: try {
115: if (userContext == null)
116: return null;
117: Extension ext[] = userContext.getExtensions();
118: if (ext == null || ext.length == 0) {
119: return null;
120: }
121: Extension ssoTokenExtn = ext[0];
122: SOAPElement element = ssoTokenExtn.get_any();
123: String nodeName = element.getNodeName();
124: stringToken = element
125: .getAttribute(IdentityPropagationConstants.USERCTX_EXTN_SSOTOKEN_VALUE);
126: if (stringToken == null || stringToken.trim().length() == 0) {
127: return null;
128: }
129: SSOTokenManager manager = SSOTokenManager.getInstance();
130: SSOToken userToken = manager.createSSOToken(stringToken);
131: return userToken;
132: } catch (SSOException se) {
133: //Detection failed , So just log it as INFO.
134: if (logger.isLoggable(Level.INFO)) {
135: logger.log(Level.INFO, "PSWS_CSPWPMI0006", se);
136: }
137: return null;
138: }
139: }
140:
141: private SSOToken getSSOTokenFromProfile() {
142: OASISUsernameTokenProfile profile = (OASISUsernameTokenProfile) ProducerThreadLocalizer
143: .getTokenProfile();
144: if (profile == null) {
145: return null;
146: }
147: String username = profile.getUsername();
148: String passwd = profile.getPassword();
149: return getUserSSOToken(username, passwd, ROOT_AUTH_CONTEXT);
150: }
151:
152: private SSOToken getUserSSOToken(String username, String password,
153: String orgDN) {
154: try {
155: return SSOUtil.createSSOToken(username, password, orgDN);
156: } catch (SSOException soe) {
157: if (logger.isLoggable(Level.INFO)) {
158: logger.log(Level.INFO, "PSWS_CSPWPMI0006", soe);
159: }
160: return null;
161: }
162: }
163: }
|