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