@@ -30,7 +30,7 @@ import url = require('url');
30
30
import * as mocks from '../resources/mocks' ;
31
31
import { AuthProviderConfig } from '../../src/auth/auth-config' ;
32
32
import { deepExtend , deepCopy } from '../../src/utils/deep-copy' ;
33
- import { User } from '@firebase/auth-types' ;
33
+ import { User , FirebaseAuth } from '@firebase/auth-types' ;
34
34
35
35
/* tslint:disable:no-var-requires */
36
36
const chalk = require ( 'chalk' ) ;
@@ -91,6 +91,10 @@ function randomOidcProviderId(): string {
91
91
return 'oidc.' + generateRandomString ( 10 , false ) . toLowerCase ( ) ;
92
92
}
93
93
94
+ function clientAuth ( ) : FirebaseAuth {
95
+ expect ( firebase . auth ) . to . be . ok ;
96
+ return firebase . auth ! ( ) ;
97
+ }
94
98
95
99
describe ( 'admin.auth' , ( ) => {
96
100
@@ -213,7 +217,7 @@ describe('admin.auth', () => {
213
217
let currentIdToken : string ;
214
218
let currentUser : User ;
215
219
// Sign in with an email and password account.
216
- return firebase . auth ! ( ) . signInWithEmailAndPassword ( mockUserData . email , mockUserData . password )
220
+ return clientAuth ( ) . signInWithEmailAndPassword ( mockUserData . email , mockUserData . password )
217
221
. then ( ( { user} ) => {
218
222
expect ( user ) . to . exist ;
219
223
currentUser = user ! ;
@@ -248,7 +252,7 @@ describe('admin.auth', () => {
248
252
} )
249
253
. then ( ( ) => {
250
254
// New sign-in should succeed.
251
- return firebase . auth ! ( ) . signInWithEmailAndPassword (
255
+ return clientAuth ( ) . signInWithEmailAndPassword (
252
256
mockUserData . email , mockUserData . password ) ;
253
257
} )
254
258
. then ( ( { user} ) => {
@@ -273,7 +277,7 @@ describe('admin.auth', () => {
273
277
// Confirm custom claims set on the UserRecord.
274
278
expect ( userRecord . customClaims ) . to . deep . equal ( customClaims ) ;
275
279
expect ( userRecord . email ) . to . exist ;
276
- return firebase . auth ! ( ) . signInWithEmailAndPassword (
280
+ return clientAuth ( ) . signInWithEmailAndPassword (
277
281
userRecord . email ! , mockUserData . password ) ;
278
282
} )
279
283
. then ( ( { user} ) => {
@@ -302,8 +306,8 @@ describe('admin.auth', () => {
302
306
// Custom claims should be cleared.
303
307
expect ( userRecord . customClaims ) . to . deep . equal ( { } ) ;
304
308
// Force token refresh. All claims should be cleared.
305
- expect ( firebase . auth ! ( ) . currentUser ) . to . exist ;
306
- return firebase . auth ! ( ) . currentUser ! . getIdToken ( true ) ;
309
+ expect ( clientAuth ( ) . currentUser ) . to . exist ;
310
+ return clientAuth ( ) . currentUser ! . getIdToken ( true ) ;
307
311
} )
308
312
. then ( ( idToken ) => {
309
313
// Verify ID token contents.
@@ -368,7 +372,7 @@ describe('admin.auth', () => {
368
372
isAdmin : true ,
369
373
} )
370
374
. then ( ( customToken ) => {
371
- return firebase . auth ! ( ) . signInWithCustomToken ( customToken ) ;
375
+ return clientAuth ( ) . signInWithCustomToken ( customToken ) ;
372
376
} )
373
377
. then ( ( { user} ) => {
374
378
expect ( user ) . to . exist ;
@@ -388,7 +392,7 @@ describe('admin.auth', () => {
388
392
isAdmin : true ,
389
393
} )
390
394
. then ( ( customToken ) => {
391
- return firebase . auth ! ( ) . signInWithCustomToken ( customToken ) ;
395
+ return clientAuth ( ) . signInWithCustomToken ( customToken ) ;
392
396
} )
393
397
. then ( ( { user} ) => {
394
398
expect ( user ) . to . exist ;
@@ -426,7 +430,7 @@ describe('admin.auth', () => {
426
430
427
431
// Sign out after each test.
428
432
afterEach ( ( ) => {
429
- return firebase . auth ! ( ) . signOut ( ) ;
433
+ return clientAuth ( ) . signOut ( ) ;
430
434
} ) ;
431
435
432
436
// Delete test user at the end of test suite.
@@ -443,10 +447,10 @@ describe('admin.auth', () => {
443
447
. then ( ( link ) => {
444
448
const code = getActionCode ( link ) ;
445
449
expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
446
- return firebase . auth ! ( ) . confirmPasswordReset ( code , newPassword ) ;
450
+ return clientAuth ( ) . confirmPasswordReset ( code , newPassword ) ;
447
451
} )
448
452
. then ( ( ) => {
449
- return firebase . auth ! ( ) . signInWithEmailAndPassword ( email , newPassword ) ;
453
+ return clientAuth ( ) . signInWithEmailAndPassword ( email , newPassword ) ;
450
454
} )
451
455
. then ( ( result ) => {
452
456
expect ( result . user ) . to . exist ;
@@ -466,10 +470,10 @@ describe('admin.auth', () => {
466
470
. then ( ( link ) => {
467
471
const code = getActionCode ( link ) ;
468
472
expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
469
- return firebase . auth ! ( ) . applyActionCode ( code ) ;
473
+ return clientAuth ( ) . applyActionCode ( code ) ;
470
474
} )
471
475
. then ( ( ) => {
472
- return firebase . auth ! ( ) . signInWithEmailAndPassword ( email , userData . password ) ;
476
+ return clientAuth ( ) . signInWithEmailAndPassword ( email , userData . password ) ;
473
477
} )
474
478
. then ( ( result ) => {
475
479
expect ( result . user ) . to . exist ;
@@ -482,7 +486,7 @@ describe('admin.auth', () => {
482
486
return admin . auth ( ) . generateSignInWithEmailLink ( email , actionCodeSettings )
483
487
. then ( ( link ) => {
484
488
expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
485
- return firebase . auth ! ( ) . signInWithEmailLink ( email , link ) ;
489
+ return clientAuth ( ) . signInWithEmailLink ( email , link ) ;
486
490
} )
487
491
. then ( ( result ) => {
488
492
expect ( result . user ) . to . exist ;
@@ -722,6 +726,23 @@ describe('admin.auth', () => {
722
726
expect ( userRecord . uid ) . to . equal ( createdUserUid ) ;
723
727
} ) ;
724
728
} ) ;
729
+
730
+ it ( 'createCustomToken() mints a JWT that can be used to sign in tenant users' , async ( ) => {
731
+ try {
732
+ clientAuth ( ) . tenantId = createdTenantId ;
733
+
734
+ const customToken = await tenantAwareAuth . createCustomToken ( 'uid1' ) ;
735
+ const { user} = await clientAuth ( ) . signInWithCustomToken ( customToken ) ;
736
+ expect ( user ) . to . not . be . null ;
737
+ const idToken = await user ! . getIdToken ( ) ;
738
+ const token = await tenantAwareAuth . verifyIdToken ( idToken ) ;
739
+
740
+ expect ( token . uid ) . to . equal ( 'uid1' ) ;
741
+ expect ( token . firebase . tenant ) . to . equal ( createdTenantId ) ;
742
+ } finally {
743
+ clientAuth ( ) . tenantId = null ;
744
+ }
745
+ } ) ;
725
746
} ) ;
726
747
727
748
// Sanity check OIDC/SAML config management API.
@@ -1203,7 +1224,7 @@ describe('admin.auth', () => {
1203
1224
1204
1225
it ( 'creates a valid Firebase session cookie' , ( ) => {
1205
1226
return admin . auth ( ) . createCustomToken ( uid , { admin : true , groupId : '1234' } )
1206
- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1227
+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
1207
1228
. then ( ( { user} ) => {
1208
1229
expect ( user ) . to . exist ;
1209
1230
return user ! . getIdToken ( ) ;
@@ -1239,7 +1260,7 @@ describe('admin.auth', () => {
1239
1260
it ( 'creates a revocable session cookie' , ( ) => {
1240
1261
let currentSessionCookie : string ;
1241
1262
return admin . auth ( ) . createCustomToken ( uid2 )
1242
- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1263
+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
1243
1264
. then ( ( { user} ) => {
1244
1265
expect ( user ) . to . exist ;
1245
1266
return user ! . getIdToken ( ) ;
@@ -1266,7 +1287,7 @@ describe('admin.auth', () => {
1266
1287
1267
1288
it ( 'fails when called with a revoked ID token' , ( ) => {
1268
1289
return admin . auth ( ) . createCustomToken ( uid3 , { admin : true , groupId : '1234' } )
1269
- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1290
+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
1270
1291
. then ( ( { user} ) => {
1271
1292
expect ( user ) . to . exist ;
1272
1293
return user ! . getIdToken ( ) ;
@@ -1294,7 +1315,7 @@ describe('admin.auth', () => {
1294
1315
1295
1316
it ( 'fails when called with a Firebase ID token' , ( ) => {
1296
1317
return admin . auth ( ) . createCustomToken ( uid )
1297
- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1318
+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
1298
1319
. then ( ( { user} ) => {
1299
1320
expect ( user ) . to . exist ;
1300
1321
return user ! . getIdToken ( ) ;
@@ -1580,7 +1601,7 @@ function testImportAndSignInUser(
1580
1601
expect ( result . successCount ) . to . equal ( 1 ) ;
1581
1602
expect ( result . errors . length ) . to . equal ( 0 ) ;
1582
1603
// Sign in with an email and password to the imported account.
1583
- return firebase . auth ! ( ) . signInWithEmailAndPassword ( users [ 0 ] . email , rawPassword ) ;
1604
+ return clientAuth ( ) . signInWithEmailAndPassword ( users [ 0 ] . email , rawPassword ) ;
1584
1605
} )
1585
1606
. then ( ( { user} ) => {
1586
1607
// Confirm successful sign-in.
0 commit comments