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: * author Kurt T Stam
022: */
023:
024: package org.josso.seam.console;
025:
026: import java.util.Map;
027:
028: import javax.faces.context.FacesContext;
029: import javax.servlet.http.Cookie;
030:
031: import org.jboss.seam.annotations.In;
032: import org.jboss.seam.annotations.Logger;
033: import org.jboss.seam.annotations.Name;
034: import org.jboss.seam.log.Log;
035: import org.jboss.seam.security.Identity;
036: import org.josso.Lookup;
037: import org.josso.agent.SSOAgent;
038: import org.josso.gateway.identity.SSORole;
039: import org.josso.gateway.session.SSOSession;
040: import org.josso.gateway.session.exceptions.NoSuchSessionException;
041: import org.josso.gateway.signon.Constants;
042:
043: /**
044: * This an authenticator for Seam that lets Seam grab signon
045: * credentials from JOSSO.
046: *
047: * The authenticator obtains the JOSSO session id, and attempts
048: * to obtain the active JOSSO session. If an active session is
049: * found it sets the username, and the roles given to this
050: * user into Seam context.
051: *
052: * @author <a href="mailto:kurt.stam@osconsulting.org">kurt.stam&064;osconsulting.org</a>
053: *
054: */
055: @Name("jossoAuthenticator")
056: public class JossoAuthenticator {
057:
058: private static final long serialVersionUID = 1L;
059:
060: @Logger
061: Log log;
062:
063: @In
064: FacesContext facesContext;
065:
066: @In
067: Identity identity;
068:
069: public void checkLogin() {
070: final boolean isLoggedIn = identity.isLoggedIn();
071: // user may already be logged in - check
072: if (isLoggedIn) {
073: return;
074: }
075: authenticate();
076: }
077:
078: public boolean authenticate() {
079: Map map = facesContext.getExternalContext()
080: .getRequestCookieMap();
081: String sessionId = null;
082: if (map.containsKey(Constants.JOSSO_SINGLE_SIGN_ON_COOKIE)) {
083: sessionId = ((Cookie) map
084: .get(Constants.JOSSO_SINGLE_SIGN_ON_COOKIE))
085: .getValue();
086: }
087: try {
088: if (sessionId != null && !"".equals(sessionId)) {
089: SSOAgent jossoAgent = Lookup.getInstance()
090: .lookupSSOAgent();
091: SSOSession session = jossoAgent.getSSOSessionManager()
092: .getSession(sessionId);
093: String username = session.getUsername();
094: identity.setUsername(username);
095: identity.setPassword(username);
096: log
097: .info("User "
098: + username
099: + " logged into Seam via JossoAuthenticator module.");
100: SSORole[] roles = jossoAgent.getSSOIdentityManager()
101: .findRolesByUsername(username);
102: for (int i = 0; i < roles.length; i++) {
103: String role = roles[i].getName();
104: log.info("User " + username + " adding role "
105: + role);
106: identity.addRole(role);
107: }
108: return true;
109: } else {
110: log.error("No JOSSO session found: " + sessionId
111: + ". User not authenticated.");
112: }
113: } catch (NoSuchSessionException e) {
114: log.error("NoSuchSessionException : " + sessionId
115: + ". User not authenticated.");
116: } catch (Exception e) {
117: log.error(e.getMessage() + ". User not authenticated.", e);
118: }
119: return false;
120: }
121: }
|