@@ -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- protected String authToken = null ;
36+ private String jwt = 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 ;
41+ private CountDownLatch jwtRetrievalLatch ;
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 ,
@@ -54,7 +54,7 @@ private PrivacyIDEA(PIConfig configuration, IPILogger logger, IPISimpleLogger si
5454 this .threadPool .allowCoreThreadTimeOut (true );
5555 if (serviceAccountAvailable ())
5656 {
57- retrieveAuthToken ();
57+ retrieveJWT ();
5858 }
5959 }
6060
@@ -364,53 +364,58 @@ private void appendRealm(Map<String, String> params)
364364 }
365365
366366 /**
367- * Retrieve the auth token from the /auth endpoint and schedule the next retrieval.
367+ * Retrieve the JWT from the /auth endpoint and schedule the next retrieval.
368368 */
369- private void retrieveAuthToken ()
369+ private void retrieveJWT ()
370370 {
371371 try
372372 {
373- authTokenLatch = new CountDownLatch (1 );
373+ this . jwtRetrievalLatch = new CountDownLatch (1 );
374374 String response = runRequestAsync (ENDPOINT_AUTH , serviceAccountParam (), Collections .emptyMap (), false , POST );
375375 LinkedHashMap <String , String > authTokenMap = parser .extractAuthToken (response );
376- this .authToken = authTokenMap .get (AUTH_TOKEN );
376+ this .jwt = authTokenMap .get (AUTH_TOKEN );
377377 long authTokenExp = Integer .parseInt (authTokenMap .get (AUTH_TOKEN_EXP ));
378378
379379 // Schedule the next token retrieval to 1 min before expiration
380380 long delay = Math .max (1 , authTokenExp - 60 - (System .currentTimeMillis () / 1000L ));
381- scheduler .schedule (this ::retrieveAuthToken , delay , TimeUnit .SECONDS );
382-
381+ this . scheduler .schedule (this ::retrieveJWT , delay , TimeUnit .SECONDS );
382+ log ( "Next JWT retrieval in " + delay + " seconds." );
383383 // Count down the latch to indicate that the token is retrieved
384- authTokenLatch .countDown ();
384+ this . jwtRetrievalLatch .countDown ();
385385 }
386386 catch (Exception e )
387387 {
388388 error ("Failed to retrieve auth token: " + e .getMessage ());
389- authTokenLatch .countDown ();
389+ this . jwtRetrievalLatch .countDown ();
390390 }
391391 }
392392
393393 /**
394- * Get the auth token from the /auth endpoint using the service account.
394+ * Get the JWT from the /auth endpoint using the service account.
395395 *
396- * @return auth token or null.
397- * @throws InterruptedException if the thread is interrupted while waiting for the auth token.
396+ * @return JWT as string or null on error.
398397 */
399- public String getAuthToken () throws InterruptedException
398+ public String getJWT ()
400399 {
401- // Wait for the auth token to be retrieved
402- authTokenLatch .await ();
403- return this .authToken ;
400+ try
401+ {
402+ jwtRetrievalLatch .await ();
403+ }
404+ catch (InterruptedException e )
405+ {
406+ error (e );
407+ return null ;
408+ }
409+ return this .jwt ;
404410 }
405411
406412 /**
407413 * @return true if a service account is available
408414 */
409415 public boolean serviceAccountAvailable ()
410416 {
411- return configuration .serviceAccountName != null && !configuration .serviceAccountName .isEmpty ()
412- && configuration .serviceAccountPass != null &&
413- !configuration .serviceAccountPass .isEmpty ();
417+ return configuration .serviceAccountName != null && !configuration .serviceAccountName .isEmpty () &&
418+ configuration .serviceAccountPass != null && !configuration .serviceAccountPass .isEmpty ();
414419 }
415420
416421 /**
@@ -424,16 +429,15 @@ public boolean serviceAccountAvailable()
424429 * @param method http request method
425430 * @return response of the server as string or null
426431 */
427- private String runRequestAsync (String path , Map <String , String > params , Map <String , String > headers ,
428- boolean authTokenRequired ,
432+ private String runRequestAsync (String path , Map <String , String > params , Map <String , String > headers , boolean authTokenRequired ,
429433 String method )
430434 {
431435 if (!configuration .forwardClientIP .isEmpty ())
432436 {
433437 params .put (CLIENT_IP , configuration .forwardClientIP );
434438 }
435- Callable <String > callable = new AsyncRequestCallable (this , endpoint , path , params , headers , authTokenRequired , method );
436- Future <String > future = threadPool .submit (callable );
439+ Callable <String > callable = new AsyncRequestCallable (this , this . endpoint , path , params , headers , authTokenRequired , method );
440+ Future <String > future = this . threadPool .submit (callable );
437441 String response = null ;
438442 try
439443 {
@@ -535,10 +539,6 @@ else if (this.simpleLog != null)
535539 {
536540 this .simpleLog .piLog (message );
537541 }
538- else
539- {
540- System .out .println (message );
541- }
542542 }
543543 }
544544
@@ -559,10 +559,6 @@ else if (this.simpleLog != null)
559559 {
560560 this .simpleLog .piLog (e .getMessage ());
561561 }
562- else
563- {
564- System .out .println (e .getLocalizedMessage ());
565- }
566562 }
567563 }
568564
@@ -743,7 +739,7 @@ public Builder proxy(String proxyHost, int proxyPort)
743739
744740 /**
745741 * Build the PrivacyIDEA instance with the set parameters.
746- *
742+ * If a service account is set, the JWT retrieval is done immediately.
747743 * @return PrivacyIDEA instance
748744 */
749745 public PrivacyIDEA build ()
0 commit comments