1515 */
1616package org .privacyidea ;
1717
18- import com .google .gson .Gson ;
19- import com .google .gson .GsonBuilder ;
20- import com .google .gson .JsonElement ;
21- import com .google .gson .JsonObject ;
22- import com .google .gson .JsonParser ;
23- import com .google .gson .JsonSyntaxException ;
2418import java .io .IOException ;
2519import java .io .UnsupportedEncodingException ;
2620import java .net .URLEncoder ;
2721import java .nio .charset .StandardCharsets ;
2822import java .security .KeyManagementException ;
2923import java .security .NoSuchAlgorithmException ;
30- import java .util .Arrays ;
31- import java .util .Collections ;
32- import java .util .LinkedHashMap ;
33- import java .util .List ;
3424import java .util .Map ;
3525import javax .net .ssl .SSLContext ;
3626import javax .net .ssl .SSLSocketFactory ;
3727import javax .net .ssl .TrustManager ;
3828import javax .net .ssl .X509TrustManager ;
29+ import okhttp3 .Callback ;
3930import okhttp3 .FormBody ;
4031import okhttp3 .HttpUrl ;
4132import okhttp3 .OkHttpClient ;
4233import okhttp3 .Request ;
43- import okhttp3 .Response ;
4434
45- import static org .privacyidea .PIConstants .ENDPOINT_AUTH ;
4635import static org .privacyidea .PIConstants .GET ;
47- import static org .privacyidea .PIConstants .HEADER_AUTHORIZATION ;
4836import static org .privacyidea .PIConstants .HEADER_USER_AGENT ;
49- import static org .privacyidea .PIConstants .PASSWORD ;
5037import static org .privacyidea .PIConstants .POST ;
51- import static org .privacyidea .PIConstants .REALM ;
52- import static org .privacyidea .PIConstants .RESULT ;
53- import static org .privacyidea .PIConstants .TOKEN ;
54- import static org .privacyidea .PIConstants .USERNAME ;
55- import static org .privacyidea .PIConstants .VALUE ;
5638import static org .privacyidea .PIConstants .WEBAUTHN_PARAMETERS ;
5739
5840/**
59- * This class handles sending sending requests to the server.
41+ * This class handles sending requests to the server.
6042 */
6143class Endpoint {
6244
6345 private final PrivacyIDEA privacyIDEA ;
64- private List <String > logExcludedEndpointPrints = Arrays .asList (PIConstants .ENDPOINT_AUTH , PIConstants .ENDPOINT_POLLTRANSACTION ); //Collections.emptyList(); //
6546 private final PIConfig piconfig ;
6647 private final OkHttpClient client ;
6748
@@ -84,7 +65,7 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
8465
8566 Endpoint (PrivacyIDEA privacyIDEA ) {
8667 this .privacyIDEA = privacyIDEA ;
87- this .piconfig = privacyIDEA .getConfiguration ();
68+ this .piconfig = privacyIDEA .configuration ();
8869
8970 OkHttpClient .Builder builder = new OkHttpClient .Builder ();
9071 if (!this .piconfig .doSSLVerify ) {
@@ -102,21 +83,28 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
10283 this .client = builder .build ();
10384 }
10485
105- String sendRequest (String endpoint , Map <String , String > params , boolean authTokenRequired , String method ) {
106- return sendRequest (endpoint , params , Collections .emptyMap (), authTokenRequired , method );
107- }
108-
109- String sendRequest (String endpoint , Map <String , String > params , Map <String , String > headers , boolean authTokenRequired , String method ) {
86+ /**
87+ * Add a request to the okhttp queue. The callback will be invoked upon success or failure.
88+ *
89+ * @param endpoint server endpoint
90+ * @param params request parameters
91+ * @param headers request headers
92+ * @param method http request method
93+ * @param callback okhttp3 callback
94+ */
95+ void sendRequestAsync (String endpoint , Map <String , String > params , Map <String , String > headers , String method , Callback callback ) {
11096 HttpUrl httpUrl = HttpUrl .parse (piconfig .serverURL + endpoint );
11197 if (httpUrl == null ) {
11298 privacyIDEA .error ("Server url could not be parsed: " + (piconfig .serverURL + endpoint ));
113- return null ;
99+ // Invoke the callback to terminate the thread that called this method.
100+ callback .onFailure (null , new IOException ("Request could not be created!" ));
101+ return ;
114102 }
115103 HttpUrl .Builder urlBuilder = httpUrl .newBuilder ();
116104
117105 if (GET .equals (method )) {
118106 params .forEach ((key , value ) -> {
119- //privacyIDEA.log("" + key + "=" + value);
107+ //privacyIDEA.log(key + "=" + value);
120108 try {
121109 String encValue = value ;
122110 encValue = URLEncoder .encode (value , StandardCharsets .UTF_8 .toString ());
@@ -128,19 +116,10 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
128116 }
129117
130118 String url = urlBuilder .build ().toString ();
131- //privacyIDEA.log("using URL: " + url);
119+ //privacyIDEA.log("URL: " + url);
132120 Request .Builder requestBuilder = new Request .Builder ()
133121 .url (url );
134122
135- if (authTokenRequired ) {
136- String authToken = getAuthTokenFromServer ();
137- if (authToken .isEmpty ()) {
138- privacyIDEA .error ("Failed to fetch authorization token from server!" );
139- return "" ;
140- }
141- requestBuilder .addHeader (HEADER_AUTHORIZATION , authToken );
142- }
143-
144123 // Add the headers
145124 requestBuilder .addHeader (HEADER_USER_AGENT , piconfig .userAgent );
146125 if (headers != null && !headers .isEmpty ()) {
@@ -160,7 +139,7 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
160139 privacyIDEA .error (e );
161140 }
162141 }
163- //privacyIDEA.log("" + key + "=" + encValue);
142+ //privacyIDEA.log(key + "=" + encValue);
164143 formBodyBuilder .add (key , encValue );
165144 }
166145 });
@@ -170,79 +149,6 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
170149
171150 Request request = requestBuilder .build ();
172151 //privacyIDEA.log("HEADERS:\n" + request.headers().toString());
173-
174- try {
175- Response response = client .newCall (request ).execute ();
176- if (response .body () != null ) {
177- String ret = response .body ().string ();
178- if (!logExcludedEndpointPrints .contains (endpoint )) {
179- privacyIDEA .log (prettyFormatJson (ret ));
180- }
181- return ret ;
182- } else {
183- privacyIDEA .log ("Response body is null." );
184- }
185- } catch (IOException e ) {
186- privacyIDEA .error (e );
187- }
188-
189- return "" ;
190- }
191-
192- String getAuthTokenFromServer () {
193- if (!privacyIDEA .checkServiceAccountAvailable ()) {
194- privacyIDEA .error ("Cannot retrieve auth token from server without service account!" );
195- return "" ;
196- }
197-
198- Map <String , String > params = new LinkedHashMap <>();
199- params .put (USERNAME , piconfig .serviceAccountName );
200- params .put (PASSWORD , piconfig .serviceAccountPass );
201-
202- if (piconfig .serviceAccountRealm != null && !piconfig .serviceAccountRealm .isEmpty ()) {
203- params .put (REALM , piconfig .serviceAccountRealm );
204- } else if (piconfig .realm != null && !piconfig .realm .isEmpty ()) {
205- params .put (REALM , piconfig .realm );
206- }
207-
208- String response = sendRequest (ENDPOINT_AUTH , params , false , POST );
209- if (response != null && !response .isEmpty ()) {
210- JsonElement root = JsonParser .parseString (response );
211- if (root != null ) {
212- try {
213- JsonObject obj = root .getAsJsonObject ();
214- return obj .getAsJsonObject (RESULT ).getAsJsonObject (VALUE ).getAsJsonPrimitive (TOKEN ).getAsString ();
215- } catch (Exception e ) {
216- privacyIDEA .error ("Response did not contain an authorization token: " + prettyFormatJson (response ));
217- }
218- }
219- } else {
220- privacyIDEA .error ("/auth response was empty or null!" );
221- }
222-
223- return "" ;
224- }
225-
226- public static String prettyFormatJson (String json ) {
227- if (json == null || json .isEmpty ()) return "" ;
228-
229- JsonObject obj ;
230- Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
231- try {
232- obj = JsonParser .parseString (json ).getAsJsonObject ();
233- } catch (JsonSyntaxException e ) {
234- e .printStackTrace ();
235- return json ;
236- }
237-
238- return gson .toJson (obj );
239- }
240-
241- public List <String > getLogExcludedEndpoints () {
242- return logExcludedEndpointPrints ;
243- }
244-
245- public void setLogExcludedEndpoints (List <String > list ) {
246- logExcludedEndpointPrints = list ;
152+ client .newCall (request ).enqueue (callback );
247153 }
248154}
0 commit comments