Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed Feb 19, 2021
1 parent 8e08fad commit bb4b45f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,138 +11,140 @@
*/
package org.eclipse.che.multiuser.keycloak.server;

import static com.google.common.base.MoreObjects.firstNonNull;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_INTERNAL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.OIDC_PROVIDER_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.proxy.ProxyAuthenticator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

import static com.google.common.base.MoreObjects.firstNonNull;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_INTERNAL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.OIDC_PROVIDER_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.proxy.ProxyAuthenticator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* OIDCInfoProvider retrieves OpenID Connect (OIDC) configuration for well-known endpoint. These
* information is useful to provide access to the Keycloak api.
*/
public class OIDCInfoProvider implements Provider<OIDCInfo> {

private static final Logger LOG = LoggerFactory.getLogger(OIDCInfoProvider.class);

@Inject
@Nullable
@Named(AUTH_SERVER_URL_SETTING)
protected String serverURL;

@Inject
@Nullable
@Named(AUTH_SERVER_URL_INTERNAL_SETTING)
protected String serverInternalURL;

@Inject
@Nullable
@Named(OIDC_PROVIDER_SETTING)
protected String oidcProviderUrl;

@Inject
@Nullable
@Named(REALM_SETTING)
protected String realm;

/** @return OIDCInfo with OIDC settings information. */
@Override
public OIDCInfo get() {
this.validate();

String serverAuthUrl = (serverInternalURL != null) ? serverInternalURL : serverURL;
String wellKnownEndpoint = this.getWellKnownEndpoint(serverAuthUrl);

LOG.info("Retrieving OpenId configuration from endpoint: {}", wellKnownEndpoint);
ProxyAuthenticator.initAuthenticator(wellKnownEndpoint);
try (InputStream inputStream = new URL(wellKnownEndpoint).openStream()) {
final JsonParser parser = new JsonFactory().createParser(inputStream);
final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() {};

Map<String, Object> openIdConfiguration =
new ObjectMapper().reader().readValue(parser, typeReference);

LOG.info("openid configuration = {}", openIdConfiguration);

String tokenPublicEndPoint = setPublicUrl((String)openIdConfiguration.get("token_endpoint"));
String userInfoPublicEndpoint = setPublicUrl((String)openIdConfiguration.get("userinfo_endpoint"));
String endSessionPublicEndpoint = setPublicUrl((String)openIdConfiguration.get("end_session_endpoint"));
String jwksPublicUri = setPublicUrl((String)openIdConfiguration.get("jwks_uri"));
String jwksUri = setInternalUrl(jwksPublicUri);
String userInfoEndpoint = setInternalUrl(userInfoPublicEndpoint);

return new OIDCInfo(
tokenPublicEndPoint,
endSessionPublicEndpoint,
userInfoPublicEndpoint,
userInfoEndpoint,
jwksPublicUri,
jwksUri,
serverAuthUrl,
serverURL);
} catch (IOException e) {
throw new RuntimeException(
"Exception while retrieving OpenId configuration from endpoint: " + wellKnownEndpoint, e);
} finally {
ProxyAuthenticator.resetAuthenticator();
}
private static final Logger LOG = LoggerFactory.getLogger(OIDCInfoProvider.class);

@Inject
@Nullable
@Named(AUTH_SERVER_URL_SETTING)
protected String serverURL;

@Inject
@Nullable
@Named(AUTH_SERVER_URL_INTERNAL_SETTING)
protected String serverInternalURL;

@Inject
@Nullable
@Named(OIDC_PROVIDER_SETTING)
protected String oidcProviderUrl;

@Inject
@Nullable
@Named(REALM_SETTING)
protected String realm;

/** @return OIDCInfo with OIDC settings information. */
@Override
public OIDCInfo get() {
this.validate();

String serverAuthUrl = (serverInternalURL != null) ? serverInternalURL : serverURL;
String wellKnownEndpoint = this.getWellKnownEndpoint(serverAuthUrl);

LOG.info("Retrieving OpenId configuration from endpoint: {}", wellKnownEndpoint);
ProxyAuthenticator.initAuthenticator(wellKnownEndpoint);
try (InputStream inputStream = new URL(wellKnownEndpoint).openStream()) {
final JsonParser parser = new JsonFactory().createParser(inputStream);
final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() {};

Map<String, Object> openIdConfiguration =
new ObjectMapper().reader().readValue(parser, typeReference);

LOG.info("openid configuration = {}", openIdConfiguration);

String tokenPublicEndPoint = setPublicUrl((String) openIdConfiguration.get("token_endpoint"));
String userInfoPublicEndpoint =
setPublicUrl((String) openIdConfiguration.get("userinfo_endpoint"));
String endSessionPublicEndpoint =
setPublicUrl((String) openIdConfiguration.get("end_session_endpoint"));
String jwksPublicUri = setPublicUrl((String) openIdConfiguration.get("jwks_uri"));
String jwksUri = setInternalUrl(jwksPublicUri);
String userInfoEndpoint = setInternalUrl(userInfoPublicEndpoint);

return new OIDCInfo(
tokenPublicEndPoint,
endSessionPublicEndpoint,
userInfoPublicEndpoint,
userInfoEndpoint,
jwksPublicUri,
jwksUri,
serverAuthUrl,
serverURL);
} catch (IOException e) {
throw new RuntimeException(
"Exception while retrieving OpenId configuration from endpoint: " + wellKnownEndpoint, e);
} finally {
ProxyAuthenticator.resetAuthenticator();
}
}

private String getWellKnownEndpoint(String serverAuthUrl) {
String wellKnownEndpoint = firstNonNull(oidcProviderUrl, serverAuthUrl + "/realms/" + realm);
if (!wellKnownEndpoint.endsWith("/")) {
wellKnownEndpoint = wellKnownEndpoint + "/";
}
wellKnownEndpoint += ".well-known/openid-configuration";
return wellKnownEndpoint;
private String getWellKnownEndpoint(String serverAuthUrl) {
String wellKnownEndpoint = firstNonNull(oidcProviderUrl, serverAuthUrl + "/realms/" + realm);
if (!wellKnownEndpoint.endsWith("/")) {
wellKnownEndpoint = wellKnownEndpoint + "/";
}

private void validate() {
if (serverURL == null && serverInternalURL == null && oidcProviderUrl == null) {
throw new RuntimeException(
"Either the '"
+ AUTH_SERVER_URL_SETTING
+ "' or '"
+ AUTH_SERVER_URL_INTERNAL_SETTING
+ "' or '"
+ OIDC_PROVIDER_SETTING
+ "' property should be set");
}

if (oidcProviderUrl == null && realm == null) {
throw new RuntimeException("The '" + REALM_SETTING + "' property should be set");
}
wellKnownEndpoint += ".well-known/openid-configuration";
return wellKnownEndpoint;
}

private void validate() {
if (serverURL == null && serverInternalURL == null && oidcProviderUrl == null) {
throw new RuntimeException(
"Either the '"
+ AUTH_SERVER_URL_SETTING
+ "' or '"
+ AUTH_SERVER_URL_INTERNAL_SETTING
+ "' or '"
+ OIDC_PROVIDER_SETTING
+ "' property should be set");
}

private String setInternalUrl(String endpointUrl) {
if (serverURL != null && serverInternalURL != null) {
return endpointUrl.replace(serverURL, serverInternalURL);
}
return endpointUrl;
if (oidcProviderUrl == null && realm == null) {
throw new RuntimeException("The '" + REALM_SETTING + "' property should be set");
}
}

private String setPublicUrl(String endpointUrl) {
if (serverInternalURL != null && serverURL != null && endpointUrl.startsWith(serverInternalURL)) {
return endpointUrl.replace(serverInternalURL, serverURL);
}
return endpointUrl;
private String setInternalUrl(String endpointUrl) {
if (serverURL != null && serverInternalURL != null) {
return endpointUrl.replace(serverURL, serverInternalURL);
}
return endpointUrl;
}

private String setPublicUrl(String endpointUrl) {
if (serverInternalURL != null
&& serverURL != null
&& endpointUrl.startsWith(serverInternalURL)) {
return endpointUrl.replace(serverInternalURL, serverURL);
}
return endpointUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@
*/
package org.eclipse.che.multiuser.keycloak.server;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.testng.Assert.assertEquals;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class OIDCInfoProviderTest {
private WireMockServer wireMockServer;

Expand Down Expand Up @@ -184,7 +183,7 @@ public void shouldParseOIDCConfigurationWithPublicUrlsForInternalServerUrl() {
String serverInternalUrl = SERVER_URL;

String OPEN_ID_CONF_TEMPLATE =
""
""
+ "{"
+ " \"token_endpoint\": \""
+ serverInternalUrl
Expand All @@ -209,11 +208,11 @@ public void shouldParseOIDCConfigurationWithPublicUrlsForInternalServerUrl() {
+ "}";

stubFor(
get(urlEqualTo("/auth/realms/che/.well-known/openid-configuration"))
.willReturn(
aResponse()
.withHeader("Content-Type", "text/html")
.withBody(OPEN_ID_CONF_TEMPLATE)));
get(urlEqualTo("/auth/realms/che/.well-known/openid-configuration"))
.willReturn(
aResponse()
.withHeader("Content-Type", "text/html")
.withBody(OPEN_ID_CONF_TEMPLATE)));

OIDCInfoProvider oidcInfoProvider = new OIDCInfoProvider();
oidcInfoProvider.serverURL = serverPublicUrl;
Expand All @@ -222,24 +221,24 @@ public void shouldParseOIDCConfigurationWithPublicUrlsForInternalServerUrl() {
OIDCInfo oidcInfo = oidcInfoProvider.get();

assertEquals(
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/token",
oidcInfo.getTokenPublicEndpoint());
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/token",
oidcInfo.getTokenPublicEndpoint());
assertEquals(
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/logout",
oidcInfo.getEndSessionPublicEndpoint());
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/logout",
oidcInfo.getEndSessionPublicEndpoint());
assertEquals(
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/userinfo",
oidcInfo.getUserInfoPublicEndpoint());
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/userinfo",
oidcInfo.getUserInfoPublicEndpoint());
assertEquals(
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/certs",
oidcInfo.getJwksPublicUri());
serverPublicUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/certs",
oidcInfo.getJwksPublicUri());

assertEquals(
serverInternalUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/certs",
oidcInfo.getJwksUri());
serverInternalUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/certs",
oidcInfo.getJwksUri());
assertEquals(
serverInternalUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/userinfo",
oidcInfo.getUserInfoEndpoint());
serverInternalUrl + "/realms/" + CHE_REALM + "/protocol/openid-connect/userinfo",
oidcInfo.getUserInfoEndpoint());

assertEquals(serverInternalUrl, oidcInfo.getAuthServerURL());
assertEquals(serverPublicUrl, oidcInfo.getAuthServerPublicURL());
Expand Down

0 comments on commit bb4b45f

Please sign in to comment.