001/* 002 * (C) Copyright 2015 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 * Nelson Silva 018 */ 019package org.nuxeo.ecm.platform.oauth2.providers; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.runtime.model.ContributionFragmentRegistry; 024 025import java.util.Collection; 026import java.util.HashMap; 027import java.util.Map; 028 029/** 030 * @since 7.3 031 */ 032public class OAuth2ServiceProviderContributionRegistry extends ContributionFragmentRegistry<OAuth2ServiceProviderDescriptor> { 033 034 protected static final Log log = LogFactory.getLog(OAuth2ServiceProviderContributionRegistry.class); 035 036 protected final Map<String, OAuth2ServiceProviderDescriptor> providers = new HashMap<>(); 037 038 @Override 039 public OAuth2ServiceProviderDescriptor clone(OAuth2ServiceProviderDescriptor source) { 040 041 OAuth2ServiceProviderDescriptor copy = new OAuth2ServiceProviderDescriptor(); 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.accessTokenKey = source.accessTokenKey; 055 copy.providerClass = source.providerClass; 056 return copy; 057 } 058 059 @Override 060 public void contributionRemoved(String name, OAuth2ServiceProviderDescriptor origContrib) { 061 providers.remove(name); 062 } 063 064 @Override 065 public void contributionUpdated(String name, OAuth2ServiceProviderDescriptor contrib, 066 OAuth2ServiceProviderDescriptor newOrigContrib) { 067 if (contrib.isEnabled()) { 068 providers.put(name, contrib); 069 } else { 070 providers.remove(name); 071 } 072 } 073 074 @Override 075 public String getContributionId(OAuth2ServiceProviderDescriptor contrib) { 076 return contrib.getName(); 077 } 078 079 @Override 080 public void merge(OAuth2ServiceProviderDescriptor src, OAuth2ServiceProviderDescriptor dst) { 081 082 if (dst.authorizationServerURL == null || dst.authorizationServerURL.isEmpty()) { 083 dst.authorizationServerURL = src.authorizationServerURL; 084 } 085 if (dst.clientId == null || dst.clientId.isEmpty()) { 086 dst.clientId = src.clientId; 087 } 088 if (dst.clientSecret == null || dst.clientSecret.isEmpty()) { 089 dst.clientSecret = src.clientSecret; 090 } 091 if (dst.icon == null || dst.icon.isEmpty()) { 092 dst.icon = src.icon; 093 } 094 if (dst.scopes == null || dst.scopes.length == 0) { 095 dst.scopes = src.scopes; 096 } 097 if (dst.tokenServerURL == null || dst.tokenServerURL.isEmpty()) { 098 dst.tokenServerURL = src.tokenServerURL; 099 } 100 if (dst.userInfoURL == null || dst.userInfoURL.isEmpty()) { 101 dst.userInfoURL = src.userInfoURL; 102 } 103 if (dst.label == null || dst.label.isEmpty()) { 104 dst.label = src.label; 105 } 106 if (dst.description == null || dst.description.isEmpty()) { 107 dst.description = src.description; 108 } 109 if (!src.accessTokenKey.equals(OAuth2ServiceProviderDescriptor.DEFAULT_ACCESS_TOKEN_KEY)) { 110 dst.accessTokenKey = src.accessTokenKey; 111 } 112 if (src.providerClass != OAuth2ServiceProviderDescriptor.DEFAULT_PROVIDER_CLASS) { 113 dst.providerClass = src.providerClass; 114 } 115 116 dst.accessTokenKey = src.accessTokenKey; 117 118 dst.enabled = src.enabled; 119 } 120 121 public OAuth2ServiceProvider getProvider(String name) { 122 OAuth2ServiceProvider provider = null; 123 OAuth2ServiceProviderDescriptor descriptor = providers.get(name); 124 if (descriptor != null && descriptor.isEnabled()) { 125 try { 126 Class<? extends OAuth2ServiceProvider> providerClass = descriptor.getProviderClass(); 127 provider = providerClass.newInstance(); 128 provider.setDescription(descriptor.getDescription()); 129 provider.setAuthorizationServerURL(descriptor.getAuthorizationServerURL()); 130 provider.setTokenServerURL(descriptor.getTokenServerURL()); 131 provider.setServiceName(descriptor.getName()); 132 provider.setClientId(descriptor.getClientId()); 133 provider.setClientSecret(descriptor.getClientSecret()); 134 provider.setScopes(descriptor.getScopes()); 135 provider.setEnabled(descriptor.isEnabled()); 136 } catch (Exception e) { 137 log.error("Failed to instantiate UserResolver", e); 138 } 139 } 140 return provider; 141 } 142 143 public Collection<OAuth2ServiceProviderDescriptor> getContribs() { 144 return providers.values(); 145 } 146}