-
Notifications
You must be signed in to change notification settings - Fork 937
Implement ReCaptchaEnterprise for App Check #5595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2e48e6c
Add recaptcha enterprise provider
hsubox76 9efa5a5
start on app-check-compat
hsubox76 f1d7b39
Correct Enterprise URL
hsubox76 1ac24a5
prettier
hsubox76 5231107
Add changeset
hsubox76 facc66f
start on app-check-compat
hsubox76 3fe7113
Add enterprise endpoint
hsubox76 e5725c2
Fix tests
hsubox76 5950d04
update index.d.ts
hsubox76 3df0e4b
Add more tests
hsubox76 5e63c0e
Update .changeset/ten-impalas-wink.md
hsubox76 d9a94e5
Address PR comments
hsubox76 1477169
Update doc comment
hsubox76 8f98ff5
Address doc-related PR comments
hsubox76 7adaa26
Formatting pass
hsubox76 54b258c
update comments
Feiyang1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@firebase/app-check': minor | ||
'@firebase/app-check-compat': minor | ||
--- | ||
|
||
Add ReCAPTCHA Enterprise as an attestation option for App Check. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,105 @@ | ||
## API Report File for "@firebase/app-check" | ||
|
||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
|
||
```ts | ||
|
||
import { FirebaseApp } from '@firebase/app'; | ||
import { PartialObserver } from '@firebase/util'; | ||
import { Unsubscribe } from '@firebase/util'; | ||
|
||
// @public | ||
export interface AppCheck { | ||
app: FirebaseApp; | ||
} | ||
|
||
// @internal (undocumented) | ||
export type _AppCheckComponentName = 'app-check'; | ||
|
||
// @internal (undocumented) | ||
export type _AppCheckInternalComponentName = 'app-check-internal'; | ||
|
||
// @public | ||
export interface AppCheckOptions { | ||
isTokenAutoRefreshEnabled?: boolean; | ||
provider: CustomProvider | ReCaptchaV3Provider; | ||
} | ||
|
||
// @public | ||
export interface AppCheckToken { | ||
readonly expireTimeMillis: number; | ||
// (undocumented) | ||
readonly token: string; | ||
} | ||
|
||
// @public | ||
export type AppCheckTokenListener = (token: AppCheckTokenResult) => void; | ||
|
||
// @public | ||
export interface AppCheckTokenResult { | ||
readonly token: string; | ||
} | ||
|
||
// Warning: (ae-forgotten-export) The symbol "AppCheckProvider" needs to be exported by the entry point index.d.ts | ||
// | ||
// @public | ||
export class CustomProvider implements AppCheckProvider { | ||
constructor(_customProviderOptions: CustomProviderOptions); | ||
// Warning: (ae-forgotten-export) The symbol "AppCheckTokenInternal" needs to be exported by the entry point index.d.ts | ||
// | ||
// @internal (undocumented) | ||
getToken(): Promise<AppCheckTokenInternal>; | ||
// @internal (undocumented) | ||
initialize(app: FirebaseApp): void; | ||
// @internal (undocumented) | ||
isEqual(otherProvider: unknown): boolean; | ||
} | ||
|
||
// @public | ||
export interface CustomProviderOptions { | ||
getToken: () => Promise<AppCheckToken>; | ||
} | ||
|
||
// @public | ||
export function getToken(appCheckInstance: AppCheck, forceRefresh?: boolean): Promise<AppCheckTokenResult>; | ||
|
||
// @public | ||
export function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck; | ||
|
||
// @public | ||
export function onTokenChanged(appCheckInstance: AppCheck, observer: PartialObserver<AppCheckTokenResult>): Unsubscribe; | ||
|
||
// @public | ||
export function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe; | ||
|
||
export { PartialObserver } | ||
|
||
// @public | ||
export class ReCaptchaV3Provider implements AppCheckProvider { | ||
constructor(_siteKey: string); | ||
// @internal | ||
getToken(): Promise<AppCheckTokenInternal>; | ||
// @internal (undocumented) | ||
initialize(app: FirebaseApp): void; | ||
// @internal (undocumented) | ||
isEqual(otherProvider: unknown): boolean; | ||
} | ||
|
||
// @public | ||
export function setTokenAutoRefreshEnabled(appCheckInstance: AppCheck, isTokenAutoRefreshEnabled: boolean): void; | ||
|
||
export { Unsubscribe } | ||
|
||
|
||
``` | ||
## API Report File for "@firebase/app-check" | ||
|
||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
|
||
```ts | ||
|
||
import { FirebaseApp } from '@firebase/app'; | ||
import { PartialObserver } from '@firebase/util'; | ||
import { Unsubscribe } from '@firebase/util'; | ||
|
||
// @public | ||
export interface AppCheck { | ||
app: FirebaseApp; | ||
} | ||
|
||
// @internal (undocumented) | ||
export type _AppCheckComponentName = 'app-check'; | ||
|
||
// @internal (undocumented) | ||
export type _AppCheckInternalComponentName = 'app-check-internal'; | ||
|
||
// @public | ||
export interface AppCheckOptions { | ||
isTokenAutoRefreshEnabled?: boolean; | ||
provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider; | ||
} | ||
|
||
// @public | ||
export interface AppCheckToken { | ||
readonly expireTimeMillis: number; | ||
// (undocumented) | ||
readonly token: string; | ||
} | ||
|
||
// @public | ||
export type AppCheckTokenListener = (token: AppCheckTokenResult) => void; | ||
|
||
// @public | ||
export interface AppCheckTokenResult { | ||
readonly token: string; | ||
} | ||
|
||
// Warning: (ae-forgotten-export) The symbol "AppCheckProvider" needs to be exported by the entry point index.d.ts | ||
// | ||
// @public | ||
export class CustomProvider implements AppCheckProvider { | ||
constructor(_customProviderOptions: CustomProviderOptions); | ||
// Warning: (ae-forgotten-export) The symbol "AppCheckTokenInternal" needs to be exported by the entry point index.d.ts | ||
// | ||
// @internal (undocumented) | ||
getToken(): Promise<AppCheckTokenInternal>; | ||
// @internal (undocumented) | ||
initialize(app: FirebaseApp): void; | ||
// @internal (undocumented) | ||
isEqual(otherProvider: unknown): boolean; | ||
} | ||
|
||
// @public | ||
export interface CustomProviderOptions { | ||
getToken: () => Promise<AppCheckToken>; | ||
} | ||
|
||
// @public | ||
export function getToken(appCheckInstance: AppCheck, forceRefresh?: boolean): Promise<AppCheckTokenResult>; | ||
|
||
// @public | ||
export function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck; | ||
|
||
// @public | ||
export function onTokenChanged(appCheckInstance: AppCheck, observer: PartialObserver<AppCheckTokenResult>): Unsubscribe; | ||
|
||
// @public | ||
export function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe; | ||
|
||
export { PartialObserver } | ||
|
||
// @public | ||
export class ReCaptchaEnterpriseProvider implements AppCheckProvider { | ||
constructor(_siteKey: string); | ||
// @internal | ||
getToken(): Promise<AppCheckTokenInternal>; | ||
// @internal (undocumented) | ||
initialize(app: FirebaseApp): void; | ||
// @internal (undocumented) | ||
isEqual(otherProvider: unknown): boolean; | ||
} | ||
|
||
// @public | ||
export class ReCaptchaV3Provider implements AppCheckProvider { | ||
constructor(_siteKey: string); | ||
// @internal | ||
getToken(): Promise<AppCheckTokenInternal>; | ||
// @internal (undocumented) | ||
initialize(app: FirebaseApp): void; | ||
// @internal (undocumented) | ||
isEqual(otherProvider: unknown): boolean; | ||
} | ||
|
||
// @public | ||
export function setTokenAutoRefreshEnabled(appCheckInstance: AppCheck, isTokenAutoRefreshEnabled: boolean): void; | ||
|
||
export { Unsubscribe } | ||
|
||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,67 @@ export class ReCaptchaV3Provider implements AppCheckProvider { | |
} | ||
} | ||
|
||
/** | ||
* App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kevinthecheung This comment will go into documentation. |
||
* for an App Check token. | ||
* | ||
* @public | ||
*/ | ||
export class ReCaptchaEnterpriseProvider implements AppCheckProvider { | ||
private _app?: FirebaseApp; | ||
private _platformLoggerProvider?: Provider<'platform-logger'>; | ||
/** | ||
* Create a ReCaptchaV3Provider instance. | ||
* @param siteKey - ReCAPTCHA V3 siteKey. | ||
Feiyang1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
constructor(private _siteKey: string) {} | ||
|
||
/** | ||
* Returns an App Check token. | ||
* @internal | ||
*/ | ||
async getToken(): Promise<AppCheckTokenInternal> { | ||
if (!this._app || !this._platformLoggerProvider) { | ||
// This should only occur if user has not called initializeAppCheck(). | ||
// We don't have an appName to provide if so. | ||
// This should already be caught in the top level `getToken()` function. | ||
throw ERROR_FACTORY.create(AppCheckError.USE_BEFORE_ACTIVATION, { | ||
appName: '' | ||
}); | ||
} | ||
Feiyang1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const attestedClaimsToken = await getReCAPTCHAToken(this._app).catch(_e => { | ||
// reCaptcha.execute() throws null which is not very descriptive. | ||
throw ERROR_FACTORY.create(AppCheckError.RECAPTCHA_ERROR); | ||
}); | ||
return exchangeToken( | ||
getExchangeRecaptchaTokenRequest(this._app, attestedClaimsToken, true), | ||
this._platformLoggerProvider | ||
); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
initialize(app: FirebaseApp): void { | ||
this._app = app; | ||
this._platformLoggerProvider = _getProvider(app, 'platform-logger'); | ||
initializeRecaptcha(app, this._siteKey, true).catch(() => { | ||
/* we don't care about the initialization result */ | ||
}); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
isEqual(otherProvider: unknown): boolean { | ||
if (otherProvider instanceof ReCaptchaEnterpriseProvider) { | ||
return this._siteKey === otherProvider._siteKey; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Custom provider class. | ||
* @public | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.