app-check-public.d.ts 7.7 KB

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