@@ -368,26 +368,32 @@ private void appendRealm(Map<String, String> params)
368368 */
369369 private void retrieveJWT ()
370370 {
371+ this .jwtRetrievalLatch = new CountDownLatch (1 );
371372 try
372373 {
373- this .jwtRetrievalLatch = new CountDownLatch (1 );
374374 String response = runRequestAsync (ENDPOINT_AUTH , serviceAccountParam (), Collections .emptyMap (), false , POST );
375- LinkedHashMap <String , String > authTokenMap = parser .extractAuthToken (response );
376- this .jwt = 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- this .scheduler .schedule (this ::retrieveJWT , delay , TimeUnit .SECONDS );
382- log ("Next JWT retrieval in " + delay + " seconds." );
383- // Count down the latch to indicate that the token is retrieved
384- this .jwtRetrievalLatch .countDown ();
375+ if (response == null )
376+ {
377+ error ("Failed to retrieve auth token, response was empty. Retrying in 10 seconds." );
378+ this .scheduler .schedule (this ::retrieveJWT , 10 , TimeUnit .SECONDS );
379+ }
380+ else
381+ {
382+ LinkedHashMap <String , String > authTokenMap = parser .extractAuthToken (response );
383+ this .jwt = authTokenMap .get (AUTH_TOKEN );
384+ long authTokenExp = Integer .parseInt (authTokenMap .get (AUTH_TOKEN_EXP ));
385+
386+ // Schedule the next token retrieval to 1 min before expiration
387+ long delay = Math .max (1 , authTokenExp - 60 - (System .currentTimeMillis () / 1000L ));
388+ this .scheduler .schedule (this ::retrieveJWT , delay , TimeUnit .SECONDS );
389+ log ("Next JWT retrieval in " + delay + " seconds." );
390+ }
385391 }
386392 catch (Exception e )
387393 {
388394 error ("Failed to retrieve auth token: " + e .getMessage ());
389- this .jwtRetrievalLatch .countDown ();
390395 }
396+ this .jwtRetrievalLatch .countDown ();
391397 }
392398
393399 /**
@@ -740,6 +746,7 @@ public Builder proxy(String proxyHost, int proxyPort)
740746 /**
741747 * Build the PrivacyIDEA instance with the set parameters.
742748 * If a service account is set, the JWT retrieval is done immediately.
749+ *
743750 * @return PrivacyIDEA instance
744751 */
745752 public PrivacyIDEA build ()
0 commit comments