From 235de42a4777b52a1408f5739d47785d31e1e967 Mon Sep 17 00:00:00 2001 From: Alan Braz <alanbraz@br.ibm.com> Date: Wed, 25 Mar 2020 11:45:35 -0300 Subject: [PATCH] get user from env var --- .../indexer/ibm/util/KeyCloakProvider.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/KeyCloakProvider.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/KeyCloakProvider.java index cb264e9ac..086292f6e 100644 --- a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/KeyCloakProvider.java +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/KeyCloakProvider.java @@ -20,10 +20,14 @@ import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + import com.google.gson.Gson; import com.google.gson.JsonObject; -public class KeyCloakUser { +@Component +public class KeyCloakProvider { static { disableSslVerification(); @@ -65,26 +69,30 @@ public class KeyCloakUser { } } -// curl --request POST \ -// --url https://keycloak-osdu-r2.osduadev-a1c3eaf78a86806e299f5f3f207556f0-0000.us-south.containers.appdomain.cloud/auth/realms/OSDU/protocol/openid-connect/token \ -// --header 'content-type: application/x-www-form-urlencoded' \ -// --data grant_type=password \ -// --data client_id=osdu-login \ -// --data username=osdu-user \ -// --data password=password1 - - public static String getToken() throws IOException { - //String aad_endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenant_id); - URL url = new URL("https://keycloak-osdu-r2.osduadev-a1c3eaf78a86806e299f5f3f207556f0-0000.us-south.containers.appdomain.cloud/auth/realms/OSDU/protocol/openid-connect/token"); + @Value("${ibm.keycloak.endpoint_url}") + private String url; + + @Value("${ibm.keycloak.realm}") + private String realm ; + + @Value("${ibm.keycloak.grant_type:password}") + private String grantType; + + @Value("${ibm.keycloak.client_id}") + private String clientId; + + public String getToken(String user, String password) throws IOException { + String endpoint = String.format("https://%s/auth/realms/%s/protocol/openid-connect/token", url, realm); + URL url = new URL(endpoint); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); Map<String, String> parameters = new HashMap<>(); - parameters.put("grant_type", "password"); - parameters.put("client_id", "osdu-login"); - parameters.put("username", "osdu-user"); - parameters.put("password", "password1"); + parameters.put("grant_type", grantType); + parameters.put("client_id", clientId); + parameters.put("username", user); + parameters.put("password", password); con.setDoOutput(true); DataOutputStream out = new DataOutputStream(con.getOutputStream()); -- GitLab