Skip to content

Commit 25f264f

Browse files
committed
Initial implementation across all SDKs.
1 parent e3e2078 commit 25f264f

File tree

14 files changed

+86
-13
lines changed

14 files changed

+86
-13
lines changed

common/api-review/app.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface FirebaseServerApp extends FirebaseApp {
7979

8080
// @public
8181
export interface FirebaseServerAppSettings extends Omit<FirebaseAppSettings, 'name'> {
82+
appCheckToken?: string;
8283
authIdToken?: string;
8384
releaseOnDeref?: object;
8485
}

packages/app/src/public-types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ export interface FirebaseServerAppSettings
196196
*/
197197
authIdToken?: string;
198198

199+
/**
200+
* An optional App Check token. If provided, the Firebase SDKs that use App Check will utilizze
201+
* this App Check token in lieu of requiring an instance of App Check to be initialized.
202+
*/
203+
appCheckToken?: string;
204+
199205
/**
200206
* An optional object. If provided, the Firebase SDK uses a `FinalizationRegistry`
201207
* object to monitor the garbage collection status of the provided object. The

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,9 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
845845
}
846846

847847
async _getAppCheckToken(): Promise<string | undefined> {
848+
if (_isFirebaseServerApp(this.app) && this.app.settings.appCheckToken) {
849+
return this.app.settings.appCheckToken;
850+
}
848851
const appCheckTokenResult = await this.appCheckServiceProvider
849852
.getImmediate({ optional: true })
850853
?.getToken();

packages/data-connect/src/api/DataConnect.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class DataConnect {
9292
private _transportOptions?: TransportOptions;
9393
private _authTokenProvider?: AuthTokenProvider;
9494
_isUsingGeneratedSdk: boolean = false;
95-
private _appCheckTokenProvider?: AppCheckTokenProvider;
95+
private _appCheckTokenProvider: AppCheckTokenProvider;
9696
// @internal
9797
constructor(
9898
public readonly app: FirebaseApp,
@@ -149,12 +149,10 @@ export class DataConnect {
149149
this._authProvider
150150
);
151151
}
152-
if (this._appCheckProvider) {
153-
this._appCheckTokenProvider = new AppCheckTokenProvider(
154-
this.app.name,
155-
this._appCheckProvider
156-
);
157-
}
152+
this._appCheckTokenProvider = new AppCheckTokenProvider(
153+
this.app,
154+
this._appCheckProvider
155+
);
158156

159157
this._initialized = true;
160158
this._transport = new this._transportClass(

packages/data-connect/src/core/AppCheckTokenProvider.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { FirebaseApp, _isFirebaseServerApp } from '@firebase/app';
1819
import {
1920
AppCheckInternalComponentName,
2021
AppCheckTokenListener,
@@ -29,10 +30,14 @@ import { Provider } from '@firebase/component';
2930
*/
3031
export class AppCheckTokenProvider {
3132
private appCheck?: FirebaseAppCheckInternal;
33+
private serverAppAppCheckToken?: string;
3234
constructor(
33-
private appName_: string,
35+
app: FirebaseApp,
3436
private appCheckProvider?: Provider<AppCheckInternalComponentName>
3537
) {
38+
if (_isFirebaseServerApp(app) && app.settings.appCheckToken) {
39+
this.serverAppAppCheckToken = app.settings.appCheckToken;
40+
}
3641
this.appCheck = appCheckProvider?.getImmediate({ optional: true });
3742
if (!this.appCheck) {
3843
void appCheckProvider
@@ -43,6 +48,10 @@ export class AppCheckTokenProvider {
4348
}
4449

4550
getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult> {
51+
if (this.serverAppAppCheckToken) {
52+
return Promise.resolve({ token: this.serverAppAppCheckToken });
53+
}
54+
4655
if (!this.appCheck) {
4756
return new Promise<AppCheckTokenResult>((resolve, reject) => {
4857
// Support delayed initialization of FirebaseAppCheck. This allows our

packages/database/src/api/Database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export function repoManagerDatabaseFromApp(
164164
repoInfo,
165165
app,
166166
authTokenProvider,
167-
new AppCheckTokenProvider(app.name, appCheckProvider)
167+
new AppCheckTokenProvider(app, appCheckProvider)
168168
);
169169
return new Database(repo, app);
170170
}

packages/database/src/core/AppCheckTokenProvider.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { FirebaseApp, _isFirebaseServerApp } from '@firebase/app'; // eslint-disable-line import/no-extraneous-dependencies
1819
import {
1920
AppCheckInternalComponentName,
2021
AppCheckTokenListener,
@@ -30,17 +31,26 @@ import { warn } from './util/util';
3031
*/
3132
export class AppCheckTokenProvider {
3233
private appCheck?: FirebaseAppCheckInternal;
34+
private serverAppAppCheckToken?: string;
35+
private appName: string;
3336
constructor(
34-
private appName_: string,
37+
app: FirebaseApp,
3538
private appCheckProvider?: Provider<AppCheckInternalComponentName>
3639
) {
40+
this.appName = app.name;
41+
if (_isFirebaseServerApp(app) && app.settings.appCheckToken) {
42+
this.serverAppAppCheckToken = app.settings.appCheckToken;
43+
}
3744
this.appCheck = appCheckProvider?.getImmediate({ optional: true });
3845
if (!this.appCheck) {
3946
appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck));
4047
}
4148
}
4249

4350
getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult> {
51+
if (this.serverAppAppCheckToken) {
52+
return Promise.resolve({ token: this.serverAppAppCheckToken });
53+
}
4454
if (!this.appCheck) {
4555
return new Promise<AppCheckTokenResult>((resolve, reject) => {
4656
// Support delayed initialization of FirebaseAppCheck. This allows our
@@ -67,7 +77,7 @@ export class AppCheckTokenProvider {
6777

6878
notifyForInvalidToken(): void {
6979
warn(
70-
`Provided AppCheck credentials for the app named "${this.appName_}" ` +
80+
`Provided AppCheck credentials for the app named "${this.appName}" ` +
7181
'are invalid. This usually indicates your app was not initialized correctly.'
7282
);
7383
}

packages/firestore/lite/register.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function registerFirestore(): void {
4949
container.getProvider('auth-internal')
5050
),
5151
new LiteAppCheckTokenProvider(
52+
app,
5253
container.getProvider('app-check-internal')
5354
),
5455
databaseIdFromApp(app, databaseId),

packages/firestore/src/api/credentials.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { FirebaseApp, _isFirebaseServerApp } from '@firebase/app';
1819
import {
1920
AppCheckInternalComponentName,
2021
AppCheckTokenListener,
@@ -497,6 +498,7 @@ export class FirebaseAppCheckTokenProvider
497498
private latestAppCheckToken: string | null = null;
498499

499500
constructor(
501+
private app: FirebaseApp,
500502
private appCheckProvider: Provider<AppCheckInternalComponentName>
501503
) {}
502504

@@ -562,6 +564,11 @@ export class FirebaseAppCheckTokenProvider
562564
}
563565

564566
getToken(): Promise<Token | null> {
567+
if (_isFirebaseServerApp(this.app) && this.app.settings.appCheckToken) {
568+
return Promise.resolve(
569+
new AppCheckToken(this.app.settings.appCheckToken)
570+
);
571+
}
565572
debugAssert(
566573
this.tokenListener != null,
567574
'FirebaseAppCheckTokenProvider not started.'
@@ -622,16 +629,25 @@ export class EmptyAppCheckTokenProvider implements CredentialsProvider<string> {
622629
/** AppCheck token provider for the Lite SDK. */
623630
export class LiteAppCheckTokenProvider implements CredentialsProvider<string> {
624631
private appCheck: FirebaseAppCheckInternal | null = null;
632+
private serverAppAppCheckToken?: string;
625633

626634
constructor(
635+
private app: FirebaseApp,
627636
private appCheckProvider: Provider<AppCheckInternalComponentName>
628637
) {
638+
if (_isFirebaseServerApp(app) && app.settings.appCheckToken) {
639+
this.serverAppAppCheckToken = app.settings.appCheckToken;
640+
}
629641
appCheckProvider.onInit(appCheck => {
630642
this.appCheck = appCheck;
631643
});
632644
}
633645

634646
getToken(): Promise<Token | null> {
647+
if (this.serverAppAppCheckToken) {
648+
return Promise.resolve(new AppCheckToken(this.serverAppAppCheckToken));
649+
}
650+
635651
if (!this.appCheck) {
636652
return Promise.resolve(null);
637653
}

packages/firestore/src/register.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function registerFirestore(
4747
container.getProvider('auth-internal')
4848
),
4949
new FirebaseAppCheckTokenProvider(
50+
app,
5051
container.getProvider('app-check-internal')
5152
),
5253
databaseIdFromApp(app, databaseId),

0 commit comments

Comments
 (0)