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

69 reuse saved jwt auth token until it expire 1 #70

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b95f47d
Update JSONParser.java
lukasmatusiewicz Dec 17, 2024
6f5e7ef
Update JSONParser.java
lukasmatusiewicz Dec 18, 2024
626a875
Update JSONParser.java
lukasmatusiewicz Dec 18, 2024
12ab544
Update PIConstants.java
lukasmatusiewicz Dec 18, 2024
2a90a4e
Update JSONParser.java
lukasmatusiewicz Dec 18, 2024
d581b23
Update JSONParser.java
lukasmatusiewicz Dec 18, 2024
872db86
Update PIConstants.java
lukasmatusiewicz Dec 18, 2024
0711b6e
Update PIResponse.java
lukasmatusiewicz Dec 18, 2024
098706d
Update PrivacyIDEA.java
lukasmatusiewicz Dec 18, 2024
072bf36
Update TestGetTokenInfo.java
lukasmatusiewicz Dec 19, 2024
c452b71
Update AsyncRequestCallable.java
lukasmatusiewicz Dec 19, 2024
26a3e90
Update AsyncRequestCallable.java
lukasmatusiewicz Dec 23, 2024
70bef18
Update PrivacyIDEA.java
lukasmatusiewicz Dec 23, 2024
f3bd138
Update PrivacyIDEA.java
lukasmatusiewicz Dec 31, 2024
f3736d0
Update JSONParser.java
lukasmatusiewicz Dec 31, 2024
01eb016
Update PrivacyIDEA.java
lukasmatusiewicz Dec 31, 2024
1e23c86
Update PrivacyIDEA.java
lukasmatusiewicz Dec 31, 2024
a6d491b
Update PrivacyIDEA.java
lukasmatusiewicz Jan 28, 2025
c09b902
update tests
lukasmatusiewicz Jan 28, 2025
faf235b
Update PrivacyIDEA.java
lukasmatusiewicz Feb 10, 2025
e6eb252
Update build.yml
lukasmatusiewicz Feb 10, 2025
9f77a94
Update PrivacyIDEA.java
lukasmatusiewicz Feb 10, 2025
3c5614f
Update JSONParser.java
lukasmatusiewicz Feb 12, 2025
81b52af
Update PrivacyIDEA.java
lukasmatusiewicz Feb 12, 2025
44cf2f8
Update pom.xml
lukasmatusiewicz Feb 12, 2025
7d3554f
Create TestJWTAuthToken.java
lukasmatusiewicz Feb 12, 2025
a94cad4
Update TestJWTAuthToken.java
lukasmatusiewicz Feb 12, 2025
d75e8cb
Update TestJWTAuthToken.java
lukasmatusiewicz Feb 12, 2025
479b918
Update TestValidateCheckSerial.java
lukasmatusiewicz Feb 12, 2025
b43e4f8
Update TestRollout.java
lukasmatusiewicz Feb 12, 2025
2629d18
update test, change naming
nilsbehlen Feb 13, 2025
e55ee8b
Update TestJWT.java
nilsbehlen Feb 13, 2025
5ba9e3e
Update TestJWT.java
lukasmatusiewicz Feb 13, 2025
f5e43c5
Update TestJWT.java
nilsbehlen Feb 13, 2025
437700b
schedule again when response is empty
nilsbehlen Feb 17, 2025
44a5fae
Merge branch 'master' into 69-reuse-saved-jwt-auth-token-until-it-exp…
lukasmatusiewicz Mar 26, 2025
3d50f1f
cleanup
lukasmatusiewicz Mar 26, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Java Development Kits
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: 17
distribution: microsoft
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,10 @@
<version>2.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
</dependencies>
</project>
31 changes: 3 additions & 28 deletions src/main/java/org/privacyidea/AsyncRequestCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand All @@ -35,7 +34,7 @@
*/
public class AsyncRequestCallable implements Callable<String>, Callback
{
private String path;
private final String path;
private final String method;
private final Map<String, String> headers;
private final Map<String, String> params;
Expand Down Expand Up @@ -63,32 +62,8 @@ 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)
{
if (!privacyIDEA.serviceAccountAvailable())
{
privacyIDEA.error("Service account is required to retrieve auth token!");
return null;
}
latch = new CountDownLatch(1);
String tmpPath = path;
path = ENDPOINT_AUTH;
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(), PIConstants.POST, this);
if (!latch.await(30, TimeUnit.SECONDS))
{
privacyIDEA.error("Latch timed out...");
return "";
}
// Extract the auth token from the response
String response = callbackResult[0];
String authToken = privacyIDEA.parser.extractAuthToken(response);
if (authToken == null)
{
// The parser already logs the error.
return null;
}
// Add the auth token to the header
headers.put(PIConstants.HEADER_AUTHORIZATION, authToken);
path = tmpPath;
callbackResult[0] = null;
// 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/privacyidea/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* This class handles sending requests to the server.
*/
class Endpoint
public class Endpoint
{
private final PrivacyIDEA privacyIDEA;
private final PIConfig piConfig;
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/org/privacyidea/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

import com.google.gson.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.privacyidea.PIConstants.*;

Expand Down Expand Up @@ -66,9 +63,9 @@ public String formatJson(String json)
* Extract the auth token from the response of the server.
*
* @param serverResponse response of the server
* @return the auth token or null if error
* @return the AuthToken obj or null if error
*/
String extractAuthToken(String serverResponse)
LinkedHashMap<String, String> extractAuthToken(String serverResponse)
{
if (serverResponse != null && !serverResponse.isEmpty())
{
Expand All @@ -78,11 +75,21 @@ String extractAuthToken(String serverResponse)
try
{
JsonObject obj = root.getAsJsonObject();
return obj.getAsJsonObject(RESULT).getAsJsonObject(VALUE).getAsJsonPrimitive(TOKEN).getAsString();
String authToken = obj.getAsJsonObject(RESULT).getAsJsonObject(VALUE).getAsJsonPrimitive(TOKEN).getAsString();
var parts = authToken.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));

return new LinkedHashMap<>(Map.of(AUTH_TOKEN, authToken, AUTH_TOKEN_EXP, String.valueOf(expDate)));
}
catch (Exception e)
{
privacyIDEA.error("Response did not contain an authorization token: " + formatJson(serverResponse));
privacyIDEA.error("Auth token extraction failed: " + e);
}
}
}
Expand Down Expand Up @@ -232,7 +239,7 @@ else if ("interactive".equals(modeFromResponse))

