app-check.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Firebase App Check
  3. *
  4. * @packageDocumentation
  5. */
  6. import { FirebaseApp } from '@firebase/app';
  7. import { PartialObserver } from '@firebase/util';
  8. import { Unsubscribe } from '@firebase/util';
  9. /**
  10. * The Firebase App Check service interface.
  11. *
  12. * @public
  13. */
  14. export declare interface AppCheck {
  15. /**
  16. * The {@link @firebase/app#FirebaseApp} this `AppCheck` instance is associated with.
  17. */
  18. app: FirebaseApp;
  19. }
  20. /**
  21. * @internal
  22. */
  23. export declare type _AppCheckComponentName = 'app-check';
  24. /**
  25. * @internal
  26. */
  27. export declare type _AppCheckInternalComponentName = 'app-check-internal';
  28. /**
  29. * Options for App Check initialization.
  30. * @public
  31. */
  32. export declare interface AppCheckOptions {
  33. /**
  34. * A reCAPTCHA V3 provider, reCAPTCHA Enterprise provider, or custom provider.
  35. */
  36. provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
  37. /**
  38. * If set to true, enables automatic background refresh of App Check token.
  39. */
  40. isTokenAutoRefreshEnabled?: boolean;
  41. }
  42. declare interface AppCheckProvider {
  43. /**
  44. * Returns an App Check token.
  45. * @internal
  46. */
  47. getToken: () => Promise<AppCheckTokenInternal>;
  48. /**
  49. * @internal
  50. */
  51. initialize(app: FirebaseApp): void;
  52. }
  53. /**
  54. * The token returned from an App Check provider.
  55. * @public
  56. */
  57. export declare interface AppCheckToken {
  58. readonly token: string;
  59. /**
  60. * The local timestamp after which the token will expire.
  61. */
  62. readonly expireTimeMillis: number;
  63. }
  64. declare interface AppCheckTokenInternal extends AppCheckToken {
  65. issuedAtTimeMillis: number;
  66. }
  67. /**
  68. * A listener that is called whenever the App Check token changes.
  69. * @public
  70. */
  71. export declare type AppCheckTokenListener = (token: AppCheckTokenResult) => void;
  72. /**
  73. * Result returned by `getToken()`.
  74. * @public
  75. */
  76. export declare interface AppCheckTokenResult {
  77. /**
  78. * The token string in JWT format.
  79. */
  80. readonly token: string;
  81. }
  82. /**
  83. * Custom provider class.
  84. * @public
  85. */
  86. export declare class CustomProvider implements AppCheckProvider {
  87. private _customProviderOptions;
  88. private _app?;
  89. constructor(_customProviderOptions: CustomProviderOptions);
  90. /**
  91. * @internal
  92. */
  93. getToken(): Promise<AppCheckTokenInternal>;
  94. /**
  95. * @internal
  96. */
  97. initialize(app: FirebaseApp): void;
  98. /**
  99. * @internal
  100. */
  101. isEqual(otherProvider: unknown): boolean;
  102. }
  103. /**
  104. * Options when creating a {@link CustomProvider}.
  105. * @public
  106. */
  107. export declare interface CustomProviderOptions {
  108. /**
  109. * Function to get an App Check token through a custom provider
  110. * service.
  111. */
  112. getToken: () => Promise<AppCheckToken>;
  113. }
  114. /**
  115. * Requests a Firebase App Check token. This method should be used
  116. * only if you need to authorize requests to a non-Firebase backend.
  117. *
  118. * Returns limited-use tokens that are intended for use with your
  119. * non-Firebase backend endpoints that are protected with
  120. * <a href="https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection">
  121. * Replay Protection</a>. This method
  122. * does not affect the token generation behavior of the
  123. * #getAppCheckToken() method.
  124. *
  125. * @param appCheckInstance - The App Check service instance.
  126. * @returns The limited use token.
  127. * @public
  128. */
  129. export declare function getLimitedUseToken(appCheckInstance: AppCheck): Promise<AppCheckTokenResult>;
  130. /**
  131. * Get the current App Check token. Attaches to the most recent
  132. * in-flight request if one is present. Returns null if no token
  133. * is present and no token requests are in-flight.
  134. *
  135. * @param appCheckInstance - The App Check service instance.
  136. * @param forceRefresh - If true, will always try to fetch a fresh token.
  137. * If false, will use a cached token if found in storage.
  138. * @public
  139. */
  140. export declare function getToken(appCheckInstance: AppCheck, forceRefresh?: boolean): Promise<AppCheckTokenResult>;
  141. /**
  142. * Activate App Check for the given app. Can be called only once per app.
  143. * @param app - the {@link @firebase/app#FirebaseApp} to activate App Check for
  144. * @param options - App Check initialization options
  145. * @public
  146. */
  147. export declare function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck;
  148. /**
  149. * Registers a listener to changes in the token state. There can be more
  150. * than one listener registered at the same time for one or more
  151. * App Check instances. The listeners call back on the UI thread whenever
  152. * the current token associated with this App Check instance changes.
  153. *
  154. * @param appCheckInstance - The App Check service instance.
  155. * @param observer - An object with `next`, `error`, and `complete`
  156. * properties. `next` is called with an
  157. * {@link AppCheckTokenResult}
  158. * whenever the token changes. `error` is optional and is called if an
  159. * error is thrown by the listener (the `next` function). `complete`
  160. * is unused, as the token stream is unending.
  161. *
  162. * @returns A function that unsubscribes this listener.
  163. * @public
  164. */
  165. export declare function onTokenChanged(appCheckInstance: AppCheck, observer: PartialObserver<AppCheckTokenResult>): Unsubscribe;
  166. /**
  167. * Registers a listener to changes in the token state. There can be more
  168. * than one listener registered at the same time for one or more
  169. * App Check instances. The listeners call back on the UI thread whenever
  170. * the current token associated with this App Check instance changes.
  171. *
  172. * @param appCheckInstance - The App Check service instance.
  173. * @param onNext - When the token changes, this function is called with aa
  174. * {@link AppCheckTokenResult}.
  175. * @param onError - Optional. Called if there is an error thrown by the
  176. * listener (the `onNext` function).
  177. * @param onCompletion - Currently unused, as the token stream is unending.
  178. * @returns A function that unsubscribes this listener.
  179. * @public
  180. */
  181. export declare function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe;
  182. export { PartialObserver }
  183. /**
  184. * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
  185. * for an App Check token.
  186. *
  187. * @public
  188. */
  189. export declare class ReCaptchaEnterpriseProvider implements AppCheckProvider {
  190. private _siteKey;
  191. private _app?;
  192. private _heartbeatServiceProvider?;
  193. /**
  194. * Throttle requests on certain error codes to prevent too many retries
  195. * in a short time.
  196. */
  197. private _throttleData;
  198. /**
  199. * Create a ReCaptchaEnterpriseProvider instance.
  200. * @param siteKey - reCAPTCHA Enterprise score-based site key.
  201. */
  202. constructor(_siteKey: string);
  203. /**
  204. * Returns an App Check token.
  205. * @internal
  206. */
  207. getToken(): Promise<AppCheckTokenInternal>;
  208. /**
  209. * @internal
  210. */
  211. initialize(app: FirebaseApp): void;
  212. /**
  213. * @internal
  214. */
  215. isEqual(otherProvider: unknown): boolean;
  216. }
  217. /**
  218. * App Check provider that can obtain a reCAPTCHA V3 token and exchange it
  219. * for an App Check token.
  220. *
  221. * @public
  222. */
  223. export declare class ReCaptchaV3Provider implements AppCheckProvider {
  224. private _siteKey;
  225. private _app?;
  226. private _heartbeatServiceProvider?;
  227. /**
  228. * Throttle requests on certain error codes to prevent too many retries
  229. * in a short time.
  230. */
  231. private _throttleData;
  232. /**
  233. * Create a ReCaptchaV3Provider instance.
  234. * @param siteKey - ReCAPTCHA V3 siteKey.
  235. */
  236. constructor(_siteKey: string);
  237. /**
  238. * Returns an App Check token.
  239. * @internal
  240. */
  241. getToken(): Promise<AppCheckTokenInternal>;
  242. /**
  243. * @internal
  244. */
  245. initialize(app: FirebaseApp): void;
  246. /**
  247. * @internal
  248. */
  249. isEqual(otherProvider: unknown): boolean;
  250. }
  251. /**
  252. * Set whether App Check will automatically refresh tokens as needed.
  253. *
  254. * @param appCheckInstance - The App Check service instance.
  255. * @param isTokenAutoRefreshEnabled - If true, the SDK automatically
  256. * refreshes App Check tokens as needed. This overrides any value set
  257. * during `initializeAppCheck()`.
  258. * @public
  259. */
  260. export declare function setTokenAutoRefreshEnabled(appCheckInstance: AppCheck, isTokenAutoRefreshEnabled: boolean): void;
  261. export { Unsubscribe }
  262. export { }