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.ui.web.auth.service;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.regex.Pattern;
027
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XNodeMap;
031import org.nuxeo.common.xmap.annotation.XObject;
032
033@XObject("specificAuthenticationChain")
034public class SpecificAuthChainDescriptor {
035
036    @XNode("@name")
037    protected String name;
038
039    @XNodeList(value = "replacementChain/plugin", type = ArrayList.class, componentType = String.class)
040    private List<String> replacementChain;
041
042    public List<String> getReplacementChain() {
043        return replacementChain;
044    }
045
046    @XNodeList(value = "allowedPlugins/plugin", type = ArrayList.class, componentType = String.class)
047    private List<String> allowedPlugins;
048
049    public List<String> getAllowedPlugins() {
050        return allowedPlugins;
051    }
052
053    @XNodeList(value = "urlPatterns/url", type = ArrayList.class, componentType = String.class)
054    private List<String> urls;
055
056    private List<Pattern> urlPatterns;
057
058    @XNodeMap(value = "headers/header", key = "@name", type = HashMap.class, componentType = String.class)
059    private Map<String, String> headers;
060
061    private Map<String, Pattern> headerPatterns;
062
063    public List<Pattern> getUrlPatterns() {
064        if (urlPatterns == null) {
065            List<Pattern> patterns = new ArrayList<Pattern>();
066            for (String url : urls) {
067                patterns.add(Pattern.compile(url));
068            }
069            urlPatterns = patterns;
070        }
071        return urlPatterns;
072    }
073
074    public Map<String, Pattern> getHeaderPatterns() {
075        if (headerPatterns == null) {
076            headerPatterns = new HashMap<String, Pattern>();
077            for (String headerName : headers.keySet()) {
078                headerPatterns.put(headerName, Pattern.compile(headers.get(headerName)));
079            }
080        }
081        return headerPatterns;
082    }
083
084    public List<String> computeResultingChain(List<String> defaultChain) {
085        if (replacementChain != null && !replacementChain.isEmpty()) {
086            return replacementChain;
087        }
088
089        List<String> filteredChain = new ArrayList<String>();
090        for (String pluginName : defaultChain) {
091            if (allowedPlugins.contains(pluginName)) {
092                filteredChain.add(pluginName);
093            }
094        }
095        return filteredChain;
096    }
097
098}