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 * @deprecated since 11.1
032 */
033@Deprecated
034public class OpenIDProviderFragmentRegistry extends ContributionFragmentRegistry<OpenIDConnectProviderDescriptor> {
035
036    protected final Map<String, OpenIDConnectProviderDescriptor> providers = new HashMap<>();
037
038    @Override
039    public OpenIDConnectProviderDescriptor clone(OpenIDConnectProviderDescriptor source) {
040
041        OpenIDConnectProviderDescriptor copy = new OpenIDConnectProviderDescriptor();
042
043        copy.scopes = source.scopes;
044        copy.authorizationServerURL = source.authorizationServerURL;
045        copy.clientId = source.clientId;
046        copy.clientSecret = source.clientSecret;
047        copy.icon = source.icon;
048        copy.enabled = source.enabled;
049        copy.name = source.name;
050        copy.tokenServerURL = source.tokenServerURL;
051        copy.userInfoURL = source.userInfoURL;
052        copy.label = source.label;
053        copy.description = source.description;
054        copy.redirectUriResolver = source.redirectUriResolver;
055        copy.userResolverClass = source.userResolverClass;
056        copy.accessTokenKey = source.accessTokenKey;
057        copy.userInfoClass = source.userInfoClass;
058        copy.userMapper = source.userMapper;
059        copy.authenticationMethod = source.authenticationMethod;
060        return copy;
061    }
062
063    @Override
064    public void contributionRemoved(String name, OpenIDConnectProviderDescriptor origContrib) {
065        providers.remove(name);
066    }
067
068    @Override
069    public void contributionUpdated(String name, OpenIDConnectProviderDescriptor contrib,
070            OpenIDConnectProviderDescriptor newOrigContrib) {
071        if (contrib.isEnabled()) {
072            providers.put(name, contrib);
073        } else {
074            providers.remove(name);
075        }
076    }
077
078    @Override
079    public String getContributionId(OpenIDConnectProviderDescriptor contrib) {
080        return contrib.getName();
081    }
082
083    @Override
084    public void merge(OpenIDConnectProviderDescriptor src, OpenIDConnectProviderDescriptor dst) {
085
086        if (dst.authorizationServerURL == null || dst.authorizationServerURL.isEmpty()) {
087            dst.authorizationServerURL = src.authorizationServerURL;
088        }
089        if (dst.clientId == null || dst.clientId.isEmpty()) {
090            dst.clientId = src.clientId;
091        }
092        if (dst.clientSecret == null || dst.clientSecret.isEmpty()) {
093            dst.clientSecret = src.clientSecret;
094        }
095        if (dst.icon == null || dst.icon.isEmpty()) {
096            dst.icon = src.icon;
097        }
098        if (dst.scopes == null || dst.scopes.length == 0) {
099            dst.scopes = src.scopes;
100        }
101        if (dst.tokenServerURL == null || dst.tokenServerURL.isEmpty()) {
102            dst.tokenServerURL = src.tokenServerURL;
103        }
104        if (dst.userInfoURL == null || dst.userInfoURL.isEmpty()) {
105            dst.userInfoURL = src.userInfoURL;
106        }
107        if (dst.label == null || dst.label.isEmpty()) {
108            dst.label = src.label;
109        }
110        if (dst.description == null || dst.description.isEmpty()) {
111            dst.description = src.description;
112        }
113        if (!src.accessTokenKey.equals(OpenIDConnectProviderDescriptor.DEFAULT_ACCESS_TOKEN_KEY)) {
114            dst.accessTokenKey = src.accessTokenKey;
115        }
116        if (src.userInfoClass != OpenIDConnectProviderDescriptor.DEFAULT_USER_INFO_CLASS) {
117            dst.userInfoClass = src.userInfoClass;
118        }
119        if (src.redirectUriResolver != OpenIDConnectProviderDescriptor.DEFAULT_REDIRECT_URI_RESOLVER_CLASS) {
120            dst.redirectUriResolver = src.redirectUriResolver;
121        }
122        if (src.getUserResolverClass() != OpenIDConnectProviderDescriptor.DEFAULT_USER_RESOLVER_CLASS) {
123            dst.userResolverClass = src.userResolverClass;
124        }
125        if (src.userMapper != null && src.userMapper.length() > 0) {
126            dst.userMapper = src.userMapper;
127        }
128        if (!src.authenticationMethod.equals(OpenIDConnectProviderDescriptor.DEFAULT_AUTHENTICATION_METHOD)) {
129            dst.authenticationMethod = src.authenticationMethod;
130        }
131
132        dst.enabled = src.enabled;
133    }
134
135    public Collection<OpenIDConnectProviderDescriptor> getContribs() {
136        return providers.values();
137    }
138}