2727import java .nio .charset .StandardCharsets ;
2828import java .security .KeyManagementException ;
2929import java .security .NoSuchAlgorithmException ;
30+ import java .util .Arrays ;
3031import java .util .Collections ;
3132import java .util .LinkedHashMap ;
3233import java .util .List ;
6061class Endpoint {
6162
6263 private final PrivacyIDEA privacyIDEA ;
63- private List <String > logExcludedEndpointPrints = Collections . emptyList (); // Arrays.asList(PIConstants.ENDPOINT_AUTH, PIConstants.ENDPOINT_POLLTRANSACTION); //
64+ private List <String > logExcludedEndpointPrints = Arrays .asList (PIConstants .ENDPOINT_AUTH , PIConstants .ENDPOINT_POLLTRANSACTION ); //Collections.emptyList( ); //
6465 private final PIConfig piconfig ;
6566 private final OkHttpClient client ;
6667
@@ -117,9 +118,9 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
117118 params .forEach ((key , value ) -> {
118119 //privacyIDEA.log("" + key + "=" + value);
119120 try {
120- String enc_value = value ;
121- enc_value = URLEncoder .encode (value , StandardCharsets .UTF_8 .toString ());
122- urlBuilder .addQueryParameter (key , enc_value );
121+ String encValue = value ;
122+ encValue = URLEncoder .encode (value , StandardCharsets .UTF_8 .toString ());
123+ urlBuilder .addQueryParameter (key , encValue );
123124 } catch (UnsupportedEncodingException e ) {
124125 e .printStackTrace ();
125126 }
@@ -150,17 +151,17 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
150151 FormBody .Builder formBodyBuilder = new FormBody .Builder ();
151152 params .forEach ((key , value ) -> {
152153 if (key != null && value != null ) {
153- String enc_value = value ;
154+ String encValue = value ;
154155 // WebAuthn params are excluded from url encoded, they are already in the correct format for the server
155156 if (!WEBAUTHN_PARAMETERS .contains (key )) {
156157 try {
157- enc_value = URLEncoder .encode (value , StandardCharsets .UTF_8 .toString ());
158+ encValue = URLEncoder .encode (value , StandardCharsets .UTF_8 .toString ());
158159 } catch (UnsupportedEncodingException e ) {
159160 privacyIDEA .error (e );
160161 }
161162 }
162- //privacyIDEA.log("" + key + "=" + enc_value );
163- formBodyBuilder .add (key , enc_value );
163+ //privacyIDEA.log("" + key + "=" + encValue );
164+ formBodyBuilder .add (key , encValue );
164165 }
165166 });
166167 // This switches okhttp to make a post request
@@ -174,7 +175,9 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
174175 Response response = client .newCall (request ).execute ();
175176 if (response .body () != null ) {
176177 String ret = response .body ().string ();
177- endpointLog (endpoint , ret );
178+ if (!logExcludedEndpointPrints .contains (endpoint )) {
179+ privacyIDEA .log (prettyFormatJson (ret ));
180+ }
178181 return ret ;
179182 } else {
180183 privacyIDEA .log ("Response body is null." );
@@ -188,7 +191,7 @@ String sendRequest(String endpoint, Map<String, String> params, Map<String, Stri
188191
189192 String getAuthTokenFromServer () {
190193 if (!privacyIDEA .checkServiceAccountAvailable ()) {
191- privacyIDEA .log ( "Service account information not set, cannot retrieve auth token " );
194+ privacyIDEA .error ( "Cannot retrieve auth token from server without service account! " );
192195 return "" ;
193196 }
194197
@@ -206,11 +209,11 @@ String getAuthTokenFromServer() {
206209 if (response != null && !response .isEmpty ()) {
207210 JsonElement root = JsonParser .parseString (response );
208211 if (root != null ) {
209- JsonObject obj = root . getAsJsonObject ();
210- if ( obj != null ) {
212+ try {
213+ JsonObject obj = root . getAsJsonObject ();
211214 return obj .getAsJsonObject (RESULT ).getAsJsonObject (VALUE ).getAsJsonPrimitive (TOKEN ).getAsString ();
212- } else {
213- privacyIDEA .log ("Response did not contain an authorization token: " + prettyFormatJson (response ));
215+ } catch ( Exception e ) {
216+ privacyIDEA .error ("Response did not contain an authorization token: " + prettyFormatJson (response ));
214217 }
215218 }
216219 } else {
@@ -235,12 +238,6 @@ public static String prettyFormatJson(String json) {
235238 return gson .toJson (obj );
236239 }
237240
238- private void endpointLog (String endpoint , String response ) {
239- if (!logExcludedEndpointPrints .contains (endpoint )) {
240- privacyIDEA .log (prettyFormatJson (response ));
241- }
242- }
243-
244241 public List <String > getLogExcludedEndpoints () {
245242 return logExcludedEndpointPrints ;
246243 }
0 commit comments