Skip to content

AppCheck do not schedule an auto-refresh if a stored token exists #5235

Open
@Gazer

Description

@Gazer

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 2022.3.1
  • Firebase Component: AppCheck
  • Component version: 32.2.0

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

When we call getAppCheckToken with forceRefresh as false, if we have a valid token stored in the cachedToken there is no call to tokenRefreshManager to schedule the next auto-refresh (if enable). Also, when the library is initialised and DefaultFirebaseAppCheck is created we call retrieveStoredAppCheckTokenInBackground to read and cache the stored token, but no call to tokenRefreshManager is done.

This means that if we restart the app with a valid token, we loose the auto-refresh feature (if enabled) and the next update will be when the cached token expires, the library will not try to refresh it in background.

Relevant Code:

private Task<Void> retrieveStoredAppCheckTokenInBackground(@NonNull Executor executor) {
    TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
    executor.execute(
        () -> {
          AppCheckToken token = storageHelper.retrieveAppCheckToken();
          if (token != null) {
            setCachedToken(token);
          }
          taskCompletionSource.setResult(null);
        });
    return taskCompletionSource.getTask();
  }

public Task<AppCheckToken> getAppCheckToken(boolean forceRefresh) {
    return retrieveStoredTokenTask.continueWithTask(
        liteExecutor,
        unused -> {
          if (!forceRefresh && hasValidToken()) {
            return Tasks.forResult(cachedToken);
          }
          if (appCheckProvider == null) {
            return Tasks.forException(new FirebaseException("No AppCheckProvider installed."));
          }
          return fetchTokenFromProvider();
        });
  }

One solution may be call tokenRefreshManager.maybeScheduleTokenRefresh(token); after setCachedToken(token); in retrieveStoredAppCheckTokenInBackground

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions