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;
023import static org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants.REDIRECT_URL;
024
025import java.io.UnsupportedEncodingException;
026import java.util.HashMap;
027import java.util.Map;
028
029import javax.servlet.http.HttpServletRequest;
030
031import com.google.common.collect.BiMap;
032import com.google.common.collect.HashBiMap;
033import org.nuxeo.common.utils.URIUtils;
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    @Override
078    public String getLoginURL(HttpServletRequest request) {
079        String redirectUrl = VirtualHostHelper.getRedirectUrl(request);
080        request.getSession().setAttribute(REDIRECT_URL, redirectUrl);
081        return getLoginURL(redirectUrl);
082    }
083
084    @Override
085    public String getLogoutURL(HttpServletRequest request) {
086        return getLogoutURL((String) request.getSession().getAttribute(REDIRECT_URL));
087    }
088
089    @Override
090    public String getUserID(HttpServletRequest httpRequest) {
091        String idpUrl = httpRequest.getHeader(config.getIdpHeader());
092        String uidHeader = config.getUidHeaders().get(idpUrl);
093        if (uidHeader == null || readHeader(httpRequest, uidHeader) == null
094                || readHeader(httpRequest, uidHeader).isEmpty()) {
095            uidHeader = config.getDefaultUidHeader();
096        }
097        return readHeader(httpRequest, uidHeader);
098    }
099
100    @Override
101    public Map<String, Object> getUserMetadata(String userIdField, HttpServletRequest httpRequest) {
102        Map<String, Object> fieldMap = new HashMap<String, Object>(config.fieldMapping.size());
103        for (String key : config.getFieldMapping().keySet()) {
104            fieldMap.put(config.getFieldMapping().get(key), readHeader(httpRequest, key));
105        }
106        // Force userIdField to shibb userId value in case of the IdP do
107        // not use the same mapping as the default's one.
108        fieldMap.put(userIdField, getUserID(httpRequest));
109        return fieldMap;
110    }
111
112    @Override
113    public BiMap<String, String> getUserMetadata() {
114        BiMap<String, String> biMap = HashBiMap.create();
115        biMap.putAll(config.getFieldMapping());
116        return biMap;
117    }
118
119    protected String readHeader(HttpServletRequest request, String key) {
120        String value = request.getHeader(key);
121        if (isNotEmpty(value) && isNotEmpty(config.getHeaderEncoding())) {
122            try {
123                value = new String(value.getBytes("ISO-8859-1"), config.getHeaderEncoding());
124            } catch (UnsupportedEncodingException ignored) {
125                // Nothing
126            }
127        }
128        return value;
129    }
130}