Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http timeouts, v1.2.2 #58

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.privacyidea</groupId>
<artifactId>privacyidea-java-client</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
33 changes: 9 additions & 24 deletions src/main/java/org/privacyidea/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers()
this.piconfig = privacyIDEA.configuration();

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS)
.writeTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS);

if (!this.piconfig.doSSLVerify)
{
// Trust all certs and verify every host
Expand Down Expand Up @@ -108,7 +112,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
if (httpUrl == null)
{
privacyIDEA.error("Server url could not be parsed: " + (piconfig.serverURL + endpoint));
// Invoke the callback to terminate the thread that called this method.
// Invoke the callback to terminate the thread that called this function.
callback.onFailure(null, new IOException("Request could not be created because the url could not be parsed"));
return;
}
Expand All @@ -118,12 +122,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
{
if (k.equals("pass") || k.equals("password"))
{
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < v.length(); i++)
{
tmp.append("*");
}
v = tmp.toString();
v = "*".repeat(v.length());
}

privacyIDEA.log(k + "=" + v);
Expand All @@ -133,15 +132,8 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
{
params.forEach((key, value) ->
{
try
{
String encValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
urlBuilder.addQueryParameter(key, encValue);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
String encValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
urlBuilder.addQueryParameter(key, encValue);
lukasmatusiewicz marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand All @@ -168,14 +160,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
// they are already in the correct encoding for the server
if (!WEBAUTHN_PARAMETERS.contains(key))
{
try
{
encValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
}
catch (UnsupportedEncodingException e)
{
privacyIDEA.error(e);
}
encValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
lukasmatusiewicz marked this conversation as resolved.
Show resolved Hide resolved
}
formBodyBuilder.add(key, encValue);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/privacyidea/PIConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PIConfig
String serviceAccountRealm = "";
boolean disableLog = false;
String userAgent;
int httpTimeoutMs = 30000;

public PIConfig(String serverURL, String userAgent)
{
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/privacyidea/PrivacyIDEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ public static class Builder
private IPILogger logger = null;
private boolean disableLog = false;
private IPISimpleLogger simpleLogBridge = null;
private int httpTimeoutMs = 30000;

/**
* @param serverURL the server URL is mandatory to communicate with privacyIDEA.
Expand Down Expand Up @@ -666,6 +667,17 @@ public Builder disableLog()
return this;
}

/**
* Set the timeout for http requests in milliseconds.
* @param httpTimeoutMs timeout in milliseconds
* @return Builder
*/
public Builder httpTimeoutMs(int httpTimeoutMs)
{
this.httpTimeoutMs = httpTimeoutMs;
return this;
}

public PrivacyIDEA build()
{
PIConfig configuration = new PIConfig(serverURL, userAgent);
Expand All @@ -675,6 +687,7 @@ public PrivacyIDEA build()
configuration.serviceAccountPass = serviceAccountPass;
configuration.serviceAccountRealm = serviceAccountRealm;
configuration.disableLog = disableLog;
configuration.httpTimeoutMs = httpTimeoutMs;
return new PrivacyIDEA(configuration, logger, simpleLogBridge);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/privacyidea/TestGetTokenInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void setup()
.serviceAccount(serviceAccount, servicePassword)
.serviceRealm(serviceRealm)
.disableLog()
.httpTimeoutMs(15000)
.sslVerify(false)
.logger(new PILogImplementation())
.build();
Expand Down
Loading