Split constructor code from factory code of ScimClient
This commit is contained in:
parent
84f1af036d
commit
b6d3c20fe1
2 changed files with 50 additions and 52 deletions
|
@ -23,43 +23,63 @@ import org.keycloak.models.RoleMapperModel;
|
||||||
import org.keycloak.storage.user.SynchronizationResult;
|
import org.keycloak.storage.user.SynchronizationResult;
|
||||||
import sh.libre.scim.jpa.ScimResource;
|
import sh.libre.scim.jpa.ScimResource;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
public class ScimClient {
|
public class ScimClient {
|
||||||
protected final Logger LOGGER = Logger.getLogger(ScimClient.class);
|
|
||||||
protected final ScimRequestBuilder scimRequestBuilder;
|
|
||||||
protected final RetryRegistry registry;
|
|
||||||
protected final KeycloakSession session;
|
|
||||||
protected final String contentType;
|
|
||||||
protected final ComponentModel model;
|
|
||||||
protected final String scimApplicationBaseUrl;
|
|
||||||
protected final Map<String, String> defaultHeaders;
|
|
||||||
protected final Map<String, String> expectedResponseHeaders;
|
|
||||||
|
|
||||||
public ScimClient(ComponentModel model, KeycloakSession session) {
|
private static final Logger LOGGER = Logger.getLogger(ScimClient.class);
|
||||||
this.model = model;
|
|
||||||
this.contentType = model.get("content-type");
|
private final ScimRequestBuilder scimRequestBuilder;
|
||||||
|
|
||||||
|
private final RetryRegistry registry;
|
||||||
|
|
||||||
|
private final KeycloakSession session;
|
||||||
|
|
||||||
|
private final ComponentModel model;
|
||||||
|
|
||||||
|
private ScimClient(ScimRequestBuilder scimRequestBuilder, RetryRegistry registry, KeycloakSession session, ComponentModel model) {
|
||||||
|
this.scimRequestBuilder = scimRequestBuilder;
|
||||||
|
this.registry = registry;
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.scimApplicationBaseUrl = model.get("endpoint");
|
this.model = model;
|
||||||
this.defaultHeaders = new HashMap<>();
|
}
|
||||||
this.expectedResponseHeaders = new HashMap<>();
|
|
||||||
|
|
||||||
switch (model.get("auth-mode")) {
|
public static ScimClient newScimClient(ComponentModel model, KeycloakSession session) {
|
||||||
case "BEARER":
|
String authMode = model.get("auth-mode");
|
||||||
defaultHeaders.put(HttpHeaders.AUTHORIZATION,
|
String authorizationHeaderValue = switch (authMode) {
|
||||||
BearerAuthentication());
|
case "BEARER" -> "Bearer " + model.get("auth-pass");
|
||||||
break;
|
case "BASIC_AUTH" -> {
|
||||||
case "BASIC_AUTH":
|
BasicAuth basicAuth = BasicAuth.builder()
|
||||||
defaultHeaders.put(HttpHeaders.AUTHORIZATION,
|
.username(model.get("auth-user"))
|
||||||
BasicAuthentication());
|
.password(model.get("auth-pass"))
|
||||||
break;
|
.build();
|
||||||
}
|
yield basicAuth.getAuthorizationHeaderValue();
|
||||||
|
}
|
||||||
|
default -> throw new IllegalArgumentException("authMode " + authMode + " is not supported");
|
||||||
|
};
|
||||||
|
|
||||||
defaultHeaders.put(HttpHeaders.CONTENT_TYPE, contentType);
|
Map<String, String> httpHeaders = new HashMap<>();
|
||||||
|
httpHeaders.put(HttpHeaders.AUTHORIZATION, authorizationHeaderValue);
|
||||||
|
httpHeaders.put(HttpHeaders.CONTENT_TYPE, model.get("content-type"));
|
||||||
|
|
||||||
scimRequestBuilder = new ScimRequestBuilder(scimApplicationBaseUrl, genScimClientConfig());
|
ScimClientConfig scimClientConfig = ScimClientConfig.builder()
|
||||||
|
.httpHeaders(httpHeaders)
|
||||||
|
.connectTimeout(5)
|
||||||
|
.requestTimeout(5)
|
||||||
|
.socketTimeout(5)
|
||||||
|
.expectedHttpResponseHeaders(Collections.emptyMap()) // strange, useful?
|
||||||
|
.hostnameVerifier((s, sslSession) -> true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String scimApplicationBaseUrl = model.get("endpoint");
|
||||||
|
ScimRequestBuilder scimRequestBuilder =
|
||||||
|
new ScimRequestBuilder(
|
||||||
|
scimApplicationBaseUrl,
|
||||||
|
scimClientConfig
|
||||||
|
);
|
||||||
|
|
||||||
RetryConfig retryConfig = RetryConfig.custom()
|
RetryConfig retryConfig = RetryConfig.custom()
|
||||||
.maxAttempts(10)
|
.maxAttempts(10)
|
||||||
|
@ -67,30 +87,8 @@ public class ScimClient {
|
||||||
.retryExceptions(ProcessingException.class)
|
.retryExceptions(ProcessingException.class)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
registry = RetryRegistry.of(retryConfig);
|
RetryRegistry retryRegistry = RetryRegistry.of(retryConfig);
|
||||||
}
|
return new ScimClient(scimRequestBuilder, retryRegistry, session, model);
|
||||||
|
|
||||||
protected String BasicAuthentication() {
|
|
||||||
return BasicAuth.builder()
|
|
||||||
.username(model.get("auth-user"))
|
|
||||||
.password(model.get("auth-pass"))
|
|
||||||
.build()
|
|
||||||
.getAuthorizationHeaderValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ScimClientConfig genScimClientConfig() {
|
|
||||||
return ScimClientConfig.builder()
|
|
||||||
.httpHeaders(defaultHeaders)
|
|
||||||
.connectTimeout(5)
|
|
||||||
.requestTimeout(5)
|
|
||||||
.socketTimeout(5)
|
|
||||||
.expectedHttpResponseHeaders(expectedResponseHeaders)
|
|
||||||
.hostnameVerifier((s, sslSession) -> true)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String BearerAuthentication() {
|
|
||||||
return "Bearer " + model.get("auth-pass");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected EntityManager getEM() {
|
protected EntityManager getEM() {
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class ScimDispatcher {
|
||||||
|
|
||||||
public void runOne(ComponentModel m, Consumer<ScimClient> f) {
|
public void runOne(ComponentModel m, Consumer<ScimClient> f) {
|
||||||
LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType());
|
LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType());
|
||||||
ScimClient client = new ScimClient(m, session);
|
ScimClient client = ScimClient.newScimClient(m, session);
|
||||||
try {
|
try {
|
||||||
f.accept(client);
|
f.accept(client);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
Loading…
Reference in a new issue