-
Notifications
You must be signed in to change notification settings - Fork 824
/
OidcMetadataFetcher.java
60 lines (51 loc) · 2.58 KB
/
OidcMetadataFetcher.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
package org.cloudfoundry.identity.uaa.provider.oauth;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.cloudfoundry.identity.uaa.cache.UrlContentCache;
import org.cloudfoundry.identity.uaa.provider.OIDCIdentityProviderDefinition;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.URL;
public class OidcMetadataFetcher {
private final UrlContentCache contentCache;
private final RestTemplate trustingRestTemplate;
private final RestTemplate nonTrustingRestTemplate;
public OidcMetadataFetcher(UrlContentCache contentCache,
RestTemplate trustingRestTemplate,
RestTemplate nonTrustingRestTemplate
) {
this.contentCache = contentCache;
this.trustingRestTemplate = trustingRestTemplate;
this.nonTrustingRestTemplate = nonTrustingRestTemplate;
}
public void fetchMetadataAndUpdateDefinition(OIDCIdentityProviderDefinition definition) throws OidcMetadataFetchingException {
if (shouldFetchMetadata(definition)) {
OidcMetadata oidcMetadata =
fetchMetadata(definition.getDiscoveryUrl(), definition.isSkipSslValidation());
updateIdpDefinition(definition, oidcMetadata);
}
}
private OidcMetadata fetchMetadata(URL discoveryUrl, boolean shouldDoSslValidation) throws OidcMetadataFetchingException {
byte[] rawContents;
if (shouldDoSslValidation) {
rawContents = contentCache.getUrlContent(discoveryUrl.toString(), trustingRestTemplate);
} else {
rawContents = contentCache.getUrlContent(discoveryUrl.toString(), nonTrustingRestTemplate);
}
try {
return new ObjectMapper().readValue(rawContents, OidcMetadata.class);
} catch (IOException e) {
throw new OidcMetadataFetchingException(e);
}
}
private void updateIdpDefinition(OIDCIdentityProviderDefinition definition, OidcMetadata oidcMetadata) {
definition.setAuthUrl(oidcMetadata.getAuthorizationEndpoint());
definition.setTokenUrl(oidcMetadata.getTokenEndpoint());
definition.setTokenKeyUrl(oidcMetadata.getJsonWebKeysUri());
definition.setUserInfoUrl(oidcMetadata.getUserinfoEndpoint());
definition.setIssuer(oidcMetadata.getIssuer());
}
private boolean shouldFetchMetadata(OIDCIdentityProviderDefinition definition) {
return definition.getDiscoveryUrl() != null && !StringUtils.isBlank(definition.getDiscoveryUrl().toString());
}
}