001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.shibboleth.service;
021
022import static org.apache.commons.lang.StringUtils.isNotEmpty;
023
024import java.io.UnsupportedEncodingException;
025import java.util.HashMap;
026import java.util.Map;
027
028import javax.servlet.http.HttpServletRequest;
029
030import com.google.common.collect.BiMap;
031import com.google.common.collect.HashBiMap;
032import org.nuxeo.common.utils.URIUtils;
033import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants;
034import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038public class ShibbolethAuthenticationServiceImpl extends DefaultComponent implements ShibbolethAuthenticationService {
039
040    public static final String CONFIG_EP = "config";
041
042    protected ShibbolethAuthenticationConfig config;
043
044    @Override
045    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
046        if (CONFIG_EP.equals(extensionPoint)) {
047            config = (ShibbolethAuthenticationConfig) contribution;
048        }
049    }
050
051    public ShibbolethAuthenticationConfig getConfig() {
052        return config;
053    }
054
055    @Override
056    public String getLoginURL(String redirectURL) {
057        if (config == null || config.getLoginURL() == null) {
058            return null;
059        }
060
061        Map<String, String> urlParameters = new HashMap<String, String>(1);
062        urlParameters.put(config.getLoginRedirectURLParameter(), redirectURL);
063        return URIUtils.addParametersToURIQuery(config.getLoginURL(), urlParameters);
064    }
065
066    @Override
067    public String getLogoutURL(String redirectURL) {
068        if (config == null || config.getLogoutURL() == null) {
069            return null;
070        }
071
072        Map<String, String> urlParameters = new HashMap<String, String>(1);
073        urlParameters.put(config.getLogoutRedirectURLParameter(), redirectURL);
074        return URIUtils.addParametersToURIQuery(config.getLogoutURL(), urlParameters);
075    }
076
077    protected static String getRedirectUrl(HttpServletRequest request) {
078        String redirectURL = VirtualHostHelper.getBaseURL(request);
079        if (request.getAttribute(NXAuthConstants.REQUESTED_URL) != null) {
080            redirectURL += request.getAttribute(NXAuthConstants.REQUESTED_URL);
081        } else if (request.getParameter(NXAuthConstants.REQUESTED_URL) != null) {
082            redirectURL += request.getParameter(NXAuthConstants.REQUESTED_URL);
083        } else {
084            redirectURL = request.getRequestURL().toString();
085            String queryString = request.getQueryString();
086            if (queryString != null) {
087                redirectURL += '?' + queryString;
088            }
089        }
090        return redirectURL;
091    }
092
093    @Override
094    public String getLoginURL(HttpServletRequest request) {
095        return getLoginURL(getRedirectUrl(request));
096    }
097
098    @Override
099    public String getLogoutURL(HttpServletRequest request) {
100        return getLogoutURL(getRedirectUrl(request));
101    }
102
103    @Override
104    public String getUserID(HttpServletRequest httpRequest) {
105        String idpUrl = httpRequest.getHeader(config.getIdpHeader());
106        String uidHeader = config.getUidHeaders().get(idpUrl);
107        if (uidHeader == null || readHeader(httpRequest, uidHeader) == null
108                || readHeader(httpRequest, uidHeader).isEmpty()) {
109            uidHeader = config.getDefaultUidHeader();
110        }
111        return readHeader(httpRequest, uidHeader);
112    }
113
114    @Override
115    public Map<String, Object> getUserMetadata(String userIdField, HttpServletRequest httpRequest) {
116        Map<String, Object> fieldMap = new HashMap<String, Object>(config.fieldMapping.size());
117        for (String key : config.getFieldMapping().keySet()) {
118            fieldMap.put(config.getFieldMapping().get(key), readHeader(httpRequest, key));
119        }
120        // Force userIdField to shibb userId value in case of the IdP do
121        // not use the same mapping as the default's one.
122        fieldMap.put(userIdField, getUserID(httpRequest));
123        return fieldMap;
124    }
125
126    @Override
127    public BiMap<String, String> getUserMetadata() {
128        BiMap<String, String> biMap = HashBiMap.create();
129        biMap.putAll(config.getFieldMapping());
130        return biMap;
131    }
132
133    protected String readHeader(HttpServletRequest request, String key) {
134        String value = request.getHeader(key);
135        if (isNotEmpty(value) && isNotEmpty(config.getHeaderEncoding())) {
136            try {
137                value = new String(value.getBytes("ISO-8859-1"), config.getHeaderEncoding());
138            } catch (UnsupportedEncodingException ignored) {
139                // Nothing
140            }
141        }
142        return value;
143    }
144}