Skip to content

Commit

Permalink
Re-establish streams when App Check token expires. (#5902)
Browse files Browse the repository at this point in the history
* Re-establish streams when App Check token expires.

* Create happy-badgers-leave.md

* Better patch message.

* better patch message.

* address comments.

* Remove outdated comment.

* Update packages/firestore/src/core/firestore_client.ts

Co-authored-by: Sebastian Schmidt <mrschmidt@google.com>

Co-authored-by: Sebastian Schmidt <mrschmidt@google.com>
  • Loading branch information
ehsannas and schmidt-sebastian committed Jan 26, 2022
1 parent 2820674 commit e28b0e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/happy-badgers-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@firebase/firestore": patch
---

Fixed an AppCheck issue that caused Firestore listeners to stop working and
receive a "Permission Denied" error. This issue only occurred for AppCheck users
that set their expiration time to under an hour.
19 changes: 17 additions & 2 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export class FirestoreClient {
private readonly clientId = AutoId.newId();
private authCredentialListener: CredentialChangeListener<User> = () =>
Promise.resolve();
private appCheckCredentialListener: (
appCheckToken: string,
user: User
) => Promise<void> = () => Promise.resolve();

offlineComponents?: OfflineComponentProvider;
onlineComponents?: OnlineComponentProvider;
Expand All @@ -122,8 +126,10 @@ export class FirestoreClient {
await this.authCredentialListener(user);
this.user = user;
});
// Register an empty credentials change listener to activate token refresh.
this.appCheckCredentials.start(asyncQueue, () => Promise.resolve());
this.appCheckCredentials.start(asyncQueue, newAppCheckToken => {
logDebug(LOG_TAG, 'Received new app check token=', newAppCheckToken);
return this.appCheckCredentialListener(newAppCheckToken, this.user);
});
}

async getConfiguration(): Promise<ComponentConfiguration> {
Expand All @@ -142,6 +148,12 @@ export class FirestoreClient {
this.authCredentialListener = listener;
}

setAppCheckTokenChangeListener(
listener: (appCheckToken: string, user: User) => Promise<void>
): void {
this.appCheckCredentialListener = listener;
}

/**
* Checks that the client has not been terminated. Ensures that other methods on
* this class cannot be called after the client is terminated.
Expand Down Expand Up @@ -234,6 +246,9 @@ export async function setOnlineComponentProvider(
client.setCredentialChangeListener(user =>
remoteStoreHandleCredentialChange(onlineComponentProvider.remoteStore, user)
);
client.setAppCheckTokenChangeListener((_, user) =>
remoteStoreHandleCredentialChange(onlineComponentProvider.remoteStore, user)
);
client.onlineComponents = onlineComponentProvider;
}

Expand Down

0 comments on commit e28b0e4

Please sign in to comment.