firebase / firebase/firebase-android-sdk

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

Open
#5,235 18 comments 2 reactions 0 assignees View on GitHub
api: app-check
Dominant language
Java
Stars
2.6k
Forks
710
Avg merge
2d 9h
Merged PRs (30d)
31

Description

### [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 retrieveStoredAppCheckTokenInBackground(@NonNull Executor executor) {
TaskCompletionSource taskCompletionSource = new TaskCompletionSource<>();
executor.execute(
() -> {
AppCheckToken token = storageHelper.retrieveAppCheckToken();
if (token != null) {
setCachedToken(token);
}
taskCompletionSource.setResult(null);
});
return taskCompletionSource.getTask();
}

public Task 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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.