Skip to content

Commit 54ce678

Browse files
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]>
1 parent 6a17eb6 commit 54ce678

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 48 additions & 19 deletions
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 {
@@ -397,11 +400,40 @@ export class FirstPartyToken implements Token {
397400
user = User.FIRST_PARTY;
398401
headers = new Map();
399402

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);
403+
constructor(
404+
private readonly gapi: Gapi,
405+
private readonly sessionIndex: string,
406+
private readonly iamToken: string | null,
407+
private readonly authTokenFactory: AuthTokenFactory | null
408+
) {}
409+
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']([]);
426+
}
427+
}
428+
429+
get authHeaders(): { [header: string]: string } {
430+
const headers: { [header: string]: string } = {
431+
'X-Goog-AuthUser': this.sessionIndex
432+
};
433+
// Use array notation to prevent minification
434+
const authHeaderTokenValue = this.getAuthToken();
435+
if (authHeaderTokenValue) {
436+
headers['Authorization'] = authHeaderTokenValue;
405437
}
406438
if (iamToken) {
407439
this.headers.set('X-Goog-Iam-Authorization-Token', iamToken);
@@ -420,12 +452,18 @@ export class FirstPartyAuthCredentialsProvider
420452
constructor(
421453
private gapi: Gapi,
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-
);
647-
return new FirstPartyAuthCredentialsProvider(
675+
return new FirstPartyCredentialsProvider(
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)