001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (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
016 */
017
018package org.nuxeo.ecm.platform.oauth2.openid;
019
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
028 * @since 5.7
029 */
030public class OpenIDProviderFragmentRegistry extends ContributionFragmentRegistry<OpenIDConnectProviderDescriptor> {
031
032    protected final Map<String, OpenIDConnectProviderDescriptor> providers = new HashMap<String, OpenIDConnectProviderDescriptor>();
033
034    @Override
035    public OpenIDConnectProviderDescriptor clone(OpenIDConnectProviderDescriptor source) {
036
037        OpenIDConnectProviderDescriptor copy = new OpenIDConnectProviderDescriptor();
038
039        copy.scopes = source.scopes;
040        copy.authorizationServerURL = source.authorizationServerURL;
041        copy.clientId = source.clientId;
042        copy.clientSecret = source.clientSecret;
043        copy.icon = source.icon;
044        copy.enabled = source.enabled;
045        copy.name = source.name;
046        copy.tokenServerURL = source.tokenServerURL;
047        copy.userInfoURL = source.userInfoURL;
048        copy.label = source.label;
049        copy.description = source.description;
050        copy.redirectUriResolver = source.redirectUriResolver;
051        copy.userResolverClass = source.userResolverClass;
052        copy.accessTokenKey = source.accessTokenKey;
053        copy.userInfoClass = source.userInfoClass;
054        copy.userMapper = source.userMapper;
055        return copy;
056    }
057
058    @Override
059    public void contributionRemoved(String name, OpenIDConnectProviderDescriptor origContrib) {
060        providers.remove(name);
061    }
062
063    @Override
064    public void contributionUpdated(String name, OpenIDConnectProviderDescriptor contrib,
065            OpenIDConnectProviderDescriptor newOrigContrib) {
066        if (contrib.isEnabled()) {
067            providers.put(name, contrib);
068        } else {
069            providers.remove(name);
070        }
071    }
072
073    @Override
074    public String getContributionId(OpenIDConnectProviderDescriptor contrib) {
075        return contrib.getName();
076    }
077
078    @Override
079    public void merge(OpenIDConnectProviderDescriptor src, OpenIDConnectProviderDescriptor dst) {
080
081        if (dst.authorizationServerURL == null || dst.authorizationServerURL.isEmpty()) {
082            dst.authorizationServerURL = src.authorizationServerURL;
083        }
084        if (dst.clientId == null || dst.clientId.isEmpty()) {
085            dst.clientId = src.clientId;
086        }
087        if (dst.clientSecret == null || dst.clientSecret.isEmpty()) {
088            dst.clientSecret = src.clientSecret;
089        }
090        if (dst.icon == null || dst.icon.isEmpty()) {
091            dst.icon = src.icon;
092        }
093        if (dst.scopes == null || dst.scopes.length == 0) {
094            dst.scopes = src.scopes;
095        }
096        if (dst.tokenServerURL == null || dst.tokenServerURL.isEmpty()) {
097            dst.tokenServerURL = src.tokenServerURL;
098        }
099        if (dst.userInfoURL == null || dst.userInfoURL.isEmpty()) {
100            dst.userInfoURL = src.userInfoURL;
101        }
102        if (dst.label == null || dst.label.isEmpty()) {
103            dst.label = src.label;
104        }
105        if (dst.description == null || dst.description.isEmpty()) {
106            dst.description = src.description;
107        }
108        if (!src.accessTokenKey.equals(OpenIDConnectProviderDescriptor.DEFAULT_ACCESS_TOKEN_KEY)) {
109            dst.accessTokenKey = src.accessTokenKey;
110        }
111        if (src.userInfoClass != OpenIDConnectProviderDescriptor.DEFAULT_USER_INFO_CLASS) {
112            dst.userInfoClass = src.userInfoClass;
113        }
114        if (src.redirectUriResolver != OpenIDConnectProviderDescriptor.DEFAULT_REDIRECT_URI_RESOLVER_CLASS) {
115            dst.redirectUriResolver = src.redirectUriResolver;
116        }
117        if (src.getUserResolverClass() != OpenIDConnectProviderDescriptor.DEFAULT_USER_RESOLVER_CLASS) {
118            dst.userResolverClass = src.userResolverClass;
119        }
120
121        if (src.userMapper != null && src.userMapper.length() > 0) {
122            dst.userMapper = src.userMapper;
123        }
124
125        dst.enabled = src.enabled;
126    }
127
128    public Collection<OpenIDConnectProviderDescriptor> getContribs() {
129        return providers.values();
130    }
131}