/
IdentityProviderManagerImpl.java
181 lines (156 loc) · 8.41 KB
/
IdentityProviderManagerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.rest.api.idp.core.plugin.impl;
import io.gravitee.common.util.RelaxedPropertySource;
import io.gravitee.node.api.secrets.model.Secret;
import io.gravitee.plugin.core.api.Plugin;
import io.gravitee.plugin.core.api.PluginContextFactory;
import io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer;
import io.gravitee.rest.api.idp.api.IdentityProvider;
import io.gravitee.rest.api.idp.api.authentication.AuthenticationProvider;
import io.gravitee.rest.api.idp.api.identity.IdentityLookup;
import io.gravitee.rest.api.idp.core.authentication.impl.CompositeIdentityManager;
import io.gravitee.rest.api.idp.core.plugin.IdentityProviderDefinition;
import io.gravitee.rest.api.idp.core.plugin.IdentityProviderManager;
import java.util.*;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
/**
* @author David BRASSELY (david at gravitee.io)
* @author GraviteeSource Team
*/
public class IdentityProviderManagerImpl implements IdentityProviderManager {
private static final Logger LOGGER = LoggerFactory.getLogger(IdentityProviderManagerImpl.class);
private final Map<String, IdentityProvider> identityProviders = new HashMap<>();
private final Map<IdentityProvider, Plugin> identityProviderPlugins = new HashMap<>();
@Autowired
private PluginContextFactory pluginContextFactory;
@Autowired
private CompositeIdentityManager compositeIdentityManager;
@Override
public void register(IdentityProviderDefinition identityProviderPluginDefinition) {
identityProviders.putIfAbsent(
identityProviderPluginDefinition.getIdentityProvider().type(),
identityProviderPluginDefinition.getIdentityProvider()
);
identityProviderPlugins.putIfAbsent(
identityProviderPluginDefinition.getIdentityProvider(),
identityProviderPluginDefinition.getPlugin()
);
}
@Override
public Collection<IdentityProvider> getAll() {
return identityProviders.values();
}
@Override
public AuthenticationProvider loadIdentityProvider(String identityProvider, Map<String, Object> properties) {
// By loading an identity provider we are mounting both authentication provider and identity lookup
AuthenticationProvider authenticationProvider = authenticationProvider(identityProvider, properties);
IdentityLookup identityLookup = identityLookup(identityProvider, properties);
compositeIdentityManager.addIdentityLookup(identityLookup);
return authenticationProvider;
}
private AuthenticationProvider authenticationProvider(String identityProviderType, Map<String, Object> properties) {
LOGGER.debug("Looking for an authentication provider for [{}]", identityProviderType);
IdentityProvider identityProvider = identityProviders.get(identityProviderType);
if (identityProvider != null) {
return create(identityProviderPlugins.get(identityProvider), identityProvider.authenticationProvider(), properties);
} else {
LOGGER.error("No identity provider is registered for type {}", identityProviderType);
throw new IllegalStateException("No identity provider is registered for type " + identityProviderType);
}
}
private IdentityLookup identityLookup(String identityProviderType, Map<String, Object> properties) {
LOGGER.debug("Looking for an identity lookup for [{}]", identityProviderType);
IdentityProvider identityProvider = identityProviders.get(identityProviderType);
if (identityProvider != null) {
return create(identityProviderPlugins.get(identityProvider), identityProvider.identityLookup(), properties);
} else {
LOGGER.error("No identity provider is registered for type {}", identityProviderType);
throw new IllegalStateException("No identity provider is registered for type " + identityProviderType);
}
}
private <T> T create(Plugin plugin, Class<T> identityClass, Map<String, Object> properties) {
if (identityClass == null) {
return null;
}
try {
T identityObj = createInstance(identityClass);
final Import annImport = identityClass.getAnnotation(Import.class);
Set<Class<?>> configurations = (annImport != null) ? new HashSet<>(Arrays.asList(annImport.value())) : Collections.emptySet();
ApplicationContext idpApplicationContext = pluginContextFactory.create(
new AnnotationBasedPluginContextConfigurer(plugin) {
@Override
public Set<Class<?>> configurations() {
return configurations;
}
@Override
public ConfigurableEnvironment environment() {
return new StandardEnvironment() {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addFirst(new RelaxedPropertySource(plugin.id(), properties));
super.customizePropertySources(propertySources);
// add missing converters in this newly created environment
// this syntax allows a property of any kind to be converted from a secret. eg. Secret +> String -> Double
this.getConversionService()
.addConverterFactory(
new ConverterFactory<Secret, Object>() {
final ConversionService conversionService = DefaultConversionService.getSharedInstance();
@Nonnull
public <C> Converter<Secret, C> getConverter(@Nonnull Class<C> targetType) {
return source -> conversionService.convert(source.asString(), targetType);
}
}
);
// byte[] has to be created separately
this.getConversionService().addConverter(Secret.class, String.class, Secret::asString);
}
};
}
}
);
idpApplicationContext.getAutowireCapableBeanFactory().autowireBean(identityObj);
if (identityObj instanceof InitializingBean) {
((InitializingBean) identityObj).afterPropertiesSet();
}
return identityObj;
} catch (Exception ex) {
LOGGER.error("An unexpected error occurs while loading identity provider", ex);
return null;
}
}
private <T> T createInstance(Class<T> clazz) throws Exception {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
LOGGER.error("Unable to instantiate class: {}", ex);
throw ex;
}
}
}