@@ -37,12 +37,15 @@ import { Deferred } from '../util/promise';
37
37
// TODO(mikelehen): This should be split into multiple files and probably
38
38
// moved to an auth/ folder to match other platforms.
39
39
40
+ export type AuthTokenFactory = ( ) => string ;
41
+
40
42
export interface FirstPartyCredentialsSettings {
41
43
// These are external types. Prevent minification.
42
44
[ 'type' ] : 'gapi' ;
43
45
[ 'client' ] : unknown ;
44
46
[ 'sessionIndex' ] : string ;
45
47
[ 'iamToken' ] : string | null ;
48
+ [ 'authTokenFactory' ] : AuthTokenFactory | null ;
46
49
}
47
50
48
51
export interface ProviderCredentialsSettings {
@@ -395,17 +398,46 @@ interface Gapi {
395
398
export class FirstPartyToken implements Token {
396
399
type = 'FirstParty' as TokenType ;
397
400
user = User . FIRST_PARTY ;
398
- headers = new Map ( ) ;
401
+ private _headers = new Map ( ) ;
402
+
403
+ constructor (
404
+ private readonly gapi : Gapi | null ,
405
+ private readonly sessionIndex : string ,
406
+ private readonly iamToken : string | null ,
407
+ private readonly authTokenFactory : AuthTokenFactory | null
408
+ ) { }
399
409
400
- constructor ( gapi : Gapi , sessionIndex : string , iamToken : string | null ) {
401
- this . headers . set ( 'X-Goog-AuthUser' , sessionIndex ) ;
402
- const authHeader = gapi [ 'auth' ] [ 'getAuthHeaderValueForFirstParty' ] ( [ ] ) ;
403
- if ( authHeader ) {
404
- this . headers . set ( 'Authorization' , authHeader ) ;
410
+ /** Gets an authorization token, using a provided factory function, or falling back to First Party GAPI. */
411
+ private getAuthToken ( ) : string | null {
412
+ if ( this . authTokenFactory ) {
413
+ return this . authTokenFactory ( ) ;
414
+ } else {
415
+ // Make sure this really is a Gapi client.
416
+ hardAssert (
417
+ ! ! (
418
+ typeof this . gapi === 'object' &&
419
+ this . gapi !== null &&
420
+ this . gapi [ 'auth' ] &&
421
+ this . gapi [ 'auth' ] [ 'getAuthHeaderValueForFirstParty' ]
422
+ ) ,
423
+ 'unexpected gapi interface'
424
+ ) ;
425
+ return this . gapi ! [ 'auth' ] [ 'getAuthHeaderValueForFirstParty' ] ( [ ] ) ;
405
426
}
406
- if ( iamToken ) {
407
- this . headers . set ( 'X-Goog-Iam-Authorization-Token' , iamToken ) ;
427
+ }
428
+
429
+ get headers ( ) : Map < string , string > {
430
+ this . _headers . set ( 'X-Goog-AuthUser' , this . sessionIndex ) ;
431
+ // Use array notation to prevent minification
432
+ const authHeaderTokenValue = this . getAuthToken ( ) ;
433
+ if ( authHeaderTokenValue ) {
434
+ this . _headers . set ( 'Authorization' , authHeaderTokenValue ) ;
435
+ }
436
+ if ( this . iamToken ) {
437
+ this . _headers . set ( 'X-Goog-Iam-Authorization-Token' , this . iamToken ) ;
408
438
}
439
+
440
+ return this . _headers ;
409
441
}
410
442
}
411
443
@@ -418,14 +450,20 @@ export class FirstPartyAuthCredentialsProvider
418
450
implements CredentialsProvider < User >
419
451
{
420
452
constructor (
421
- private gapi : Gapi ,
453
+ private gapi : Gapi | null ,
422
454
private sessionIndex : string ,
423
- private iamToken : string | null
455
+ private iamToken : string | null ,
456
+ private authTokenFactory : AuthTokenFactory | null
424
457
) { }
425
458
426
459
getToken ( ) : Promise < Token | null > {
427
460
return Promise . resolve (
428
- new FirstPartyToken ( this . gapi , this . sessionIndex , this . iamToken )
461
+ new FirstPartyToken (
462
+ this . gapi ,
463
+ this . sessionIndex ,
464
+ this . iamToken ,
465
+ this . authTokenFactory
466
+ )
429
467
) ;
430
468
}
431
469
@@ -634,20 +672,11 @@ export function makeAuthCredentialsProvider(
634
672
switch ( credentials [ 'type' ] ) {
635
673
case 'gapi' :
636
674
const client = credentials [ 'client' ] as Gapi ;
637
- // Make sure this really is a Gapi client.
638
- hardAssert (
639
- ! ! (
640
- typeof client === 'object' &&
641
- client !== null &&
642
- client [ 'auth' ] &&
643
- client [ 'auth' ] [ 'getAuthHeaderValueForFirstParty' ]
644
- ) ,
645
- 'unexpected gapi interface'
646
- ) ;
647
675
return new FirstPartyAuthCredentialsProvider (
648
676
client ,
649
677
credentials [ 'sessionIndex' ] || '0' ,
650
- credentials [ 'iamToken' ] || null
678
+ credentials [ 'iamToken' ] || null ,
679
+ credentials [ 'authTokenFactory' ] || null
651
680
) ;
652
681
653
682
case 'provider' :
0 commit comments