Created
March 30, 2018 23:09
-
-
Save jviveiros/8815bc71aa17bac30c6ea139e0a44e62 to your computer and use it in GitHub Desktop.
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.verygoodsecurity.example; | |
import com.mashape.unirest.http.Unirest; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
import org.apache.http.HttpHost; | |
import org.apache.http.auth.AuthSchemeProvider; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.CredentialsProvider; | |
import org.apache.http.client.config.AuthSchemes; | |
import org.apache.http.config.Lookup; | |
import org.apache.http.config.RegistryBuilder; | |
import org.apache.http.impl.auth.BasicSchemeFactory; | |
import org.apache.http.impl.client.BasicCredentialsProvider; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.http.impl.client.ProxyAuthenticationStrategy; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
public class RevealAPICall { | |
static String username = System.getenv("FORWARD_HTTP_PROXY_USERNAME"); | |
static String password = System.getenv("FORWARD_HTTP_PROXY_PASSWORD"); | |
static String forward_proxy = System.getenv("FORWARD_HTTP_PROXY_HOST"); | |
static String reverse_proxy = System.getenv("REVERSE_HTTP_PROXY_HOST"); | |
static { | |
Unirest.setDefaultHeader("Content-type", "application/json"); | |
} | |
static String revealViaForwardProxy(String tokenizedData) throws UnirestException { | |
System.setProperty("javax.net.ssl.trustStore", ExampleTest.class.getClassLoader().getResource("cacerts").getFile()); | |
System.setProperty("javax.net.ssl.trustStorePassword", "password"); | |
System.setProperty("javax.net.ssl.trustStoreType", "JKS"); | |
HttpClientBuilder clientBuilder = HttpClientBuilder.create(); | |
CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); | |
clientBuilder.useSystemProperties(); | |
clientBuilder.setProxy(HttpHost.create(forward_proxy)); | |
clientBuilder.setDefaultCredentialsProvider(credsProvider); | |
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); | |
Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create() | |
.register(AuthSchemes.BASIC, new BasicSchemeFactory()) | |
.build(); | |
clientBuilder.setDefaultAuthSchemeRegistry(authProviders); | |
Unirest.setHttpClient(clientBuilder.build()); | |
return Unirest.post("https://httpbin.verygoodsecurity.io/post") | |
.body(tokenizedData) | |
.asJson().getBody().getObject() | |
.getString("data"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment