|
1 | 1 | # Configuration |
2 | 2 |
|
3 | 3 | - [Proxy configuration](#proxy-configuration) |
| 4 | + - [Custom proxy authenticator](#custom-proxy-authenticator) |
| 5 | + - [Example: NTLM authenticator](#example-ntlm-authenticator) |
4 | 6 | - [Configure retries of calls and timeouts](#configure-retries-of-calls-and-timeouts) |
5 | 7 | - [Maximum retries](#maximum-retries) |
6 | 8 | - [Connection timeout](#connection-timeout) |
|
11 | 13 | - [Base App URL](#base-app-url) |
12 | 14 | - [Token URL](#token-url-deprecated) |
13 | 15 | - [Revoke URL](#revoke-url-deprecated) |
| 16 | +- [SSL configuration](#ssl-configuration) |
14 | 17 |
|
15 | 18 | # Proxy configuration |
16 | 19 |
|
17 | 20 | To set up proxy |
18 | 21 | use [BoxApiConnection.setProxy](https://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxAPIConnection.html#setProxy-java.net.Proxy-) |
19 | 22 | to set proxy address |
20 | | -and [BoxApiConnection.setProxyUsername](https://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxAPIConnection.html#setProxyUsername-java.lang.String-) / |
21 | | -[BoxApiConnection.setProxyPassword](https://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxAPIConnection.html#setProxyPassword-java.lang.String-) |
| 23 | +and [BoxApiConnection.setProxyBasicAuthentication][set-basic-proxy-auth] |
22 | 24 | to set username and password required by proxy: |
23 | 25 |
|
24 | 26 | ```java |
25 | 27 | BoxAPIConnection api=new BoxAPIConnection("access_token"); |
26 | | -Proxy proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy_url",8888)); |
| 28 | +Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy_url",8888)); |
27 | 29 | // You can use any subclass of BoxAPIConnection |
28 | 30 | api.setProxy(proxy); |
29 | | -api.setProxyUsername("proxyUsername"); |
30 | | -api.setProxyPassword("proxyPassword"); |
| 31 | +api.setProxyBasicAuthentication("proxyUsername", "proxyPassword"); |
31 | 32 | ``` |
| 33 | +Proxy username and password will be used only if provided `SocketAddress` is an instance of |
| 34 | +`InetSocketAddress`. If you would like to use a custom `SocketAddress` you can provide your own |
| 35 | +`okhttp3.Authenticator` using [BoxApiConnection.setProxyAuthenticator(Authenticator)][set-proxy-authenticator] |
| 36 | + |
| 37 | + |
| 38 | +## Custom proxy authenticator |
| 39 | +By using [BoxApiConnection.setProxyBasicAuthentication][set-basic-proxy-auth] you can enable default |
| 40 | +proxy authenticator that handles only Basic authentication. But you can provide your own authenticator by using |
| 41 | +[BoxApiConnection.setProxyAuthenticator(Authenticator)][set-proxy-authenticator]. |
| 42 | + |
| 43 | +To do that you will need to add a dependency to your project: |
| 44 | +``` |
| 45 | +"com.squareup.okhttp3:okhttp:XXX" |
| 46 | +``` |
| 47 | +Please match the version with what SDK is using by checking `build.gradle` |
| 48 | +and looking for entry `implementation "com.squareup.okhttp3:okhttp:"`. |
| 49 | + |
| 50 | +Now you can add an authenticator. by calling |
| 51 | + |
| 52 | +```java |
| 53 | +BoxAPIConnection api = new BoxAPIConnection("access_token"); |
| 54 | +Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy_url",8888)); |
| 55 | +api.setProxy(proxy); |
| 56 | +api.setProxyAuthenticator((route, response) -> response |
| 57 | + .request() |
| 58 | + .newBuilder() |
| 59 | + .addHeader("Proxy-Authorization", "My custom authenticator") |
| 60 | + .build() |
| 61 | +); |
| 62 | +``` |
| 63 | + |
| 64 | +### Example: NTLM authenticator |
| 65 | + |
| 66 | +For example, you can add NTLM authorization. This is example NTLM authenticator that |
| 67 | +is using parts from Apache Http Client 5. |
| 68 | + |
| 69 | +```java |
| 70 | +import okhttp3.Authenticator; |
| 71 | +import okhttp3.Request; |
| 72 | +import okhttp3.Response; |
| 73 | +import okhttp3.Route; |
| 74 | +import org.apache.hc.client5.http.impl.auth.NTLMEngineException; |
| 75 | + |
| 76 | +public class NTLMAuthenticator implements Authenticator { |
| 77 | + final NTLMEngineImpl engine = new NTLMEngineImpl(); |
| 78 | + private final String domain; |
| 79 | + private final String username; |
| 80 | + private final String password; |
| 81 | + private final String ntlmMsg1; |
| 82 | + |
| 83 | + public NTLMAuthenticator(String username, String password, String domain) { |
| 84 | + this.domain = domain; |
| 85 | + this.username = username; |
| 86 | + this.password = password; |
| 87 | + String localNtlmMsg1 = null; |
| 88 | + try { |
| 89 | + localNtlmMsg1 = engine.generateType1Msg(null, null); |
| 90 | + } catch (Exception e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + ntlmMsg1 = localNtlmMsg1; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Request authenticate(Route route, Response response) { |
| 98 | + if(response.code() == 407 && "Proxy authorization required".equals(response.message())) { |
| 99 | + String ntlmChallenge = response.headers("Proxy-Authenticate") |
| 100 | + .stream() |
| 101 | + .filter(h -> h.startsWith("NTLM ")) |
| 102 | + .findFirst().orElse(""); |
| 103 | + if(ntlmChallenge.length() > 5) { |
| 104 | + try { |
| 105 | + String ntlmMsg3 = engine.generateType3Msg(username, password.toCharArray(), domain, "ok-http-example-ntlm", ntlmChallenge.substring(5)); |
| 106 | + return response.request().newBuilder().header("proxy-Authorization", "NTLM " + ntlmMsg3).build(); |
| 107 | + } catch (NTLMEngineException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + return response.request().newBuilder().header("proxy-Authorization", "NTLM " + ntlmMsg1).build(); |
| 112 | + } |
| 113 | + return response.request(); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +The `NTLMEngineImpl` could be created by using Apache implementation that can be found |
| 119 | +[here](https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMEngineImpl.java). |
| 120 | +You can add a dependency to `org.apache.httpcomponents.client5:httpclient5:5.1.3`. |
| 121 | +Copy the `NTLMEngineImpl` class and add it to your source. |
| 122 | + |
| 123 | +Now you can use custom NTML Authenticator in your `BoxAPIConnection`: |
| 124 | +```java |
| 125 | +BoxAPIConnection api = new BoxAPIConnection("access_token"); |
| 126 | +Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy_url",8888)); |
| 127 | +api.setProxy(proxy); |
| 128 | +api.setProxyAuthenticator(new NTLMAuthenticator("some proxy username", "some proxy password", "proxy workgroup")); |
| 129 | +``` |
| 130 | + |
| 131 | +[set-basic-proxy-auth]: https://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxAPIConnection.html#setProxyBasicAuthentication-java.lang.String-java.lang.String- |
| 132 | +[set-proxy-authenticator]: https://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxAPIConnection.html#setProxyAuthenticator-okhttp3.Authenticator- |
32 | 133 |
|
33 | 134 | # Configure retries of calls and timeouts |
34 | 135 | SDK can retry failed calls when: |
@@ -151,3 +252,41 @@ api.setRevokeURL("https://example.com/revoke"); |
151 | 252 | ``` |
152 | 253 |
|
153 | 254 | If you use `setRevokeUrl` this URL will be used over the one coming from`setBaseUrl` when doing authentication. |
| 255 | + |
| 256 | +# SSL configuration |
| 257 | +You can override default settings used to verify SSL certificates. |
| 258 | +This can be used to allow using self-signed certificates. For example: |
| 259 | +```java |
| 260 | +BoxAPIConnection api = new BoxAPIConnection(...); |
| 261 | + |
| 262 | +// to allow self-signed certificates |
| 263 | +X509TrustManager trustManager = new X509TrustManager() { |
| 264 | + @Override |
| 265 | + public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
| 274 | + return new java.security.cert.X509Certificate[]{}; |
| 275 | + } |
| 276 | +}; |
| 277 | + |
| 278 | +// to allow self-signed certificates created for localhost |
| 279 | +HostnameVerifier hostnameVerifier = (hostname, session) -> true; |
| 280 | + |
| 281 | +api.configureSslCertificatesValidation(trustManager, hostnameVerifier); |
| 282 | +``` |
| 283 | + |
| 284 | +If you just need to provide trust manager use `BoxAPIConnection.DEFAULT_HOSTNAME_VERIFIER` as a hostname verifier. |
| 285 | +The same goes for hostname verifier. If you need just to provide it use |
| 286 | +`BoxAPIConnection.DEFAULT_TRUST_MANAGER` as a trust manager. |
| 287 | +Example: |
| 288 | +```java |
| 289 | +BoxAPIConnection api = new BoxAPIConnection(...); |
| 290 | +X509TrustManager trustManager = ... |
| 291 | +api.configureSslCertificatesValidation(trustManager, BoxAPIConnection.DEFAULT_HOSTNAME_VERIFIER); |
| 292 | +``` |
0 commit comments