001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021:
022: package org.josso.activex;
023:
024: import org.apache.commons.beanutils.BeanUtils;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.josso.gateway.GatewayServiceLocator;
028: import org.josso.gateway.assertion.exceptions.AssertionNotValidException;
029: import org.josso.gateway.identity.SSORole;
030: import org.josso.gateway.identity.SSOUser;
031: import org.josso.gateway.identity.exceptions.NoSuchUserException;
032: import org.josso.gateway.identity.exceptions.IdentityProvisioningException;
033: import org.josso.gateway.identity.exceptions.SSOIdentityException;
034: import org.josso.gateway.identity.service.SSOIdentityManager;
035: import org.josso.gateway.identity.service.SSOIdentityProvider;
036: import org.josso.gateway.session.exceptions.NoSuchSessionException;
037: import org.josso.gateway.session.service.SSOSessionManager;
038:
039: import java.io.InputStream;
040: import java.io.FileInputStream;
041: import java.io.IOException;
042: import java.lang.reflect.InvocationTargetException;
043: import java.util.Enumeration;
044: import java.util.Properties;
045:
046: /**
047: * This component is based on JavaBeans components architecture.
048: * It is packaged by the J2SDK ActiveX bridge as an ActiveX control,
049: * thereby allowing it to be used as a functional component in an ActiveX container.
050: *
051: * To use this ActiveX control, you have to follow this steps :
052: *
053: * <ul>
054: * <li>1. Instantiate the control.</li>
055: * <li>2. Configure control properties using setProperty method. The default implementation uses SOAP,
056: * so you must configure the SOAP end point i.e. setProperty("endpoint", "myhost.com:8080");</li>
057: * <li>3. Initialize the control : invoke the init() method befor using the control.</li>
058: * <li>4. Invoke operations, i.e. accessSession("2F122BEE8684C0BEE186C0BE91083171");</li>
059: * </ul>
060: *
061: * You could specify a differente GatewayServiceLocator class and configure specific properties for it.
062: * If no GatewayServiceLocator FQCN is specified, the WebserviceGatewayServiceLocator is used as default.
063: *
064: * The control configuration can be specified through the "setProperty" method, all properties starting with
065: * the "gwy." prefix will be used to configure the GatewayServiceLocator this control uses.
066: *
067: * If you use the WebserviceGatweayServiceLocator, you can use the following properties :
068: * <ul>
069: * <li>gwy.endpoint : the SOAP endpoint</li>
070: * <li>gwy.transportSecurity : "none" or "confidential", default to "none"./li>
071: * <li>gwy.username : the username credential used for the "confidential" transport security.</li>
072: * <li>gwy.password : the passwrord credential used for "confidential" transport security.</li>
073: * </ul>
074: *
075: * Check the Java Console for log messages.
076: *
077: * @see org.josso.gateway.GatewayServiceLocator
078: * @see org.josso.gateway.WebserviceGatewayServiceLocator
079: *
080: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
081: * @version $Id: JOSSOActiveX.java 508 2008-02-18 13:32:29Z sgonzalez $
082: */
083:
084: public class JOSSOActiveX {
085:
086: private static final Log logger = LogFactory
087: .getLog(JOSSOActiveX.class);
088:
089: private String _version;
090: private String log4jProperties;
091: private SSOIdentityProvider _ip;
092: private SSOIdentityManager _im;
093: private SSOSessionManager _sm;
094: private Properties _props;
095:
096: private String _gwyServiceLocatorClass = "org.josso.gateway.WebserviceGatewayServiceLocator";
097:
098: public JOSSOActiveX() {
099:
100: logger.debug("JOSSOActiveX:Creating new instance ... ");
101:
102: // Configure standard component keeper, not using JMX
103: System.setProperty("org.josso.ComponentKeeperFactory",
104: "org.josso.ComponentKeeperFactoryImpl");
105:
106: _props = new Properties();
107:
108: Properties p = new Properties();
109: InputStream is = getClass().getResourceAsStream(
110: "/org/josso/josso.properties");
111: try {
112: p.load(is);
113: _version = p.get("Name") + "-" + p.get("version");
114: } catch (Exception e) {
115: _version = "n/a";
116: }
117: }
118:
119: public void init() {
120:
121: try {
122:
123: if (log4jProperties != null) {
124: resetLog4j();
125: }
126:
127: GatewayServiceLocator sl = doMakeGatewayServiceLocator();
128:
129: logger
130: .debug("JOSSOActiveX:Getting new SSOIdentityProvider instance");
131: _ip = sl.getSSOIdentityProvider();
132: assert _ip != null : "No Identity provider found !";
133:
134: logger
135: .debug("JOSSOActiveX:Getting new SSOIdentityManager instance");
136: _im = sl.getSSOIdentityManager();
137: assert _im != null : "No Identity manager found";
138:
139: logger
140: .debug("JOSSOActiveX:Getting new SSOSessionManager instance");
141: _sm = sl.getSSOSessionManager();
142: assert _sm != null : "No Session manager found";
143:
144: logger.debug("JOSSOActiveX:" + getVersion()
145: + " initialized OK");
146:
147: } catch (Exception e) {
148: logger.error("JOSSOActiveX:" + e.getMessage(), e);
149: logger.debug("JOSSOActiveX:" + getVersion()
150: + " initialized with ERRORS");
151:
152: throw new RuntimeException(
153: "JOSSOActiveX:Error during initialization : "
154: + e.getMessage() != null ? e.getMessage()
155: : e.toString(), e);
156: }
157:
158: }
159:
160: /**
161: * This operation allows external log4j configuration while using JRE/ActiveX bridge ...
162: * @throws IOException
163: */
164: private void resetLog4j() throws IOException {
165: FileInputStream fis = null;
166:
167: try {
168: org.apache.log4j.LogManager.resetConfiguration();
169:
170: fis = new FileInputStream(log4jProperties);
171: Properties log4jProperties = new Properties();
172: log4jProperties.load(fis);
173:
174: new org.apache.log4j.PropertyConfigurator()
175: .configure(log4jProperties);
176:
177: } finally {
178: if (fis != null)
179: fis.close();
180: }
181:
182: }
183:
184: /**
185: * The version associated with this control.
186: */
187: public String getVersion() {
188: return _version;
189: }
190:
191: public String getLog4jProperties() {
192: return log4jProperties;
193: }
194:
195: public void setLog4jProperties(String log4jProperties) {
196: this .log4jProperties = log4jProperties;
197: }
198:
199: /**
200: * Obtains the SSO Session token associated to the authentication assertion token.
201: * @param assertionId
202: * @return
203: */
204: public String resolveAuthenticationAssertion(String assertionId) {
205: try {
206: return getIdentityProvider()
207: .resolveAuthenticationAssertion(assertionId);
208: } catch (AssertionNotValidException e) {
209: return null;
210: } catch (IdentityProvisioningException e) {
211: logger.error(e.getMessage(), e);
212: throw new RuntimeException(e.getMessage() != null ? e
213: .getMessage() : e.toString(), e);
214: }
215: }
216:
217: /**
218: * Finds the user associated to a sso session
219: *
220: * @param sessionId the sso session identifier
221: */
222: public SSOUser findUserInSession(String sessionId) {
223: try {
224: return getIdentityManager().findUserInSession(sessionId);
225: } catch (SSOIdentityException e) {
226: return null; // Session has expired ...
227: } catch (Exception e) {
228: logger.error(e.getMessage(), e);
229: throw new RuntimeException(e.getMessage() != null ? e
230: .getMessage() : e.toString(), e);
231: }
232: }
233:
234: /**
235: * Returns all properties associated to a given user.
236: */
237: public SSOProperties getUserProperties(String username) {
238: try {
239: SSOUser user = getIdentityManager().findUser(username);
240: return new SSOProperties(user.getProperties());
241: } catch (Exception e) {
242: logger.error(e.getMessage(), e);
243: throw new RuntimeException(e.getMessage() != null ? e
244: .getMessage() : e.toString(), e);
245: }
246:
247: }
248:
249: /**
250: * Returns all roles associated to a given user.
251: */
252: public SSORoles getUserRoles(String username) {
253: try {
254: return new SSORoles(getIdentityManager()
255: .findRolesByUsername(username));
256: } catch (Exception e) {
257: logger.error(e.getMessage(), e);
258: throw new RuntimeException(e.getMessage() != null ? e
259: .getMessage() : e.toString(), e);
260: }
261:
262: }
263:
264: /**
265: * Returns true if the user belongs to the given rolename.
266: */
267: public boolean isUserInRole(String sessionId, String rolename) {
268: try {
269:
270: SSOUser user = this .findUserInSession(sessionId);
271:
272: if (user == null)
273: return false;
274:
275: SSORole[] roles = getIdentityManager().findRolesByUsername(
276: user.getName());
277:
278: for (int i = 0; i < roles.length; i++) {
279: SSORole role = roles[i];
280: if (role.getName().equals(rolename)) {
281: return true;
282: }
283: }
284: return false;
285: } catch (Exception e) {
286: logger.error(e.getMessage(), e);
287: throw new RuntimeException(e.getMessage() != null ? e
288: .getMessage() : e.toString(), e);
289: }
290:
291: }
292:
293: // -----------------------------------------------------------------------------
294:
295: /**
296: * This method accesss the session associated to the received id.
297: * This resets the session last access time and updates the access count.
298: *
299: * @param sessionId the session id previously returned by initiateSession.
300: *
301: * @return true if the session is valid, flase otherwise.
302: */
303: public boolean accessSession(String sessionId) {
304: try {
305: getSessionManager().accessSession(sessionId);
306: return true;
307: } catch (NoSuchSessionException e) {
308: return false;
309: } catch (Exception e) {
310: logger.error(e.getMessage(), e);
311: throw new RuntimeException(e.getMessage() != null ? e
312: .getMessage() : e.toString(), e);
313: }
314:
315: }
316:
317: // -----------------------------------------------------------------------------
318:
319: /**
320: * This method is used to configure the control.
321: * Available properties
322: *
323: * @param name the property name (i.e. .endpoint)
324: * @param value
325: */
326: public void setProperty(String name, String value) {
327: _props.setProperty(name, value);
328: }
329:
330: /**
331: * Returns the value of the specified property.
332: */
333: public String getProperty(String name) {
334: return _props.getProperty(name);
335: }
336:
337: /**
338: * Getter for the configuration property to define the concrete GatewayServiceLocator class.
339: *
340: * @return the FQCN used to create the GatewayServiceLocatorInstance
341: */
342: public String getGwyServiceLocatorClass() {
343: return _gwyServiceLocatorClass;
344: }
345:
346: /**
347: * Configuration property to define the concrete GatewayServiceLocator class.
348: *
349: * @param gwyServiceLocatorClass the FQCN used to create the GatewayServiceLocatorInstance
350: */
351: public void setGwyServiceLocatorClass(String gwyServiceLocatorClass) {
352: _gwyServiceLocatorClass = gwyServiceLocatorClass;
353: }
354:
355: /**
356: * Getter for the Identity Manager this control is using.
357: */
358: protected SSOIdentityProvider getIdentityProvider() {
359: return _ip;
360: }
361:
362: /**
363: * Getter for the Identity Manager this control is using.
364: */
365: protected SSOIdentityManager getIdentityManager() {
366: return _im;
367: }
368:
369: /**
370: * Getter for the Session Manager this control is using.
371: */
372: protected SSOSessionManager getSessionManager() {
373: return _sm;
374: }
375:
376: /**
377: * This method creates a new GatewayServiceLocatorInstance using the
378: * configured GatewayServiceLocator class.
379: *
380: * It also sets all configured properties with the prefix "gwy." to the new service locator instance.
381: * For example : the "gwy.endpoint" property will be used to set the endpoint property (setEndpoint(""))
382: * in the new gateway service locator instance.
383: *
384: *
385: */
386: protected GatewayServiceLocator doMakeGatewayServiceLocator() {
387:
388: GatewayServiceLocator serviceLocator = null;
389:
390: try {
391: serviceLocator = (GatewayServiceLocator) Class.forName(
392: _gwyServiceLocatorClass).newInstance();
393: } catch (Exception e) {
394: logger.error(e.getMessage(), e);
395: throw new RuntimeException(
396: "JOSSOActiveX:Can't instantiate gwy service locator : \n"
397: + e.getMessage() != null ? e.getMessage()
398: : e.toString(), e);
399: }
400:
401: Enumeration en = _props.keys();
402: while (en.hasMoreElements()) {
403: String key = (String) en.nextElement();
404: Object value = _props.get(key);
405:
406: if (key.startsWith("gwy.")) {
407:
408: String name = key.substring(4);
409:
410: try {
411:
412: if (value != null)
413: BeanUtils.setProperty(serviceLocator, name,
414: value);
415:
416: logger
417: .debug("JOSSOActiveX:setting property to GatewayServiceLocator : "
418: + name + "=" + value);
419:
420: } catch (IllegalAccessException e) {
421: logger
422: .error("JOSSOActiveX:Can't set property to GatewayServiceLocator : "
423: + name
424: + "="
425: + value
426: + "\n"
427: + e.getMessage());
428:
429: } catch (InvocationTargetException e) {
430: logger
431: .error("JOSSOActiveX:Can't set property to GatewayServiceLocator : "
432: + name
433: + "="
434: + value
435: + "\n"
436: + e.getMessage());
437: }
438: }
439: }
440:
441: return serviceLocator;
442: }
443:
444: public static void main(String[] args) {
445: logger.debug("Hello, World!");
446:
447: JOSSOActiveX x = new JOSSOActiveX();
448: x.init();
449: }
450:
451: }
|