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