@@ -26,6 +26,33 @@ import { ComponentContainer } from '@firebase/component';
26
26
import { FirebaseAppImpl } from './firebaseApp' ;
27
27
import { ERROR_FACTORY , AppError } from './errors' ;
28
28
import { name as packageName , version } from '../package.json' ;
29
+ import { base64Decode } from '@firebase/util' ;
30
+
31
+ // Parse the token and check to see if the `exp` claim is in the future.
32
+ // Throws an error if the token or claim could not be parsed, or if `exp` is in the past.
33
+ function validateTokenTTL ( base64Token : string , tokenName : string ) : void {
34
+ const secondPart = base64Decode ( base64Token . split ( '.' ) [ 1 ] ) ;
35
+ if ( secondPart === null ) {
36
+ throw ERROR_FACTORY . create ( AppError . INVALID_SERVER_APP_TOKEN_FORMAT , {
37
+ tokenName
38
+ } ) ;
39
+ }
40
+ const expClaim = JSON . parse ( secondPart ) . exp ;
41
+ if ( expClaim === undefined ) {
42
+ throw ERROR_FACTORY . create ( AppError . INVALID_SERVER_APP_TOKEN_FORMAT , {
43
+ tokenName
44
+ } ) ;
45
+ }
46
+ const exp = JSON . parse ( secondPart ) . exp * 1000 ;
47
+ const now = new Date ( ) . getTime ( ) ;
48
+ // const now = new Date(new Date().getDate() - 1).now()
49
+ const diff = exp - now ;
50
+ if ( diff <= 0 ) {
51
+ throw ERROR_FACTORY . create ( AppError . SERVER_APP_TOKEN_EXPIRED , {
52
+ tokenName
53
+ } ) ;
54
+ }
55
+ }
29
56
30
57
export class FirebaseServerAppImpl
31
58
extends FirebaseAppImpl
@@ -67,6 +94,16 @@ export class FirebaseServerAppImpl
67
94
...serverConfig
68
95
} ;
69
96
97
+ // Validate the authIdtoken validation window.
98
+ if ( this . _serverConfig . authIdToken ) {
99
+ validateTokenTTL ( this . _serverConfig . authIdToken , 'authIdToken' ) ;
100
+ }
101
+
102
+ // Validate the appCheckToken validation window.
103
+ if ( this . _serverConfig . appCheckToken ) {
104
+ validateTokenTTL ( this . _serverConfig . appCheckToken , 'appCheckToken' ) ;
105
+ }
106
+
70
107
this . _finalizationRegistry = null ;
71
108
if ( typeof FinalizationRegistry !== 'undefined' ) {
72
109
this . _finalizationRegistry = new FinalizationRegistry ( ( ) => {
0 commit comments