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