Skip to content

Commit 1a029d9

Browse files
formatting (#54)
* formatting * chained calls chop down if too long * chained calls chop down if too long * Formatting * Create Utils.java * Update the tests
1 parent 8c80314 commit 1a029d9

17 files changed

+505
-651
lines changed

src/main/java/org/privacyidea/AsyncRequestCallable.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public class AsyncRequestCallable implements Callable<String>, Callback
4343
final String[] callbackResult = {null};
4444
private CountDownLatch latch;
4545

46-
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params,
47-
Map<String, String> headers, boolean authTokenRequired, String method)
46+
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params, Map<String, String> headers, boolean authTokenRequired, String method)
4847
{
4948
this.privacyIDEA = privacyIDEA;
5049
this.endpoint = endpoint;
@@ -69,8 +68,7 @@ public String call() throws Exception
6968
latch = new CountDownLatch(1);
7069
String tmpPath = path;
7170
path = ENDPOINT_AUTH;
72-
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(),
73-
PIConstants.POST, this);
71+
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(), PIConstants.POST, this);
7472
latch.await();
7573
// Extract the auth token from the response
7674
String response = callbackResult[0];
@@ -105,10 +103,8 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
105103
{
106104
if (response.body() != null)
107105
{
108-
String s = response.body()
109-
.string();
110-
if (!privacyIDEA.logExcludedEndpoints()
111-
.contains(path) && !ENDPOINT_AUTH.equals(path))
106+
String s = response.body().string();
107+
if (!privacyIDEA.logExcludedEndpoints().contains(path) && !ENDPOINT_AUTH.equals(path))
112108
{
113109
privacyIDEA.log(path + ":\n" + privacyIDEA.parser.formatJson(s));
114110
}

src/main/java/org/privacyidea/Endpoint.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
108108
{
109109
privacyIDEA.error("Server url could not be parsed: " + (piconfig.serverURL + endpoint));
110110
// Invoke the callback to terminate the thread that called this method.
111-
callback.onFailure(null,
112-
new IOException("Request could not be created because the url could not be parsed"));
111+
callback.onFailure(null, new IOException("Request could not be created because the url could not be parsed"));
113112
return;
114113
}
115114
HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
@@ -145,8 +144,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
145144
});
146145
}
147146

148-
String url = urlBuilder.build()
149-
.toString();
147+
String url = urlBuilder.build().toString();
150148
//privacyIDEA.log("URL: " + url);
151149
Request.Builder requestBuilder = new Request.Builder().url(url);
152150

@@ -187,7 +185,6 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
187185

188186
Request request = requestBuilder.build();
189187
//privacyIDEA.log("HEADERS:\n" + request.headers().toString());
190-
client.newCall(request)
191-
.enqueue(callback);
188+
client.newCall(request).enqueue(callback);
192189
}
193190
}

src/main/java/org/privacyidea/JSONParser.java

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171
public class JSONParser
7272
{
73-
7473
private final PrivacyIDEA privacyIDEA;
7574

7675
public JSONParser(PrivacyIDEA privacyIDEA)
@@ -92,13 +91,10 @@ public String formatJson(String json)
9291
}
9392

