|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + AppCheckInternalComponentName, |
| 20 | + AppCheckTokenListener, |
| 21 | + AppCheckTokenResult, |
| 22 | + FirebaseAppCheckInternal |
| 23 | +} from '@firebase/app-check-interop-types'; |
| 24 | +import { Provider } from '@firebase/component'; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Abstraction around AppCheck's token fetching capabilities. |
| 29 | + */ |
| 30 | +export class AppCheckTokenProvider { |
| 31 | + private appCheck?: FirebaseAppCheckInternal; |
| 32 | + constructor( |
| 33 | + private appName_: string, |
| 34 | + private appCheckProvider?: Provider<AppCheckInternalComponentName> |
| 35 | + ) { |
| 36 | + this.appCheck = appCheckProvider?.getImmediate({ optional: true }); |
| 37 | + if (!this.appCheck) { |
| 38 | + appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult> { |
| 43 | + if (!this.appCheck) { |
| 44 | + return new Promise<AppCheckTokenResult>((resolve, reject) => { |
| 45 | + // Support delayed initialization of FirebaseAppCheck. This allows our |
| 46 | + // customers to initialize the RTDB SDK before initializing Firebase |
| 47 | + // AppCheck and ensures that all requests are authenticated if a token |
| 48 | + // becomes available before the timoeout below expires. |
| 49 | + setTimeout(() => { |
| 50 | + if (this.appCheck) { |
| 51 | + this.getToken(forceRefresh).then(resolve, reject); |
| 52 | + } else { |
| 53 | + resolve(null); |
| 54 | + } |
| 55 | + }, 0); |
| 56 | + }); |
| 57 | + } |
| 58 | + return this.appCheck.getToken(forceRefresh); |
| 59 | + } |
| 60 | + |
| 61 | + addTokenChangeListener(listener: AppCheckTokenListener) { |
| 62 | + this.appCheckProvider |
| 63 | + ?.get() |
| 64 | + .then(appCheck => appCheck.addTokenListener(listener)); |
| 65 | + } |
| 66 | + |
| 67 | + // Not currently used at the moment. Will update if needed. |
| 68 | + // notifyForInvalidToken(): void { |
| 69 | + // warn( |
| 70 | + // `Provided AppCheck credentials for the app named "${this.appName_}" ` + |
| 71 | + // 'are invalid. This usually indicates your app was not initialized correctly.' |
| 72 | + // ); |
| 73 | + // } |
| 74 | +} |
0 commit comments