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    public static final boolean DEFAULT_HANDLE_PROMPT_VALUE = true;
037
038    @XNode("@name")
039    protected String name;
040
041    @XNode("@handlePrompt")
042    private boolean handlePrompt = DEFAULT_HANDLE_PROMPT_VALUE;
043
044    @XNodeList(value = "replacementChain/plugin", type = ArrayList.class, componentType = String.class)
045    private List<String> replacementChain;
046
047    public List<String> getReplacementChain() {
048        return replacementChain;
049    }
050
051    @XNodeList(value = "allowedPlugins/plugin", type = ArrayList.class, componentType = String.class)
052    private List<String> allowedPlugins;
053
054    public List<String> getAllowedPlugins() {
055        return allowedPlugins;
056    }
057
058    @XNodeList(value = "urlPatterns/url", type = ArrayList.class, componentType = String.class)
059    private List<String> urls;
060
061    private List<Pattern> urlPatterns;
062
063    @XNodeMap(value = "headers/header", key = "@name", type = HashMap.class, componentType = String.class)
064    private Map<String, String> headers;
065
066    private Map<String, Pattern> headerPatterns;
067
068    public List<Pattern> getUrlPatterns() {
069        if (urlPatterns == null) {
070            List<Pattern> patterns = new ArrayList<Pattern>();
071            for (String url : urls) {
072                patterns.add(Pattern.compile(url));
073            }
074            urlPatterns = patterns;
075        }
076        return urlPatterns;
077    }
078
079    public Map<String, Pattern> getHeaderPatterns() {
080        if (headerPatterns == null) {
081            headerPatterns = new HashMap<String, Pattern>();
082            for (String headerName : headers.keySet()) {
083                headerPatterns.put(headerName, Pattern.compile(headers.get(headerName)));
084            }
085        }
086        return headerPatterns;
087    }
088
089    public List<String> computeResultingChain(List<String> defaultChain) {
090        if (replacementChain != null && !replacementChain.isEmpty()) {
091            return replacementChain;
092        }
093
094        List<String> filteredChain = new ArrayList<String>();
095        for (String pluginName : defaultChain) {
096            if (allowedPlugins.contains(pluginName)) {
097                filteredChain.add(pluginName);
098            }
099        }
100        return filteredChain;
101    }
102
103    /**
104     * Return if the auth filter has to handle prompt or return 401
105     *
106     * @return
107     * @since 8.2
108     */
109    public boolean doHandlePrompt() {
110        return handlePrompt;
111    }
112
113}