diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java index 27ee5aaaaa7b..2c140c1a7b94 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java @@ -15,6 +15,7 @@ import java.util.Objects; import java.util.Timer; import java.util.TimerTask; +import java.util.function.Supplier; import com.azure.communication.common.implementation.TokenParser; @@ -28,7 +29,7 @@ public final class CommunicationTokenCredential implements AutoCloseable { private AccessToken accessToken; private final TokenParser tokenParser = new TokenParser(); - private TokenRefresher refresher; + private Supplier> refresher; private FetchingTask fetchingTask; private boolean isClosed = false; @@ -52,11 +53,11 @@ public CommunicationTokenCredential(String token) { * @param tokenRefreshOptions implementation to supply fresh token when reqested */ public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions) { - TokenRefresher tokenRefresher = tokenRefreshOptions.getTokenRefresher(); + Supplier> tokenRefresher = tokenRefreshOptions.getTokenRefresher(); Objects.requireNonNull(tokenRefresher, "'tokenRefresher' cannot be null."); refresher = tokenRefresher; - if (tokenRefreshOptions.getToken() != null) { - setToken(tokenRefreshOptions.getToken()); + if (tokenRefreshOptions.getInitialToken() != null) { + setToken(tokenRefreshOptions.getInitialToken()); if (tokenRefreshOptions.isRefreshProactively()) { OffsetDateTime nextFetchTime = accessToken.getExpiresAt().minusMinutes(DEFAULT_EXPIRING_OFFSET_MINUTES); fetchingTask = new FetchingTask(this, nextFetchTime); @@ -114,10 +115,10 @@ private void setToken(String freshToken) { } private Mono fetchFreshToken() { - Mono tokenAsync = refresher.getToken(); + Mono tokenAsync = refresher.get(); if (tokenAsync == null) { return FluxUtil.monoError(logger, - new RuntimeException("TokenRefresher returned null when getTokenAsync is called")); + new RuntimeException("get() function of the token refresher should not return null.")); } return tokenAsync; } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenRefreshOptions.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenRefreshOptions.java index 8f4f1095b074..82d2020c2511 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenRefreshOptions.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenRefreshOptions.java @@ -2,11 +2,14 @@ // Licensed under the MIT License. package com.azure.communication.common; +import java.util.function.Supplier; +import reactor.core.publisher.Mono; + /** * Options for refreshing CommunicationTokenCredential */ public final class CommunicationTokenRefreshOptions { - private final TokenRefresher tokenRefresher; + private final Supplier> tokenRefresher; private final boolean refreshProactively; private final String initialToken; @@ -19,7 +22,7 @@ public final class CommunicationTokenRefreshOptions { * with setCallbackOffsetMinutes or default value of * two minutes */ - public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean refreshProactively) { + public CommunicationTokenRefreshOptions(Supplier> tokenRefresher, boolean refreshProactively) { this.tokenRefresher = tokenRefresher; this.refreshProactively = refreshProactively; this.initialToken = null; @@ -35,7 +38,7 @@ public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean r * two minutes * @param initialToken the optional serialized JWT token */ - public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean refreshProactively, String initialToken) { + public CommunicationTokenRefreshOptions(Supplier> tokenRefresher, boolean refreshProactively, String initialToken) { this.tokenRefresher = tokenRefresher; this.refreshProactively = refreshProactively; this.initialToken = initialToken; @@ -44,7 +47,7 @@ public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean r /** * @return the token refresher to provide capacity to fetch fresh token */ - public TokenRefresher getTokenRefresher() { + public Supplier> getTokenRefresher() { return tokenRefresher; } @@ -56,9 +59,9 @@ public boolean isRefreshProactively() { } /** - * @return the serialized JWT token + * @return the initial token */ - public String getToken() { + public String getInitialToken() { return initialToken; } } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/TokenRefresher.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/TokenRefresher.java deleted file mode 100644 index 4b55d527de5a..000000000000 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/TokenRefresher.java +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. -package com.azure.communication.common; - -import reactor.core.publisher.Mono; - -/** - * Interface to provide capacity to fetch fresh token - */ -@FunctionalInterface -public interface TokenRefresher { - /** - * Asynchronous call to fetch a fresh token - * @return Wrapper for asynchronous call - */ - Mono getToken(); -} diff --git a/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationTokenCredentialTests.java b/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationTokenCredentialTests.java index d0fc9aa27de8..244a14da6b4e 100644 --- a/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationTokenCredentialTests.java +++ b/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationTokenCredentialTests.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.function.Supplier; public class CommunicationTokenCredentialTests { private final JwtTokenMocker tokenMocker = new JwtTokenMocker(); @@ -55,7 +56,7 @@ public void constructWithExpiredTokenWithoutRefresher() tokenCredential.close(); } - class MockImmediateRefresher implements TokenRefresher { + class MockImmediateRefresher implements Supplier> { private int numCalls = 0; private Runnable onCallReturn; @@ -72,7 +73,7 @@ public void resetCallCount() { } @Override - public Mono getToken() { + public Mono get() { numCalls++; if (this.onCallReturn != null) { this.onCallReturn.run(); @@ -237,7 +238,7 @@ public void shouldStopRefreshTimerWhenClosed() throws InterruptedException, Exec assertFalse(tokenCredential.hasProactiveFetcher()); } - class ExceptionRefresher implements TokenRefresher { + class ExceptionRefresher implements Supplier> { private int numCalls; private Runnable onCallReturn; @@ -254,7 +255,7 @@ public void resetCallCount() { } @Override - public Mono getToken() { + public Mono get() { numCalls++; if (this.onCallReturn != null) { this.onCallReturn.run();