@@ -20,6 +20,21 @@ import '../test/setup';
20
20
import { ComponentContainer } from '@firebase/component' ;
21
21
import { FirebaseServerAppImpl } from './firebaseServerApp' ;
22
22
import { FirebaseServerAppSettings } from './public-types' ;
23
+ import { base64Encode } from '@firebase/util' ;
24
+
25
+ const BASE64_DUMMY = base64Encode ( 'dummystrings' ) ; // encodes to ZHVtbXlzdHJpbmdz
26
+
27
+ // Creates a three part dummy token with an expiration claim in the second part. The expration
28
+ // time is based on the date offset provided.
29
+ function createServerAppTokenWithOffset ( daysOffset : number ) : string {
30
+ const timeInSeconds = Math . trunc (
31
+ new Date ( ) . setDate ( new Date ( ) . getDate ( ) + daysOffset ) / 1000
32
+ ) ;
33
+ const secondPart = JSON . stringify ( { exp : timeInSeconds } ) ;
34
+ const token =
35
+ BASE64_DUMMY + '.' + base64Encode ( secondPart ) + '.' + BASE64_DUMMY ;
36
+ return token ;
37
+ }
23
38
24
39
describe ( 'FirebaseServerApp' , ( ) => {
25
40
it ( 'has various accessors' , ( ) => {
@@ -155,4 +170,148 @@ describe('FirebaseServerApp', () => {
155
170
156
171
expect ( JSON . stringify ( app ) ) . to . eql ( undefined ) ;
157
172
} ) ;
173
+
174
+ it ( 'accepts a valid authIdToken expiration' , ( ) => {
175
+ const options = { apiKey : 'APIKEY' } ;
176
+ const authIdToken = createServerAppTokenWithOffset ( /*daysOffset=*/ 1 ) ;
177
+ const serverAppSettings : FirebaseServerAppSettings = {
178
+ automaticDataCollectionEnabled : false ,
179
+ releaseOnDeref : options ,
180
+ authIdToken
181
+ } ;
182
+ let encounteredError = false ;
183
+ try {
184
+ new FirebaseServerAppImpl (
185
+ options ,
186
+ serverAppSettings ,
187
+ 'testName' ,
188
+ new ComponentContainer ( 'test' )
189
+ ) ;
190
+ } catch ( e ) {
191
+ encounteredError = true ;
192
+ }
193
+ expect ( encounteredError ) . to . be . false ;
194
+ } ) ;
195
+
196
+ it ( 'throws when authIdToken has expired' , ( ) => {
197
+ const options = { apiKey : 'APIKEY' } ;
198
+ const authIdToken = createServerAppTokenWithOffset ( /*daysOffset=*/ - 1 ) ;
199
+ const serverAppSettings : FirebaseServerAppSettings = {
200
+ automaticDataCollectionEnabled : false ,
201
+ releaseOnDeref : options ,
202
+ authIdToken
203
+ } ;
204
+ let encounteredError = false ;
205
+ try {
206
+ new FirebaseServerAppImpl (
207
+ options ,
208
+ serverAppSettings ,
209
+ 'testName' ,
210
+ new ComponentContainer ( 'test' )
211
+ ) ;
212
+ } catch ( e ) {
213
+ encounteredError = true ;
214
+ expect ( ( e as Error ) . toString ( ) ) . to . contain (
215
+ 'app/server-app-token-expired'
216
+ ) ;
217
+ }
218
+ expect ( encounteredError ) . to . be . true ;
219
+ } ) ;
220
+
221
+ it ( 'throws when authIdToken has too few parts' , ( ) => {
222
+ const options = { apiKey : 'APIKEY' } ;
223
+ const authIdToken = 'blah' ;
224
+ const serverAppSettings : FirebaseServerAppSettings = {
225
+ automaticDataCollectionEnabled : false ,
226
+ releaseOnDeref : options ,
227
+ authIdToken : base64Encode ( authIdToken )
228
+ } ;
229
+ let encounteredError = false ;
230
+ try {
231
+ new FirebaseServerAppImpl (
232
+ options ,
233
+ serverAppSettings ,
234
+ 'testName' ,
235
+ new ComponentContainer ( 'test' )
236
+ ) ;
237
+ } catch ( e ) {
238
+ encounteredError = true ;
239
+ expect ( ( e as Error ) . toString ( ) ) . to . contain (
240
+ 'Unexpected end of JSON input'
241
+ ) ;
242
+ }
243
+ expect ( encounteredError ) . to . be . true ;
244
+ } ) ;
245
+
246
+ it ( 'accepts a valid appCheckToken expiration' , ( ) => {
247
+ const options = { apiKey : 'APIKEY' } ;
248
+ const appCheckToken = createServerAppTokenWithOffset ( /*daysOffset=*/ 1 ) ;
249
+ const serverAppSettings : FirebaseServerAppSettings = {
250
+ automaticDataCollectionEnabled : false ,
251
+ releaseOnDeref : options ,
252
+ appCheckToken
253
+ } ;
254
+ let encounteredError = false ;
255
+ try {
256
+ new FirebaseServerAppImpl (
257
+ options ,
258
+ serverAppSettings ,
259
+ 'testName' ,
260
+ new ComponentContainer ( 'test' )
261
+ ) ;
262
+ } catch ( e ) {
263
+ encounteredError = true ;
264
+ }
265
+ expect ( encounteredError ) . to . be . false ;
266
+ } ) ;
267
+
268
+ it ( 'throws when appCheckToken has expired' , ( ) => {
269
+ const options = { apiKey : 'APIKEY' } ;
270
+ const appCheckToken = createServerAppTokenWithOffset ( /*daysOffset=*/ - 1 ) ;
271
+ const serverAppSettings : FirebaseServerAppSettings = {
272
+ automaticDataCollectionEnabled : false ,
273
+ releaseOnDeref : options ,
274
+ appCheckToken
275
+ } ;
276
+ let encounteredError = false ;
277
+ try {
278
+ new FirebaseServerAppImpl (
279
+ options ,
280
+ serverAppSettings ,
281
+ 'testName' ,
282
+ new ComponentContainer ( 'test' )
283
+ ) ;
284
+ } catch ( e ) {
285
+ encounteredError = true ;
286
+ expect ( ( e as Error ) . toString ( ) ) . to . contain (
287
+ 'app/server-app-token-expired'
288
+ ) ;
289
+ }
290
+ expect ( encounteredError ) . to . be . true ;
291
+ } ) ;
292
+
293
+ it ( 'throws when appCheckToken has too few parts' , ( ) => {
294
+ const options = { apiKey : 'APIKEY' } ;
295
+ const appCheckToken = 'blah' ;
296
+ const serverAppSettings : FirebaseServerAppSettings = {
297
+ automaticDataCollectionEnabled : false ,
298
+ releaseOnDeref : options ,
299
+ appCheckToken : base64Encode ( appCheckToken )
300
+ } ;
301
+ let encounteredError = false ;
302
+ try {
303
+ new FirebaseServerAppImpl (
304
+ options ,
305
+ serverAppSettings ,
306
+ 'testName' ,
307
+ new ComponentContainer ( 'test' )
308
+ ) ;
309
+ } catch ( e ) {
310
+ encounteredError = true ;
311
+ expect ( ( e as Error ) . toString ( ) ) . to . contain (
312
+ 'Unexpected end of JSON input'
313
+ ) ;
314
+ }
315
+ expect ( encounteredError ) . to . be . true ;
316
+ } ) ;
158
317
} ) ;
0 commit comments