Skip to content

Commit

Permalink
refactors requestNewAuthToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Greer committed Nov 14, 2023
1 parent 3c2e238 commit 13a7145
Showing 1 changed file with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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<String>() {
@Override
public String call() throws Exception {
return authHandler.onAuthTokenRequested();
}
}).onSuccess(new Future.SuccessCallback<String>() {
@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;
Expand All @@ -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 {
Expand Down

0 comments on commit 13a7145

Please sign in to comment.