Skip to content

Commit 81b52af

Browse files
Update PrivacyIDEA.java
1 parent 3c5614f commit 81b52af

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

src/main/java/org/privacyidea/PrivacyIDEA.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public class PrivacyIDEA implements Closeable
3333
private final IPILogger log;
3434
private final IPISimpleLogger simpleLog;
3535
private final Endpoint endpoint;
36-
private String authToken = null;
36+
protected String authToken = null;
3737
// Thread pool for connections
3838
private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
3939
private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 20, 10, TimeUnit.SECONDS, queue);
4040
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
41-
private CountDownLatch authTokenLatch = new CountDownLatch(1);
41+
private CountDownLatch authTokenLatch;
4242
final JSONParser parser;
4343
// Responses from these endpoints will not be logged. The list can be overwritten.
4444
private List<String> logExcludedEndpoints = Arrays.asList(PIConstants.ENDPOINT_AUTH,
@@ -368,21 +368,26 @@ private void appendRealm(Map<String, String> params)
368368
*/
369369
private void retrieveAuthToken()
370370
{
371-
String response = runRequestAsync(ENDPOINT_AUTH, serviceAccountParam(), Collections.emptyMap(), false, POST);
372-
LinkedHashMap<String, String> authTokenMap = parser.extractAuthToken(response);
373-
this.authToken = authTokenMap.get(AUTH_TOKEN);
374-
int authTokenExp = Integer.parseInt(authTokenMap.get(AUTH_TOKEN_EXP));
375-
log("Auth token expires in: " + (authTokenExp - System.currentTimeMillis() / 1000L) + " seconds.");
376-
377-
// Schedule the next token retrieval to 1 min before expiration
378-
long delay = authTokenExp - 60 - System.currentTimeMillis() / 1000L;
379-
scheduler.schedule(this::retrieveAuthToken, delay, TimeUnit.SECONDS);
380-
381-
// Count down the latch to indicate that the token is retrieved
382-
authTokenLatch.countDown();
383-
384-
// Create a new CountDownLatch for the next token retrieval
385-
authTokenLatch = new CountDownLatch(1);
371+
try
372+
{
373+
authTokenLatch = new CountDownLatch(1);
374+
String response = runRequestAsync(ENDPOINT_AUTH, serviceAccountParam(), Collections.emptyMap(), false, POST);
375+
LinkedHashMap<String, String> authTokenMap = parser.extractAuthToken(response);
376+
this.authToken = authTokenMap.get(AUTH_TOKEN);
377+
long authTokenExp = Integer.parseInt(authTokenMap.get(AUTH_TOKEN_EXP));
378+
379+
// Schedule the next token retrieval to 1 min before expiration
380+
long delay = Math.max(1, authTokenExp - 60 - (System.currentTimeMillis() / 1000L));
381+
scheduler.schedule(this::retrieveAuthToken, delay, TimeUnit.SECONDS);
382+
383+
// Count down the latch to indicate that the token is retrieved
384+
authTokenLatch.countDown();
385+
}
386+
catch (Exception e)
387+
{
388+
error("Failed to retrieve auth token: " + e.getMessage());
389+
authTokenLatch.countDown();
390+
}
386391
}
387392

388393
/**
@@ -419,7 +424,8 @@ public boolean serviceAccountAvailable()
419424
* @param method http request method
420425
* @return response of the server as string or null
421426
*/
422-
private String runRequestAsync(String path, Map<String, String> params, Map<String, String> headers, boolean authTokenRequired,
427+
private String runRequestAsync(String path, Map<String, String> params, Map<String, String> headers,
428+
boolean authTokenRequired,
423429
String method)
424430
{
425431
if (!configuration.forwardClientIP.isEmpty())

0 commit comments

Comments
 (0)