1616 */
1717package org .privacyidea ;
1818
19+ import okhttp3 .*;
20+
21+ import javax .net .ssl .SSLContext ;
22+ import javax .net .ssl .SSLSocketFactory ;
23+ import javax .net .ssl .TrustManager ;
24+ import javax .net .ssl .X509TrustManager ;
1925import java .io .IOException ;
20- import java .io .UnsupportedEncodingException ;
26+ import java .net .InetSocketAddress ;
27+ import java .net .Proxy ;
2128import java .net .URLEncoder ;
2229import java .nio .charset .StandardCharsets ;
2330import java .security .KeyManagementException ;
2431import java .security .NoSuchAlgorithmException ;
2532import java .util .Map ;
2633import java .util .concurrent .TimeUnit ;
27- import javax .net .ssl .SSLContext ;
28- import javax .net .ssl .SSLSocketFactory ;
29- import javax .net .ssl .TrustManager ;
30- import javax .net .ssl .X509TrustManager ;
31- import okhttp3 .Callback ;
32- import okhttp3 .FormBody ;
33- import okhttp3 .HttpUrl ;
34- import okhttp3 .OkHttpClient ;
35- import okhttp3 .Request ;
3634
37- import static org .privacyidea .PIConstants .GET ;
38- import static org .privacyidea .PIConstants .HEADER_USER_AGENT ;
39- import static org .privacyidea .PIConstants .POST ;
40- import static org .privacyidea .PIConstants .WEBAUTHN_PARAMETERS ;
35+ import static org .privacyidea .PIConstants .*;
4136
4237/**
4338 * This class handles sending requests to the server.
4439 */
4540class Endpoint
4641{
4742 private final PrivacyIDEA privacyIDEA ;
48- private final PIConfig piconfig ;
43+ private final PIConfig piConfig ;
4944 private final OkHttpClient client ;
5045
5146 final TrustManager [] trustAllManager = new TrustManager []{new X509TrustManager ()
@@ -70,14 +65,14 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers()
7065 Endpoint (PrivacyIDEA privacyIDEA )
7166 {
7267 this .privacyIDEA = privacyIDEA ;
73- this .piconfig = privacyIDEA .configuration ();
68+ this .piConfig = privacyIDEA .configuration ();
7469
7570 OkHttpClient .Builder builder = new OkHttpClient .Builder ();
76- builder .connectTimeout (piconfig .httpTimeoutMs , TimeUnit .MILLISECONDS )
77- .writeTimeout (piconfig .httpTimeoutMs , TimeUnit .MILLISECONDS )
78- .readTimeout (piconfig .httpTimeoutMs , TimeUnit .MILLISECONDS );
71+ builder .connectTimeout (piConfig .httpTimeoutMs , TimeUnit .MILLISECONDS )
72+ .writeTimeout (piConfig .httpTimeoutMs , TimeUnit .MILLISECONDS )
73+ .readTimeout (piConfig .httpTimeoutMs , TimeUnit .MILLISECONDS );
7974
80- if (!this .piconfig . doSSLVerify )
75+ if (!this .piConfig . verifySSL )
8176 {
8277 // Trust all certs and verify every host
8378 try
@@ -93,6 +88,13 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers()
9388 privacyIDEA .error (e );
9489 }
9590 }
91+
92+ if (!piConfig .proxyHost .isEmpty ())
93+ {
94+ Proxy proxy = new Proxy (Proxy .Type .HTTP , new InetSocketAddress (piConfig .proxyHost , piConfig .proxyPort ));
95+ builder .proxy (proxy );
96+ }
97+
9698 this .client = builder .build ();
9799 }
98100
@@ -105,44 +107,47 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers()
105107 * @param method http request method
106108 * @param callback okhttp3 callback
107109 */
108- void sendRequestAsync (String endpoint , Map <String , String > params , Map <String , String > headers , String method ,
109- Callback callback )
110+ void sendRequestAsync (String endpoint , Map <String , String > params , Map <String , String > headers , String method , Callback callback )
110111 {
111- HttpUrl httpUrl = HttpUrl .parse (piconfig .serverURL + endpoint );
112+ HttpUrl httpUrl = HttpUrl .parse (piConfig .serverURL + endpoint );
112113 if (httpUrl == null )
113114 {
114- privacyIDEA .error ("Server url could not be parsed: " + (piconfig .serverURL + endpoint ));
115+ privacyIDEA .error ("Server url could not be parsed: " + (piConfig .serverURL + endpoint ));
115116 // Invoke the callback to terminate the thread that called this function.
116117 callback .onFailure (null , new IOException ("Request could not be created because the url could not be parsed" ));
117118 return ;
118119 }
119120 HttpUrl .Builder urlBuilder = httpUrl .newBuilder ();
121+ if (!piConfig .forwardClientIP .isEmpty ())
122+ {
123+ privacyIDEA .log ("Forwarding client IP: " + piConfig .forwardClientIP );
124+ params .put (CLIENT_IP , piConfig .forwardClientIP );
125+ }
120126 privacyIDEA .log (method + " " + endpoint );
121127 params .forEach ((k , v ) ->
122- {
128+ {
123129 if (k .equals ("pass" ) || k .equals ("password" ))
124130 {
125131 v = "*" .repeat (v .length ());
126132 }
127-
128133 privacyIDEA .log (k + "=" + v );
129- });
134+ });
130135
131136 if (GET .equals (method ))
132137 {
133138 params .forEach ((key , value ) ->
134- {
139+ {
135140 String encValue = URLEncoder .encode (value , StandardCharsets .UTF_8 );
136141 urlBuilder .addQueryParameter (key , encValue );
137- });
142+ });
138143 }
139144
140145 String url = urlBuilder .build ().toString ();
141146 //privacyIDEA.log("URL: " + url);
142147 Request .Builder requestBuilder = new Request .Builder ().url (url );
143148
144149 // Add the headers
145- requestBuilder .addHeader (HEADER_USER_AGENT , piconfig .userAgent );
150+ requestBuilder .addHeader (HEADER_USER_AGENT , piConfig .userAgent );
146151 if (headers != null && !headers .isEmpty ())
147152 {
148153 headers .forEach (requestBuilder ::addHeader );
@@ -152,7 +157,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
152157 {
153158 FormBody .Builder formBodyBuilder = new FormBody .Builder ();
154159 params .forEach ((key , value ) ->
155- {
160+ {
156161 if (key != null && value != null )
157162 {
158163 String encValue = value ;
@@ -164,7 +169,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
164169 }
165170 formBodyBuilder .add (key , encValue );
166171 }
167- });
172+ });
168173 // This switches okhttp to make a post request
169174 requestBuilder .post (formBodyBuilder .build ());
170175 }
0 commit comments