001: /**
002: * $Id: PSClientAwareAppContext.java,v 1.5 2005/10/13 16:35:49 dpolla Exp $
003: * Copyright 2002 Sun Microsystems, Inc. Allrights reserved. Use of
004: * this product is subjectto license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users Subject to Standard License
006: * Terms and Conditions.
007: *
008: * Sun, Sun Microsystems, the Sun logo, and Sun ONE are trademarks or
009: * registered trademarks of Sun Microsystems,Inc. in the United States
010: * and other countries.
011: */package com.sun.ssoadapter.config;
012:
013: import java.util.Map;
014: import java.util.HashMap;
015: import java.util.Hashtable;
016: import java.util.Iterator;
017: import java.util.Set;
018: import java.security.AccessController;
019:
020: import java.io.IOException;
021: import java.util.MissingResourceException;
022:
023: import com.iplanet.sso.SSOTokenManager;
024: import com.iplanet.sso.SSOToken;
025: import com.iplanet.sso.SSOException;
026: import com.iplanet.sso.SSOTokenListener;
027: import com.iplanet.sso.SSOTokenEvent;
028:
029: import com.iplanet.services.cdm.Client;
030: import com.iplanet.services.cdm.ClientException;
031: import com.iplanet.am.util.AMClientDetector;
032: import com.iplanet.am.util.Debug;
033: import com.iplanet.am.util.AdminUtils;
034:
035: import com.sun.identity.security.AdminTokenAction;
036:
037: import com.sun.ssoadapter.SSOAdapterSession;
038:
039: /**
040: * There should only be one instance of this object per-jvm
041: */
042:
043: public class PSClientAwareAppContext implements ClientAwareAppContext,
044: DSAMEConstants, SSOTokenListener {
045: private static final String SSO_TOKEN = "ps.session";
046:
047: private String this ClassName = null;
048:
049: //
050: // this member is re-used throughout the life of this class to get
051: // instances of SSOToken
052: //
053: private SSOTokenManager tokenMgr = null;
054:
055: private SessionListener listener = null;
056:
057: private AMClientDetector cd = null;
058:
059: private DSAMEConnection dsameConn = null;
060: private DSAMEUtils dsameUtils = null;
061:
062: /**
063: *
064: * @param sl
065: * @throws com.sun.ssoadapter.config.SAALException
066: */
067: public void init(SessionListener sl) throws SAALException {
068: //
069: // order matters here!
070: //
071: this ClassName = getClass().getName(); // for debug
072:
073: //
074: // Keep a copy of the SessionListener to invoke the sessionDestroyed()
075: //
076: listener = sl;
077:
078: //
079: // Get the AdminConnection to DSAME
080: //
081: try {
082: SSOToken ssot = (SSOToken) AccessController
083: .doPrivileged(AdminTokenAction.getInstance());
084: dsameConn = new DSAMEConnection(ssot);
085:
086: //
087: // initialize session stuff
088: //
089: tokenMgr = DSAMEConnection.getSSOTokenManager();
090:
091: //
092: // Create the instance of DSAMEUtils (one time only)
093: //
094: dsameUtils = DSAMEUtils.getInstance(dsameConn);
095: } catch (SSOException se) {
096: throw new SAALException(this ClassName + ": " + se);
097: }
098:
099: //
100: // initialize Client stuff
101: //
102: cd = new AMClientDetector();
103: }
104:
105: /**
106: * Add the client type info if client == null
107: * @param service
108: * @param clnt
109: */
110: private void addClientTypeInfo(Map service, String clnt) {
111: String client = (clnt != null) ? clnt : DEFAULT_CLIENT_TYPE;
112: if (service != null) {
113: service.put(CLIENT_TYPE, client);
114: }
115:
116: return;
117: }
118:
119: /**
120: *
121: * @param service
122: * @param attributeName
123: * @throws java.io.IOException
124: * @throws java.util.MissingResourceException
125: * @return
126: */
127: public String getStringAttribute(Map service, String attributeName)
128: throws IOException, MissingResourceException {
129: return getStringAttribute(service, DEFAULT_CLIENT_TYPE,
130: attributeName);
131: }
132:
133: /**
134: *
135: * @param service
136: * @param clientType
137: * @param attributeName
138: * @throws java.io.IOException
139: * @throws java.util.MissingResourceException
140: * @return
141: */
142: public String getStringAttribute(Map service, String clientType,
143: String attributeName) throws IOException,
144: MissingResourceException {
145: String serviceName = dsameUtils.getServiceName(service);
146: String val = null;
147:
148: Set vals = getAttribute(serviceName, clientType, attributeName);
149:
150: if (vals != null && vals.size() > 0) {
151: Iterator iter = vals.iterator();
152: val = (String) iter.next();
153: }
154:
155: return (val);
156: }
157:
158: /**
159: *
160: * @param service
161: * @param attributeName
162: * @throws java.io.IOException
163: * @throws java.util.MissingResourceException
164: * @return
165: */
166: public Set getAttribute(Map service, String attributeName)
167: throws IOException, MissingResourceException {
168: return getAttribute(service, DEFAULT_CLIENT_TYPE, attributeName);
169: }
170:
171: /**
172: *
173: * @param service
174: * @param clientType
175: * @param attributeName
176: * @throws java.io.IOException
177: * @throws java.util.MissingResourceException
178: * @return
179: */
180: public Set getAttribute(Map service, String clientType,
181: String attributeName) throws IOException,
182: MissingResourceException {
183: String serviceName = dsameUtils.getServiceName(service);
184: return getAttribute(serviceName, clientType, attributeName);
185: }
186:
187: /**
188: * All getAttributes() get mapped into this
189: * @param serviceName
190: * @param client
191: * @param attrName
192: * @throws java.util.MissingResourceException
193: * @throws java.io.IOException
194: * @return
195: */
196: private Set getAttribute(String serviceName, String client,
197: String attrName) throws MissingResourceException,
198: IOException {
199: if (serviceName == null) {
200: throw new MissingResourceException("Missing in Map",
201: "DSAMEServiceAppContext", SERVICENAME);
202: }
203:
204: Set vals = dsameConn.getGlobalAttribute(serviceName, attrName);
205: Set retVals = null;
206:
207: int type = dsameUtils.getAttributeType(serviceName, attrName);
208: switch (type) {
209: case SINGLE:
210: //
211: // return the List. The getStringAttribute() will pick up
212: // only the first value !
213: //
214: retVals = vals;
215: break;
216:
217: case LIST:
218: //
219: //
220: //
221: retVals = dsameUtils.getClientValues(vals, client, null,
222: true);
223:
224: break;
225:
226: default:
227: retVals = vals;
228: break;
229: }
230:
231: return (retVals);
232: }
233:
234: //
235: // session methods
236: //
237:
238: /**
239: *
240: * @return
241: */
242: private SSOTokenManager getSSOTokenManager() {
243: return (tokenMgr);
244: }
245:
246: /**
247: *
248: * @param session
249: * @return
250: */
251: protected SSOToken getSSOToken(SSOAdapterSession session) {
252: return session.getSSOToken();
253: }
254:
255: /**
256: *
257: * @param session
258: * @return
259: */
260: public boolean validateSession(SSOAdapterSession session) {
261: return session.isSessionValid();
262: }
263:
264: /**
265: *
266: * @param session
267: * @return
268: */
269: public String getSessionID(SSOAdapterSession session) {
270: return session.getSessionID();
271: }
272:
273: /**
274: * Add this Object to the DSAME's session listener.
275: * @param session
276: * @throws java.lang.IllegalStateException
277: */
278: public void addSessionListener(SSOAdapterSession session)
279: throws IllegalStateException {
280: try {
281: SSOToken token = session.getSSOToken();
282: token.addSSOTokenListener(this ); // register us
283: } catch (SSOException se) {
284: throw new IllegalStateException(
285: "DSAMESessionUserContext.addSessionListener()" + se);
286: }
287: }
288:
289: /**
290: *
291: * @param evt
292: */
293: public void ssoTokenChanged(SSOTokenEvent evt) {
294: String sid = evt.getToken().getTokenID().toString();
295: listener.sessionDestroyed(sid);
296: }
297:
298: /*
299: * Check to see if uid is an authorized authless uid
300: */
301: /**
302: *
303: * @param uid
304: * @return
305: */
306: public boolean isAuthorizedAuthlessUID(String uid) {
307: boolean authorized = false;
308:
309: // check for null uid
310: if (uid == null) {
311: return authorized;
312: }
313:
314: Set authorizedUIDs = null;
315: //debug.message("PSClientAwareAppContext: verifying " + uid + " is an " +
316: // "authorized authentication-less user.");
317:
318: // iterate thru authorized UID list and perform a lowercase comparison
319: //
320: try {
321: authorizedUIDs = dsameConn
322: .getGlobalAttribute(SUN_SSOADAPTER_SERVICE,
323: ATTR_AUTHORIZEDAUTHLESSUIDS);
324: } catch (IOException ioe) {
325: //debug.message("PSClientAwareAppContext: failed to verify authless uid. ", ioe);
326: return authorized;
327: }
328:
329: // iterate thru authorized UID list and perform a lowercase comparison
330: //
331: if (authorizedUIDs != null) {
332: uid = uid.toLowerCase();
333: //debug.message("PSClientAwareAppContext: authorized uids: " + authorizedUIDs);
334:
335: for (Iterator iter = authorizedUIDs.iterator(); iter
336: .hasNext();) {
337: String tuid = ((String) iter.next()).toLowerCase();
338: if (tuid.equals(uid)) {
339: authorized = true;
340: break;
341: }
342: }
343: }
344:
345: authorizedUIDs = null;
346: //debug.message("PSClientAwareAppContext: authless uid verified: " + authorized);
347:
348: return authorized;
349: }
350:
351: //
352: // client
353: //
354:
355: /**
356: *
357: * @param session
358: * @return
359: */
360: public String getClientType(SSOAdapterSession session) {
361: return session.getClientType();
362: }
363: }
|