001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *     Academie de Rennes - proxy CAS support
017 *
018 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
019 */
020
021package org.nuxeo.ecm.platform.ui.web.auth.cas2;
022
023import java.io.IOException;
024import java.util.HashMap;
025import java.util.Map;
026
027import javax.servlet.http.Cookie;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.common.utils.URIUtils;
034import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants;
035import org.nuxeo.ecm.platform.ui.web.auth.interfaces.NuxeoAuthenticationPlugin;
036import org.nuxeo.ecm.platform.ui.web.auth.plugins.AnonymousAuthenticator;
037import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Anonymous authenticator that redirect logout to CAS server authentication to connect to nuxeo.
042 *
043 * @author Benjamin JALON
044 */
045public class AnonymousAuthenticatorForCAS2 extends AnonymousAuthenticator {
046
047    protected static final Log log = LogFactory.getLog(AnonymousAuthenticatorForCAS2.class);
048
049    protected Cas2Authenticator casAuthenticator;
050
051    @Override
052    public Boolean handleLogout(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
053
054        boolean isRedirectionToCas = false;
055
056        Cookie[] cookies = httpRequest.getCookies();
057        for (Cookie cookie : cookies) {
058            if (NXAuthConstants.SSO_INITIAL_URL_REQUEST_KEY.equals(cookie.getName())) {
059                isRedirectionToCas = true;
060                break;
061            }
062        }
063
064        if (isRedirectionToCas) {
065            String authURL = getCas2Authenticator().getServiceURL(httpRequest, Cas2Authenticator.LOGIN_ACTION);
066            String appURL = getCas2Authenticator().getAppURL(httpRequest);
067
068            try {
069                Map<String, String> urlParameters = new HashMap<String, String>();
070                urlParameters.put("service", appURL);
071                String location = URIUtils.addParametersToURIQuery(authURL, urlParameters);
072                httpResponse.sendRedirect(location);
073                return true;
074            } catch (IOException e) {
075                log.error("Unable to redirect to CAS logout screen:", e);
076                return false;
077            }
078        }
079
080        return super.handleLogout(httpRequest, httpResponse);
081    }
082
083    public Cas2Authenticator getCas2Authenticator() {
084        if (casAuthenticator != null) {
085            return casAuthenticator;
086        }
087
088        PluggableAuthenticationService service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(
089                PluggableAuthenticationService.NAME);
090        if (service == null) {
091            log.error("Can't get PluggableAuthenticationService");
092            return null;
093        }
094
095        NuxeoAuthenticationPlugin plugin = service.getPlugin("CAS2_AUTH");
096        if (plugin == null) {
097            log.error("Can't get Cas Authenticator from PluggableAuthenticationService");
098        }
099
100        casAuthenticator = (Cas2Authenticator) plugin;
101        return casAuthenticator;
102    }
103
104}