From 13a714533ea0a783d127035809c55db28fe719e5 Mon Sep 17 00:00:00 2001 From: Evan Greer Date: Tue, 14 Nov 2023 15:23:57 -0700 Subject: [PATCH] refactors requestNewAuthToken --- .../iterableapi/IterableAuthManager.java | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java index 6ad569c6c..ae02abfde 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java @@ -11,7 +11,8 @@ import java.io.UnsupportedEncodingException; import java.util.Timer; import java.util.TimerTask; -import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class IterableAuthManager { private static final String TAG = "IterableAuth"; @@ -20,6 +21,7 @@ public class IterableAuthManager { private final IterableApi api; private final IterableAuthHandler authHandler; private final long expiringAuthTokenRefreshPeriod; + private final ExecutorService executor = Executors.newSingleThreadExecutor(); @VisibleForTesting Timer timer; @@ -35,43 +37,17 @@ public class IterableAuthManager { public synchronized void requestNewAuthToken(boolean hasFailedPriorAuth) { if (authHandler != null) { - if (!pendingAuth) { - if (!(this.hasFailedPriorAuth && hasFailedPriorAuth)) { - this.hasFailedPriorAuth = hasFailedPriorAuth; - pendingAuth = true; - Future.runAsync(new Callable() { - @Override - public String call() throws Exception { - return authHandler.onAuthTokenRequested(); - } - }).onSuccess(new Future.SuccessCallback() { - @Override - public void onSuccess(String authToken) { - if (authToken != null) { - queueExpirationRefresh(authToken); - } else { - IterableLogger.w(TAG, "Auth token received as null. Calling the handler in 10 seconds"); - //TODO: Make this time configurable and in sync with SDK initialization flow for auth null scenario - scheduleAuthTokenRefresh(10000); - authHandler.onTokenRegistrationFailed(new Throwable("Auth token null")); - return; - } - IterableApi.getInstance().setAuthToken(authToken); - pendingAuth = false; - reSyncAuth(); - authHandler.onTokenRegistrationSuccessful(authToken); - } - }) - .onFailure(new Future.FailureCallback() { - @Override - public void onFailure(Throwable throwable) { - IterableLogger.e(TAG, "Error while requesting Auth Token", throwable); - authHandler.onTokenRegistrationFailed(throwable); - pendingAuth = false; - reSyncAuth(); - } - }); - } + if (!pendingAuth && !(this.hasFailedPriorAuth && hasFailedPriorAuth)) { + this.hasFailedPriorAuth = hasFailedPriorAuth; + pendingAuth = true; + executor.submit(() -> { + try { + final String authToken = authHandler.onAuthTokenRequested(); + handleAuthTokenSuccess(authToken); + } catch (final Exception e) { + handleAuthTokenFailure(e); + } + }); } else if (!hasFailedPriorAuth) { //setFlag to resync auth after current auth returns requiresAuthRefresh = true; @@ -82,6 +58,29 @@ public void onFailure(Throwable throwable) { } } + private void handleAuthTokenSuccess(String authToken) { + if (authToken != null) { + queueExpirationRefresh(authToken); + } else { + IterableLogger.w(TAG, "Auth token received as null. Calling the handler in 10 seconds"); + //TODO: Make this time configurable and in sync with SDK initialization flow for auth null scenario + scheduleAuthTokenRefresh(10000); + authHandler.onTokenRegistrationFailed(new Throwable("Auth token null")); + return; + } + IterableApi.getInstance().setAuthToken(authToken); + pendingAuth = false; + reSyncAuth(); + authHandler.onTokenRegistrationSuccessful(authToken); + } + + private void handleAuthTokenFailure(Throwable throwable) { + IterableLogger.e(TAG, "Error while requesting Auth Token", throwable); + authHandler.onTokenRegistrationFailed(throwable); + pendingAuth = false; + reSyncAuth(); + } + public void queueExpirationRefresh(String encodedJWT) { clearRefreshTimer(); try {