9493
JsonObject obj;
95-
Gson gson = new GsonBuilder().setPrettyPrinting()
96-
.setLenient()
97-
.create();
94+
Gson gson = new GsonBuilder().setPrettyPrinting().setLenient().create();
9895
try
9996
{
100-
obj = JsonParser.parseString(json)
101-
.getAsJsonObject();
97+
obj = JsonParser.parseString(json).getAsJsonObject();
10298
}
10399
catch (JsonSyntaxException e)
104100
{
@@ -125,10 +121,7 @@ String extractAuthToken(String serverResponse)
125121
try
126122
{
127123
JsonObject obj = root.getAsJsonObject();
128-
return obj.getAsJsonObject(RESULT)
129-
.getAsJsonObject(VALUE)
130-
.getAsJsonPrimitive(TOKEN)
131-
.getAsString();
124+
return obj.getAsJsonObject(RESULT).getAsJsonObject(VALUE).getAsJsonPrimitive(TOKEN).getAsString();
132125
}
133126
catch (Exception e)
134127
{
@@ -162,8 +155,7 @@ public PIResponse parsePIResponse(String serverResponse)
162155
JsonObject obj;
163156
try
164157
{
165-
obj = JsonParser.parseString(serverResponse)
166-
.getAsJsonObject();
158+
obj = JsonParser.parseString(serverResponse).getAsJsonObject();
167159
}
168160
catch (JsonSyntaxException e)
169161
{
@@ -182,8 +174,7 @@ public PIResponse parsePIResponse(String serverResponse)
182174
String r = getString(result, AUTHENTICATION);
183175
for (AuthenticationStatus en : AuthenticationStatus.values())
184176
{
185-
if (en.toString()
186-
.equals(r))
177+
if (en.toString().equals(r))
187178
{
188179
response.authentication = en;
189180
}
@@ -243,8 +234,7 @@ else if ("interactive".equals(modeFromResponse))
243234
{
244235
for (int i = 0; i < arrChallenges.size(); i++)
245236
{
246-
JsonObject challenge = arrChallenges.get(i)
247-
.getAsJsonObject();
237+
JsonObject challenge = arrChallenges.get(i).getAsJsonObject();
248238
String serial = getString(challenge, SERIAL);
249239
String message = getString(challenge, MESSAGE);
250240
String clientmode = getString(challenge, CLIENT_MODE);
@@ -255,19 +245,16 @@ else if ("interactive".equals(modeFromResponse))
255245
if (TOKEN_TYPE_WEBAUTHN.equals(type))
256246
{
257247
String webAuthnSignRequest = getItemFromAttributes(WEBAUTHN_SIGN_REQUEST, challenge);
258-
response.multichallenge.add(
259-
new WebAuthn(serial, message, clientmode, image, transactionid, webAuthnSignRequest));
248+
response.multichallenge.add(new WebAuthn(serial, message, clientmode, image, transactionid, webAuthnSignRequest));
260249
}
261250
else if (TOKEN_TYPE_U2F.equals(type))
262251
{
263252
String u2fSignRequest = getItemFromAttributes(U2F_SIGN_REQUEST, challenge);
264-
response.multichallenge.add(
265-
new U2F(serial, message, clientmode, image, transactionid, u2fSignRequest));
253+
response.multichallenge.add(new U2F(serial, message, clientmode, image, transactionid, u2fSignRequest));
266254
}
267255
else
268256
{
269-
response.multichallenge.add(
270-
new Challenge(serial, message, clientmode, image, transactionid, type));
257+
response.multichallenge.add(new Challenge(serial, message, clientmode, image, transactionid, type));
271258
}
272259
}
273260
}
@@ -280,13 +267,11 @@ static String mergeWebAuthnSignRequest(WebAuthn webAuthn, List<String> arr) thro
280267
List<JsonArray> extracted = new ArrayList<>();
281268
for (String signRequest : arr)
282269
{
283-
JsonObject obj = JsonParser.parseString(signRequest)
284-
.getAsJsonObject();
270+
JsonObject obj = JsonParser.parseString(signRequest).getAsJsonObject();
285271
extracted.add(obj.getAsJsonArray("allowCredentials"));
286272
}
287273

288-
JsonObject signRequest = JsonParser.parseString(webAuthn.signRequest())
289-
.getAsJsonObject();
274+
JsonObject signRequest = JsonParser.parseString(webAuthn.signRequest()).getAsJsonObject();
290275
JsonArray allowCredentials = new JsonArray();
291276
extracted.forEach(allowCredentials::addAll);
292277

@@ -301,8 +286,7 @@ private String getItemFromAttributes(String item, JsonObject jsonObject)
301286
JsonElement attributeElement = jsonObject.get(ATTRIBUTES);
302287
if (attributeElement != null && !attributeElement.isJsonNull())
303288
{
304-
JsonElement requestElement = attributeElement.getAsJsonObject()
305-
.get(item);
289+
JsonElement requestElement = attributeElement.getAsJsonObject().get(item);
306290
if (requestElement != null && !requestElement.isJsonNull())
307291
{
308292
ret = requestElement.toString();
@@ -328,8 +312,7 @@ List<TokenInfo> parseTokenInfoList(String serverResponse)
328312
JsonObject object;
329313
try
330314
{
331-
object = JsonParser.parseString(serverResponse)
332-
.getAsJsonObject();
315+
object = JsonParser.parseString(serverResponse).getAsJsonObject();
333316
}
334317
catch (JsonSyntaxException e)
335318
{
@@ -375,8 +358,7 @@ private TokenInfo parseSingleTokenInfo(String json)
375358
JsonObject obj;
376359
try
377360
{
378-
obj = JsonParser.parseString(json)
379-
.getAsJsonObject();
361+
obj = JsonParser.parseString(json).getAsJsonObject();
380362
}
381363
catch (JsonSyntaxException e)
382364
{
@@ -408,15 +390,13 @@ private TokenInfo parseSingleTokenInfo(String json)
408390
JsonObject joInfo = obj.getAsJsonObject(INFO);
409391
if (joInfo != null)
410392
{
411-
joInfo.entrySet()
412-
.forEach(entry ->
413-
{
414-
if (entry.getKey() != null && entry.getValue() != null)
415-
{
416-
info.info.put(entry.getKey(), entry.getValue()
417-
.getAsString());
418-
}
419-
});
393+
joInfo.entrySet().forEach(entry ->
394+
{
395+
if (entry.getKey() != null && entry.getValue() != null)
396+
{
397+
info.info.put(entry.getKey(), entry.getValue().getAsString());
398+
}
399+
});
420400
}
421401

422402
JsonArray arrRealms = obj.getAsJsonArray(REALMS);
@@ -455,8 +435,7 @@ RolloutInfo parseRolloutInfo(String serverResponse)
455435
JsonObject obj;
456436
try
457437
{
458-
obj = JsonParser.parseString(serverResponse)
459-
.getAsJsonObject();
438+
obj = JsonParser.parseString(serverResponse).getAsJsonObject();
460439

461440
JsonObject result = obj.getAsJsonObject(RESULT);
462441
JsonElement errElem = result.get(ERROR);
@@ -521,8 +500,7 @@ Map<String, String> parseWebAuthnSignResponse(String json)
521500
JsonObject obj;
522501
try
523502
{
524-
obj = JsonParser.parseString(json)
525-
.getAsJsonObject();
503+
obj = JsonParser.parseString(json).getAsJsonObject();
526504
}
527505
catch (JsonSyntaxException e)
528506
{
@@ -562,8 +540,7 @@ Map<String, String> parseU2FSignResponse(String json)
562540
JsonObject obj;
563541
try
564542
{
565-
obj = JsonParser.parseString(json)
566-
.getAsJsonObject();
543+
obj = JsonParser.parseString(json).getAsJsonObject();
567544
}
568545
catch (JsonSyntaxException e)
569546
{

src/main/java/org/privacyidea/PIConstants.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ private PIConstants()
104104

105105

106106
// These will be excluded from url encoding
107-
public static final List<String> WEBAUTHN_PARAMETERS = Arrays.asList(CREDENTIALID, CLIENTDATA, SIGNATUREDATA,
108-
AUTHENTICATORDATA, USERHANDLE,
107+
public static final List<String> WEBAUTHN_PARAMETERS = Arrays.asList(CREDENTIALID, CLIENTDATA, SIGNATUREDATA, AUTHENTICATORDATA, USERHANDLE,
109108
ASSERTIONCLIENTEXTENSIONS);
110109
public static final List<String> U2F_PARAMETERS = Arrays.asList(CLIENTDATA, SIGNATUREDATA);
111110

src/main/java/org/privacyidea/PIResponse.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ public String otpMessage()
8181
private String reduceChallengeMessagesWhere(Predicate<Challenge> predicate)
8282
{
8383
StringBuilder sb = new StringBuilder();
84-
sb.append(multichallenge.stream()
85-
.filter(predicate)
86-
.map(Challenge::getMessage)
87-
.distinct()
88-
.reduce("", (a, s) -> a + s + ", ")
89-
.trim());
84+
sb.append(multichallenge.stream().filter(predicate).map(Challenge::getMessage).distinct().reduce("", (a, s) -> a + s + ", ").trim());
9085

9186
if (sb.length() > 0)
9287
{
@@ -101,10 +96,7 @@ private String reduceChallengeMessagesWhere(Predicate<Challenge> predicate)
10196
*/
10297
public List<String> triggeredTokenTypes()
10398
{
104-
return multichallenge.stream()
105-
.map(Challenge::getType)
106-
.distinct()
107-
.collect(Collectors.toList());
99+
return multichallenge.stream().map(Challenge::getType).distinct().collect(Collectors.toList());
108100
}
109101

110102
/**
@@ -115,16 +107,13 @@ public List<String> triggeredTokenTypes()
115107
public List<WebAuthn> webAuthnSignRequests()
116108
{
117109
List<WebAuthn> ret = new ArrayList<>();
118-
multichallenge.stream()
119-
.filter(c -> TOKEN_TYPE_WEBAUTHN.equals(c.getType()))
120-
.collect(Collectors.toList())
121-
.forEach(c ->
122-
{
123-
if (c instanceof WebAuthn)
124-
{
125-
ret.add((WebAuthn) c);
126-
}
127-
});
110+
multichallenge.stream().filter(c -> TOKEN_TYPE_WEBAUTHN.equals(c.getType())).collect(Collectors.toList()).forEach(c ->
111+
{
112+
if (c instanceof WebAuthn)
113+
{
114+
ret.add((WebAuthn) c);
115+
}
116+
});
128117
return ret;
129118
}
130119

@@ -149,9 +138,7 @@ public String mergedSignRequest()
149138
}
150139

151140
WebAuthn webAuthn = webAuthnSignRequests.get(0);
152-
List<String> stringSignRequests = webAuthnSignRequests.stream()
153-
.map(WebAuthn::signRequest)
154-
.collect(Collectors.toList());
141+
List<String> stringSignRequests = webAuthnSignRequests.stream().map(WebAuthn::signRequest).collect(Collectors.toList());
155142

156143
try
157144
{
@@ -162,7 +149,7 @@ public String mergedSignRequest()
162149
return "";
163150
}
164151
}
165-
152+
166153
/**
167154
* Get all U2F challenges from the multi_challenge.
168155
*
@@ -171,16 +158,13 @@ public String mergedSignRequest()
171158
public List<U2F> u2fSignRequests()
172159
{
173160
List<U2F> ret = new ArrayList<>();
174-
multichallenge.stream()
175-
.filter(c -> TOKEN_TYPE_U2F.equals(c.getType()))
176-
.collect(Collectors.toList())
177-
.forEach(c ->
178-
{
179-
if (c instanceof U2F)
180-
{
181-
ret.add((U2F) c);
182-
}
183-
});
161+
multichallenge.stream().filter(c -> TOKEN_TYPE_U2F.equals(c.getType())).collect(Collectors.toList()).forEach(c ->
162+
{
163+
if (c instanceof U2F)
164+
{
165+
ret.add((U2F) c);
166+
}
167+
});
184168
return ret;
185169
}
186170

0 commit comments

Comments
 (0)