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.tc55.agent;
023:
024: import org.apache.catalina.Container;
025: import org.apache.catalina.Context;
026: import org.apache.catalina.Realm;
027: import org.josso.agent.AbstractSSOAgent;
028: import org.josso.agent.SSOAgentRequest;
029:
030: import java.security.Principal;
031:
032: /**
033: * Catalina SSO Agent Implementation that authenticates using the configured Catalina Realm's
034: * Gateway SSO Login module.
035: *
036: * @author <a href="mailto:gbrigand@josso.org">Gianluca Brigandi</a>
037: * @version CVS $Id: CatalinaSSOAgent.java 508 2008-02-18 13:32:29Z sgonzalez $
038: */
039: public class CatalinaSSOAgent extends AbstractSSOAgent {
040:
041: private Container _container;
042:
043: public CatalinaSSOAgent() {
044: super ();
045: }
046:
047: public CatalinaSSOAgent(Container container) {
048: super ();
049: _container = container;
050:
051: }
052:
053: public void start() {
054: super .start();
055: // Add context config as partner app ...
056: if (_container instanceof Context) {
057: Context context = (Context) _container;
058: _cfg.addSSOPartnerApp(context.getPath(), null);
059: }
060: }
061:
062: /**
063: * Sets the Catalina Context to be used by the authenticator.
064: *
065: * @param container
066: */
067: public void setCatalinaContainer(Container container) {
068: _container = container;
069:
070: }
071:
072: /**
073: * Authenticates the Single Sign-on Session by calling the
074: * configured Realm for the Catalina Context. The configured Realm
075: * should be the JAAS one so that the GatewayLoginModule can act
076: * and validate de given SSO Session Identifier in the Gateway.
077: *
078: * @param request
079: * @return the authenticated principal.
080: */
081: protected Principal authenticate(SSOAgentRequest request) {
082: CatalinaSSOAgentRequest r = (CatalinaSSOAgentRequest) request;
083: Context c = r.getContext();
084:
085: // Invoke authentication
086: Realm realm = c.getRealm();
087:
088: if (debug > 0)
089: log("Using realm : " + realm.getClass().getName()
090: + " SSOSID : " + r.getSessionId());
091:
092: Principal p = realm.authenticate(r.getSessionId(), r
093: .getSessionId());
094:
095: if (debug > 0)
096: log("Received principal : " + p + "["
097: + (p != null ? p.getClass().getName() : "<null>")
098: + "]");
099:
100: return p;
101: }
102:
103: protected void log(String message) {
104: if (_container != null) {
105: if (_container.getLogger().isDebugEnabled())
106: _container.getLogger().debug(
107: this .toString() + ": " + message);
108: } else
109: System.out.println(this .toString() + ": " + message);
110: }
111:
112: protected void log(String message, Throwable throwable) {
113: if (_container != null) {
114: if (_container.getLogger().isDebugEnabled())
115: _container.getLogger().debug(
116: this .toString() + ": " + message, throwable);
117: } else
118: System.out.println(this .toString() + ": " + message);
119: }
120:
121: /**
122: * Return a String rendering of this object.
123: */
124: public String toString() {
125:
126: StringBuffer sb = new StringBuffer("CatalinaSSOAgent[");
127: sb.append(_container != null ? _container.getName() : "");
128: sb.append("]");
129: return (sb.toString());
130:
131: }
132:
133: }
|