-
Notifications
You must be signed in to change notification settings - Fork 928
Add App Check token to FirebaseServerApp #8651
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
Changes from 18 commits
25f264f
b971b89
7c8ec93
de89ecd
e632eeb
1e511b5
ad17dab
33e4889
02708d3
c1a1322
34372c4
a5075a2
a218674
e6b6625
9da69bc
9a1299b
b3a1c4f
037041f
d6e1917
4fc151f
302e1dc
61ec38d
0526b87
c444e66
3352b7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@firebase/app': minor | ||
'firebase': minor | ||
'@firebase/data-connect': patch | ||
'@firebase/firestore': patch | ||
'@firebase/functions': patch | ||
'@firebase/database': patch | ||
'@firebase/vertexai': patch | ||
'@firebase/storage': patch | ||
'@firebase/auth': patch | ||
--- | ||
|
||
`FirebaseServerApp` may now be initalized with an App Check token in leu of invoking the App Check | ||
`getToken` method. This should unblock the use of App Check enforced products in SSR environments | ||
where the App Check SDK cannot be initialized. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,32 @@ import { ComponentContainer } from '@firebase/component'; | |
import { FirebaseAppImpl } from './firebaseApp'; | ||
import { ERROR_FACTORY, AppError } from './errors'; | ||
import { name as packageName, version } from '../package.json'; | ||
import { base64Decode } from '@firebase/util'; | ||
|
||
// Parse the token and check to see if the `exp` claim is in the future. | ||
// Throws an error if the token or claim could not be parsed, or if `exp` is in the past. | ||
function validateTokenTTL(base64Token: string, tokenName: string): void { | ||
const secondPart = base64Decode(base64Token.split('.')[1]); | ||
if (secondPart === null) { | ||
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, { | ||
tokenName | ||
}); | ||
} | ||
const expClaim = JSON.parse(secondPart).exp; | ||
if (expClaim === undefined) { | ||
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, { | ||
tokenName | ||
}); | ||
} | ||
const exp = JSON.parse(secondPart).exp * 1000; | ||
const now = new Date().getTime(); | ||
const diff = exp - now; | ||
if (diff <= 0) { | ||
throw ERROR_FACTORY.create(AppError.SERVER_APP_TOKEN_EXPIRED, { | ||
tokenName | ||
}); | ||
} | ||
} | ||
|
||
export class FirebaseServerAppImpl | ||
extends FirebaseAppImpl | ||
|
@@ -67,6 +93,16 @@ export class FirebaseServerAppImpl | |
...serverConfig | ||
}; | ||
|
||
// Ensure that the current time is within the authIdtoken window of validity. | ||
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. authIdtoken and appChecktoken look like literals that should be backticked. 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. Fixed. |
||
if (this._serverConfig.authIdToken) { | ||
validateTokenTTL(this._serverConfig.authIdToken, 'authIdToken'); | ||
} | ||
|
||
// Ensure that the current time is within the appCheckToken window of validity. | ||
if (this._serverConfig.appCheckToken) { | ||
validateTokenTTL(this._serverConfig.appCheckToken, 'appCheckToken'); | ||
} | ||
|
||
this._finalizationRegistry = null; | ||
if (typeof FinalizationRegistry !== 'undefined') { | ||
this._finalizationRegistry = new FinalizationRegistry(() => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,12 @@ export interface FirebaseServerAppSettings | |
*/ | ||
authIdToken?: string; | ||
|
||
/** | ||
* An optional App Check token. If provided, the Firebase SDKs that use App Check will utilizze | ||
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. Typo "utilize." Personally I like in lieu, but I'll bet good money our style guide advises something more like "instead of" or "in place of." 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. Fixed! I went with "in place of". |
||
* this App Check token in lieu of requiring an instance of App Check to be initialized. | ||
*/ | ||
appCheckToken?: string; | ||
|
||
/** | ||
* An optional object. If provided, the Firebase SDK uses a `FinalizationRegistry` | ||
* object to monitor the garbage collection status of the provided object. The | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these strings get pulled into release notes or other public docs?
If so, I'd go with "can now be initialized" and "instead of invoking."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't directly. Later, the release engineer crafts the release notes using these as ... inspiration. Updated!