2323import java .util .UUID ;
2424import java .util .concurrent .ConcurrentHashMap ;
2525import java .util .concurrent .TimeUnit ;
26- import java .util .concurrent .locks .Lock ;
27- import java .util .concurrent .locks .ReentrantLock ;
2826import okhttp3 .*;
2927import org .jetbrains .annotations .NotNull ;
3028
@@ -48,7 +46,7 @@ public class KeyFlowAuthenticator implements Authenticator {
4846 private final String tokenUrl ;
4947 private long tokenLeewayInSeconds = DEFAULT_TOKEN_LEEWAY ;
5048
51- private final Lock tokenRefreshLock = new ReentrantLock ();
49+ private final Object tokenRefreshMonitor = new Object ();
5250
5351 /**
5452 * Creates the initial service account and refreshes expired access token.
@@ -190,20 +188,16 @@ protected String getAccessToken() {
190188 * @throws IOException request for new access token failed
191189 * @throws ApiException response for new access token with bad status code
192190 */
191+ @ SuppressWarnings ("PMD.AvoidSynchronizedStatement" )
193192 public String getAccessToken () throws IOException , ApiException , InvalidKeySpecException {
194- try {
195- tokenRefreshLock .lock ();
196-
193+ synchronized (tokenRefreshMonitor ) {
197194 if (token == null ) {
198195 createAccessToken ();
199196 } else if (token .isExpired ()) {
200197 createAccessTokenWithRefreshToken ();
201198 }
202- } finally {
203- tokenRefreshLock .unlock ();
199+ return token .getAccessToken ();
204200 }
205-
206- return token .getAccessToken ();
207201 }
208202
209203 /**
@@ -214,20 +208,23 @@ public String getAccessToken() throws IOException, ApiException, InvalidKeySpecE
214208 * @throws ApiException response for new access token with bad status code
215209 * @throws JsonSyntaxException parsing of the created access token failed
216210 */
211+ @ SuppressWarnings ("PMD.AvoidSynchronizedStatement" )
217212 protected void createAccessToken ()
218213 throws InvalidKeySpecException , IOException , ApiException {
219- String assertion ;
220- try {
221- assertion = generateSelfSignedJWT ();
222- } catch (NoSuchAlgorithmException e ) {
223- throw new IllegalStateException (
224- "could not find required algorithm for jwt signing. This should not happen and should be reported on https://github.com/stackitcloud/stackit-sdk-java/issues" ,
225- e );
226- }
214+ synchronized (tokenRefreshMonitor ) {
215+ String assertion ;
216+ try {
217+ assertion = generateSelfSignedJWT ();
218+ } catch (NoSuchAlgorithmException e ) {
219+ throw new IllegalStateException (
220+ "could not find required algorithm for jwt signing. This should not happen and should be reported on https://github.com/stackitcloud/stackit-sdk-java/issues" ,
221+ e );
222+ }
227223
228- String grant = "urn:ietf:params:oauth:grant-type:jwt-bearer" ;
229- try (Response response = requestToken (grant , assertion ).execute ()) {
230- parseTokenResponse (response );
224+ String grant = "urn:ietf:params:oauth:grant-type:jwt-bearer" ;
225+ try (Response response = requestToken (grant , assertion ).execute ()) {
226+ parseTokenResponse (response );
227+ }
231228 }
232229 }
233230
@@ -238,11 +235,14 @@ protected void createAccessToken()
238235 * @throws ApiException response for new access token with bad status code
239236 * @throws JsonSyntaxException can not parse new access token
240237 */
241- protected synchronized void createAccessTokenWithRefreshToken ()
238+ @ SuppressWarnings ("PMD.AvoidSynchronizedStatement" )
239+ protected void createAccessTokenWithRefreshToken ()
242240 throws IOException , ApiException {
243- String refreshToken = token .refreshToken ;
244- try (Response response = requestToken (REFRESH_TOKEN , refreshToken ).execute ()) {
245- parseTokenResponse (response );
241+ synchronized (tokenRefreshMonitor ) {
242+ String refreshToken = token .refreshToken ;
243+ try (Response response = requestToken (REFRESH_TOKEN , refreshToken ).execute ()) {
244+ parseTokenResponse (response );
245+ }
246246 }
247247 }
248248
@@ -253,7 +253,7 @@ protected synchronized void createAccessTokenWithRefreshToken()
253253 * @throws ApiException if the response has a bad status code
254254 * @throws JsonSyntaxException if the response body cannot be parsed
255255 */
256- private synchronized void parseTokenResponse (Response response )
256+ private void parseTokenResponse (Response response )
257257 throws ApiException {
258258 if (response .code () != HttpURLConnection .HTTP_OK ) {
259259 String body = null ;
0 commit comments