@@ -226,23 +226,31 @@ export class FirebaseTokenVerifier {
226226 *
227227 * @param jwtToken - The Firebase Auth JWT token to verify.
228228 * @param isEmulator - Whether to accept Auth Emulator tokens.
229+ * @param clockSkewSeconds - The number of seconds to tolerate when checking the token's iat. Must be between 0-60, and an integer. Defualts to 0.
229230 * @returns A promise fulfilled with the decoded claims of the Firebase Auth ID token.
230231 */
231- public verifyJWT ( jwtToken : string , isEmulator = false ) : Promise < FirebaseIdToken > {
232+ public verifyJWT ( jwtToken : string , isEmulator = false , clockSkewSeconds : number = 0 ) : Promise < FirebaseIdToken > {
232233 if ( ! isString ( jwtToken ) ) {
233234 throw new FirebaseAuthError (
234235 AuthClientErrorCode . INVALID_ARGUMENT ,
235236 `First argument to ${ this . tokenInfo . verifyApiName } must be a ${ this . tokenInfo . jwtName } string.`
236237 ) ;
237238 }
238- return this . decodeAndVerify ( jwtToken , isEmulator ) . then ( payload => {
239+
240+ if ( clockSkewSeconds < 0 || clockSkewSeconds > 60 || ! Number . isInteger ( clockSkewSeconds ) ) {
241+ throw new FirebaseAuthError (
242+ AuthClientErrorCode . INVALID_ARGUMENT ,
243+ 'clockSkewSeconds must be an integer between 0 and 60.'
244+ )
245+ }
246+ return this . decodeAndVerify ( jwtToken , isEmulator , 0 ) . then ( payload => {
239247 payload . uid = payload . sub ;
240248 return payload ;
241249 } ) ;
242250 }
243251
244- private async decodeAndVerify ( token : string , isEmulator : boolean ) : Promise < FirebaseIdToken > {
245- const currentTimestamp = Math . ceil ( Date . now ( ) / 1000 ) ;
252+ private async decodeAndVerify ( token : string , isEmulator : boolean , clockSkewSeconds : number = 0 ) : Promise < FirebaseIdToken > {
253+ const currentTimestamp = Math . floor ( Date . now ( ) / 1000 ) + clockSkewSeconds ;
246254 try {
247255 const rs256Token = this . safeDecode ( token , isEmulator , currentTimestamp ) ;
248256 const { payload } = rs256Token . decodedToken ;
0 commit comments