refactor(oidc): use similar rest client config across OIDC (#2411)
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package org.booklore.config.security.oidc;
|
package org.booklore.config.security.oidc;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -15,14 +15,14 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
public class OidcDiscoveryService {
|
public class OidcDiscoveryService {
|
||||||
|
|
||||||
private static final Pattern TRAILING_SLASH = Pattern.compile("/+$");
|
private static final Pattern TRAILING_SLASH = Pattern.compile("/+$");
|
||||||
private static final long CACHE_TTL_MS = 3_600_000; // 1 hour
|
private static final long CACHE_TTL_MS = 3_600_000; // 1 hour
|
||||||
private static final int CONNECT_TIMEOUT_MS = 10_000;
|
|
||||||
private static final int READ_TIMEOUT_MS = 10_000;
|
|
||||||
|
|
||||||
private final ConcurrentMap<String, CachedDiscovery> cache = new ConcurrentHashMap<>();
|
private final ConcurrentMap<String, CachedDiscovery> cache = new ConcurrentHashMap<>();
|
||||||
|
private final RestTemplate oidcRestTemplate;
|
||||||
|
|
||||||
public record DiscoveryDocument(
|
public record DiscoveryDocument(
|
||||||
String issuer,
|
String issuer,
|
||||||
@@ -63,16 +63,10 @@ public class OidcDiscoveryService {
|
|||||||
String discoveryUrl = issuerUri + "/.well-known/openid-configuration";
|
String discoveryUrl = issuerUri + "/.well-known/openid-configuration";
|
||||||
log.info("Fetching OIDC discovery document from {}", discoveryUrl);
|
log.info("Fetching OIDC discovery document from {}", discoveryUrl);
|
||||||
|
|
||||||
var factory = new SimpleClientHttpRequestFactory();
|
Map<String, Object> doc = oidcRestTemplate.getForObject(
|
||||||
factory.setConnectTimeout(CONNECT_TIMEOUT_MS);
|
discoveryUrl,
|
||||||
factory.setReadTimeout(READ_TIMEOUT_MS);
|
Map.class
|
||||||
|
);
|
||||||
var restClient = RestClient.builder().requestFactory(factory).build();
|
|
||||||
|
|
||||||
Map<String, Object> doc = restClient.get()
|
|
||||||
.uri(discoveryUrl)
|
|
||||||
.retrieve()
|
|
||||||
.body(Map.class);
|
|
||||||
|
|
||||||
if (doc == null) {
|
if (doc == null) {
|
||||||
throw new IllegalStateException("Failed to fetch OIDC discovery document from " + discoveryUrl);
|
throw new IllegalStateException("Failed to fetch OIDC discovery document from " + discoveryUrl);
|
||||||
|
|||||||
@@ -4,15 +4,14 @@ import com.nimbusds.jose.jwk.JWKSet;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.booklore.model.dto.settings.OidcProviderDetails;
|
import org.booklore.model.dto.settings.OidcProviderDetails;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClient;
|
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.booklore.util.FileUtils;
|
import org.booklore.util.FileUtils;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -28,6 +27,8 @@ public class OidcDiagnosticService {
|
|||||||
private static final int CONNECT_TIMEOUT_MS = 10_000;
|
private static final int CONNECT_TIMEOUT_MS = 10_000;
|
||||||
private static final int READ_TIMEOUT_MS = 10_000;
|
private static final int READ_TIMEOUT_MS = 10_000;
|
||||||
|
|
||||||
|
private final RestTemplate oidcRestTemplate;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public OidcTestResult testConnection(OidcProviderDetails providerDetails) {
|
public OidcTestResult testConnection(OidcProviderDetails providerDetails) {
|
||||||
List<OidcTestCheck> checks = new ArrayList<>();
|
List<OidcTestCheck> checks = new ArrayList<>();
|
||||||
@@ -39,12 +40,7 @@ public class OidcDiagnosticService {
|
|||||||
String issuerUri = FileUtils.trimTrailingSlashes(providerDetails.getIssuerUri());
|
String issuerUri = FileUtils.trimTrailingSlashes(providerDetails.getIssuerUri());
|
||||||
String discoveryUrl = issuerUri + "/.well-known/openid-configuration";
|
String discoveryUrl = issuerUri + "/.well-known/openid-configuration";
|
||||||
|
|
||||||
var factory = new SimpleClientHttpRequestFactory();
|
doc = oidcRestTemplate.getForObject(discoveryUrl, Map.class);
|
||||||
factory.setConnectTimeout(CONNECT_TIMEOUT_MS);
|
|
||||||
factory.setReadTimeout(READ_TIMEOUT_MS);
|
|
||||||
|
|
||||||
var restClient = RestClient.builder().requestFactory(factory).build();
|
|
||||||
doc = restClient.get().uri(discoveryUrl).retrieve().body(Map.class);
|
|
||||||
|
|
||||||
if (doc == null) {
|
if (doc == null) {
|
||||||
checks.add(new OidcTestCheck("Discovery Document", CheckStatus.FAIL, "Empty response from discovery endpoint"));
|
checks.add(new OidcTestCheck("Discovery Document", CheckStatus.FAIL, "Empty response from discovery endpoint"));
|
||||||
|
|||||||
Reference in New Issue
Block a user