if (TOKEN_TYPE_WEBAUTHN.equals(type))
{
String webauthnSignRequest = getItemFromAttributes(WEBAUTHN_SIGN_REQUEST, challenge);
String webauthnSignRequest = getItemFromAttributes(challenge);
response.multiChallenge.add(new WebAuthn(serial, message, clientMode, image, transactionID, webauthnSignRequest));
}
else
Expand Down Expand Up @@ -263,13 +270,13 @@ static String mergeWebAuthnSignRequest(WebAuthn webauthn, List<String> arr) thro
return signRequest.toString();
}

private String getItemFromAttributes(String item, JsonObject jsonObject)
private String getItemFromAttributes(JsonObject jsonObject)
{
String ret = "";
JsonElement attributeElement = jsonObject.get(ATTRIBUTES);
if (attributeElement != null && !attributeElement.isJsonNull())
{
JsonElement requestElement = attributeElement.getAsJsonObject().get(item);
JsonElement requestElement = attributeElement.getAsJsonObject().get(PIConstants.WEBAUTHN_SIGN_REQUEST);
if (requestElement != null && !requestElement.isJsonNull())
{
ret = requestElement.toString();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/privacyidea/PIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public class PIConstants
public static final String PASSWORD = "password";
public static final String PASS = "pass";
public static final String SERIAL = "serial";
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 TYPE = "type";
public static final String TRANSACTION_ID = "transaction_id";
public static final String REALM = "realm";
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/privacyidea/PIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.privacyidea.PIConstants.*;
import static org.privacyidea.PIConstants.TOKEN_TYPE_PUSH;
import static org.privacyidea.PIConstants.TOKEN_TYPE_WEBAUTHN;

/**
* This class parses the JSON response of privacyIDEA into a POJO for easier access.
Expand Down
Loading
Loading