Skip to content

Commit 79df09b

Browse files
Allow FirstPartyAuth to specify a token factory func. (#6495)
* Allow FirstPartyAuth to specify a token factory func. (#4773) * Allow firstparty credentials to specify an authToken factory that is used in lieu of direct GAPI. * Remove stray import * Add return type to private method Co-authored-by: wu-hui <[email protected]> * First Party Auth Factory * More fix * Nullable gapi * Fix Co-authored-by: Richie Foreman <[email protected]>
1 parent a80e29c commit 79df09b

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

packages/firestore/src/api/credentials.ts

+51-22
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ import { Deferred } from '../util/promise';
3737
// TODO(mikelehen): This should be split into multiple files and probably
3838
// moved to an auth/ folder to match other platforms.
3939

40+
export type AuthTokenFactory = () => string;
41+
4042
export interface FirstPartyCredentialsSettings {
4143
// These are external types. Prevent minification.
4244
['type']: 'gapi';
4345
['client']: unknown;
4446
['sessionIndex']: string;
4547
['iamToken']: string | null;
48+
['authTokenFactory']: AuthTokenFactory | null;
4649
}
4750

4851
export interface ProviderCredentialsSettings {
@@ -395,17 +398,46 @@ interface Gapi {
395398
export class FirstPartyToken implements Token {
396399
type = 'FirstParty' as TokenType;
397400
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+
) {}
399409

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']([]);
405426
}
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);
408438
}
439+
440+
return this._headers;
409441
}
410442
}
411443

@@ -418,14 +450,20 @@ export class FirstPartyAuthCredentialsProvider
418450
implements CredentialsProvider<User>
419451
{
420452
constructor(
421-
private gapi: Gapi,
453+
private gapi: Gapi | null,
422454
private sessionIndex: string,
423-
private iamToken: string | null
455+
private iamToken: string | null,
456+
private authTokenFactory: AuthTokenFactory | null
424457
) {}
425458

426459
getToken(): Promise<Token | null> {
427460
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+
)
429467
);
430468
}
431469

@@ -634,20 +672,11 @@ export function makeAuthCredentialsProvider(
634672
switch (credentials['type']) {
635673
case 'gapi':
636674
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-
);
647675
return new FirstPartyAuthCredentialsProvider(
648676
client,
649677
credentials['sessionIndex'] || '0',
650-
credentials['iamToken'] || null
678+
credentials['iamToken'] || null,
679+
credentials['authTokenFactory'] || null
651680
);
652681

653682
case 'provider':

0 commit comments

Comments
 (0)