Skip to content
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
10 changes: 8 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Changelog

### v1.3.0 - 0 Apr 2025

* Passkey functions
* JWT will be reused and renewed automatically
* Added option to add arbitrary parameters to the requests

### v1.2.2 - 5 Mar 2024

* Fixed a problem with the threadpool where thread would not time out and accumulate over time
* Fixed a problem with the thread pool where thread would not time out and accumulate over time
* Added the option to set http timeouts

### v1.2.1 - 9 Aug 2023
Expand Down Expand Up @@ -42,4 +48,4 @@
### v0.1 - 18 Sep 2020

* First version
* Supports basic OTP token
* Supports basic OTP token
10 changes: 5 additions & 5 deletions 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.2</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -16,7 +16,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<version>3.5.3</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
Expand Down Expand Up @@ -50,8 +50,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>11</source>
<target>11</target>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -121,4 +121,4 @@
<version>4.4.0</version>
</dependency>
</dependencies>
</project>
</project>
14 changes: 2 additions & 12 deletions src/main/java/org/privacyidea/AsyncRequestCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,25 @@ public class AsyncRequestCallable implements Callable<String>, Callback
private final String method;
private final Map<String, String> headers;
private final Map<String, String> params;
private final boolean authTokenRequired;
private final Endpoint endpoint;
private final PrivacyIDEA privacyIDEA;
final String[] callbackResult = {null};
private CountDownLatch latch;

public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params,
Map<String, String> headers, boolean authTokenRequired, String method)
Map<String, String> headers, String method)
{
this.privacyIDEA = privacyIDEA;
this.endpoint = endpoint;
this.path = path;
this.params = params;
this.headers = headers;
this.authTokenRequired = authTokenRequired;
this.method = method;
}

@Override
public String call() throws Exception
{
// If an auth token is required for the request, get that first then do the actual request
if (this.authTokenRequired)
{
// Wait for the auth token to be retrieved and add it to the header
headers.put(PIConstants.HEADER_AUTHORIZATION, privacyIDEA.getJWT());
}

// Do the actual request
latch = new CountDownLatch(1);
endpoint.sendRequestAsync(path, params, headers, method, this);
if (!latch.await(30, TimeUnit.SECONDS))
Expand All @@ -90,7 +80,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
if (response.body() != null)
{
String s = response.body().string();
if (!privacyIDEA.logExcludedEndpoints().contains(path) && !ENDPOINT_AUTH.equals(path))
if (!privacyIDEA.logExcludedEndpoints().contains(path))
{
privacyIDEA.log(path + ":\n" + privacyIDEA.parser.formatJson(s));
}
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/privacyidea/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
return;
}
HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
if (!piConfig.forwardClientIP.isEmpty())
{
privacyIDEA.log("Forwarding client IP: " + piConfig.forwardClientIP);
params.put(CLIENT_IP, piConfig.forwardClientIP);
}
privacyIDEA.log(method + " " + endpoint);
params.forEach((k, v) ->
{
Expand Down Expand Up @@ -150,7 +145,17 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
requestBuilder.addHeader(HEADER_USER_AGENT, piConfig.userAgent);
if (headers != null && !headers.isEmpty())
{
headers.forEach(requestBuilder::addHeader);
headers.forEach((k, v) ->
{
if (v == null)
{
privacyIDEA.error("Unable to add header " + k + " because the value is null");
}
else
{
requestBuilder.addHeader(k, v);
}
});
}

if (POST.equals(method))
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/privacyidea/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public String formatJson(String json)
* @param serverResponse response of the server
* @return the AuthToken obj or null if error
*/
LinkedHashMap<String, String> extractAuthToken(String serverResponse)
LinkedHashMap<String, String> getJWT(String serverResponse)
{
if (serverResponse != null && !serverResponse.isEmpty())
{
Expand All @@ -75,21 +75,22 @@ LinkedHashMap<String, String> extractAuthToken(String serverResponse)
try
{
JsonObject obj = root.getAsJsonObject();
String authToken = obj.getAsJsonObject(RESULT).getAsJsonObject(VALUE).getAsJsonPrimitive(TOKEN).getAsString();
var parts = authToken.split("\\.");
String jwt = obj.getAsJsonObject(RESULT).getAsJsonObject(VALUE).getAsJsonPrimitive(TOKEN).getAsString();
var parts = jwt.split("\\.");
String dec = new String(Base64.getDecoder().decode(parts[1]));

// Extract the expiration date from the token
int respDate = obj.getAsJsonPrimitive(TIME).getAsInt();
int expDate = JsonParser.parseString(dec).getAsJsonObject().getAsJsonPrimitive(EXP).getAsInt();
int difference = expDate - respDate;
privacyIDEA.log("JWT Validity: " + difference / 60 + " minutes. Token expires at: " + new Date(expDate * 1000L));
int responseTime = obj.getAsJsonPrimitive(TIME).getAsInt();
int expirationTime = JsonParser.parseString(dec).getAsJsonObject().getAsJsonPrimitive(EXP).getAsInt();
int difference = expirationTime - responseTime;
privacyIDEA.log("JWT Validity: " + difference / 60 + " minutes. Token expires at: " + new Date(expirationTime * 1000L));

return new LinkedHashMap<>(Map.of(AUTH_TOKEN, authToken, AUTH_TOKEN_EXP, String.valueOf(expDate)));
return new LinkedHashMap<>(Map.of(JWT, jwt, JWT_EXPIRATION_TIME, String.valueOf(expirationTime)));
}
catch (Exception e)
{
privacyIDEA.error("Auth token extraction failed: " + e);
privacyIDEA.error("JWT token extraction failed: " + e);
privacyIDEA.error("Server response: " + serverResponse);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/privacyidea/PIConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class PIConfig
public String serviceAccountPass = "";
public String serviceAccountRealm = "";
public boolean disableLog = false;
public String forwardClientIP = "";
public int httpTimeoutMs = 30000;
protected String proxyHost = "";
protected int proxyPort = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/privacyidea/PIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class PIConstants
public static final String TIME = "time";
public static final String EXP = "exp";
public static final String CHALLENGE_STATUS = "challenge_status";
public static final String AUTH_TOKEN = "authToken";
public static final String AUTH_TOKEN_EXP = "authTokenExp";
public static final String JWT = "jwt";
public static final String JWT_EXPIRATION_TIME = "jwt_expiration_time";
public static final String TYPE = "type";
public static final String TRANSACTION_ID = "transaction_id";
public static final String REALM = "realm";
Expand Down
Loading